7. 创建一个 java web 项目
- 首先我们在 tomcat 的 webapp 路径下创建一个标准的 web 项目目录结构
mkdir -p /usr/local/tomcat/webapps/myapp/{WEB-INF,classes,lib}
# tree /usr/local/tomcat/webapps/myapp/
/usr/local/tomcat/webapps/myapp/
├── classes
├── lib
└── WEB-INF
- 在项目路径 myapp 下创建 index.html
echo '<h1>myapp of tomcatttt</h1>' > /usr/local/tomcat/webapps/myapp/index.html
- 创建index.jsp 文件
vim /usr/local/tomcat/webapps/myapp/index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>myapp of magedu</title>
</head>
<body>
<h1> 这个是JSP文件... </h1>
<%
out.println("hello jsp");
%>
</body>
</html>
- 创建web.xml
vim /usr/local/tomcat/webapps/myapp/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"
metadata-complete="true">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to myapp web project !
</description>
</web-app>
- 设置权限
chown -R tomcat.tomcat /usr/local/tomcat/
- 重启服务
su - tomcat -c '/usr/local/tomcat/bin/shutdown.sh'
su - tomcat -c '/usr/local/tomcat/bin/startup.sh'
- 测试
8. 虚拟主机配置实验
尝试配置一个虚拟主机,并将myapp部署到/data/webapps目录下
- 我们在 /data/webapps 路径下创建一个标准的 web 项目目录结构:
mkdir -p /data/webapps/myapp/{WEB-INF,classes,lib}
- 在项目路径 myapp 下创建 index.html
echo '<h1>myapp of tomcatt under data-webapps </h1>' > /data/webapps/myapp/index.html
- 创建index.jsp 文件
vim /data/webapps/myapp/index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>myapp of magedu</title>
</head>
<body>
<h1> myapp web project in /data/webapps/myapp </h1>
<%
out.println("hello jsp");
%>
</body>
</html>
- 创建web.xml
vim /data/webapps/myapp/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"
metadata-complete="true">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to myapp web project !
</description>
</web-app>
- 设置权限
chown -R tomcat.tomcat /data
- 修改配置文件
vim /usr/local/tomcat/conf/server.xml
...省略
40 </Host>
41 <Host name="www.tommmcat.net" appBase="/data/webapps/" unpackWARs="True" autoDeploy="false" />
42 </Engine>
...省略
在
<Engine>
内增加一段<Host>
在虚拟主机中主机名定义www.tommmcat.net
,所以需要主机在本机手动配置一个域名解析。
- 重启服务
su - tomcat -c '/usr/local/tomcat/bin/shutdown.sh'
su - tomcat -c '/usr/local/tomcat/bin/startup.sh'
- 在host文件增加一行
vim /etc/hosts
192.168.99.121 www.tommmcat.net
- 测试
curl www.tommmcat.net:8080/myapp/
curl www.tommmcat.net:8080/myapp/index.jsp
8.1. 虚拟主机配置中 context 配置选项
Context作用:路径映射
应用独立配置,例如单独配置应用日志、单独配置应用访问控制
path
,指的是访问的url的路径
docBase
,文件路径,可以是绝对路径,也可以是相对路径(相对于Host的appBase)
reloadable
,true表示如果WEB-INF/classes或META-INF/lib目录下.class文件有改动,就会将WEB应用重新加载。生成环境中,会使用false来禁用。
- 复制一份 webapp 项目到 /data/ 路径下
cd /data
cp -r webapps/myapp ./
- 修改index.html
vim myapp/index.html
<h1>myapp of context page under data-myappv1 </h1>
- 修改index.jsp
vim myapp/index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>myapp of magedu</title>
</head>
<body>
<h1> test web project in /data/test </h1>
<%
out.println("hello jsp");
%>
</body>
</html>
- 修改目录名
mv myapp test
- 设置权限
chown -R tomcat.tomcat /data
- 修改配置文件
vim /usr/local/tomcat/conf/server.xml
<Host name="www.tommmcat.net" appBase="/data/webapps" unpackWARs="true" autoDeploy="true" >
<Context path="/test" docBase="/data/test" reloadable="" />
</Host>
在
<Engine>
内增加一段<Host>
- 重启服务
su - tomcat -c '/usr/local/tomcat/bin/shutdown.sh'
su - tomcat -c '/usr/local/tomcat/bin/startup.sh'
- 测试
curl www.tommmcat.net:8080/test/
curl www.tommmcat.net:8080/test/index.jsp
- 目录结构
# ll /usr/local/tomcat/work/Catalina/www.tommmcat.net/
total 0
drwxr-x--- 3 tomcat tomcat 17 Sep 2 15:23 myapp
drwxr-x--- 3 tomcat tomcat 17 Sep 2 15:24 test
8.2. tomcat 的 web.xml 配置介绍
- 路径
/usr/local/tomcat/conf/web.xml
- 我们将 web.xml 中的注释删除掉,
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>fork</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>xpoweredBy</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<!-- Note: Extensions are always matched in a case-insensitive manner. -->
<mime-mapping>
<extension>123</extension>
<mime-type>application/vnd.lotus-1-2-3</mime-type>
</mime-mapping>
.
.
.mine-mapping 的配置文件有几千行,这里我省略展示.........
.
.
<mime-mapping>
<extension>zmm</extension>
<mime-type>application/vnd.handheld-entertainment+xml</mime-type>
</mime-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
- 配置内容和关系
通过上面的 web.xml 配置文件,我们发现,有 servlet 和 servlet-mapping, session-config, mime-mapping, welcome-file-list 配置端。
其中 servlet 和 servlet-mapping 定了以 java 后台请求的入口, session-config 定义了 session 会话的配置, welcome-file-list 定义了默认主页的访问顺序,根据默认配置,index.html 高于 index.htm 高于 index.jsp