0%

tomcat之web配置文件

web.xml配置文件

web.xml是web应用的部署文件,包括tomcat中conf/web.xml中的默认配置以及web应用WEB-INF/web.xml下的定制配置

主要分为以下几类

  • ServletContext初始化参数

  • 会话配置

  • Servlet声明配置

  • 应用生命周期监听器

  • Filter定义及映射

  • MIME类型映射

  • 欢迎文件列表

  • 错误页面

  • 本地化及编码映射

  • 安全配置

  • JNDI配置

ServletContext初始化参数

使用添加初始化参数,可以使用javax.servlet.ServletContext.getInitParameter()方法获取参数值

1
2
3
4
<context-param>
<param-name>name</param-name>
<param-value>value</param-value>
</context-param>

会话配置

用于配置web应用会话,包括超时时间、cookie配置以及会话追踪模式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<session-config>
<session-timeout>30</session-timeout>
<cookie-config>
<name>jessionid</name>
<domain>myApp.com</domain>
<path>/</path>
<!-- 安全性配置,只有mode为cookie时才会生效 -->
<http-only>true</http-only>
<!-- 安全性配置,只有mode为cookie时才会生效 -->
<secure>true</secure>
<max-age>3600</max-age>
</cookie-config>
<!-- 会话追踪模式 -->
<tracking-mode>COOKIE</tracking-mode>
</session-config>

Servlet支持三种会话追踪模式:COOKIE、URL、SSL

  • COOKIE:通过HTTP Cookie追踪会话,当首次发起HTTP请求时,Servlet容器会发送一个用于会话的Cookie到客户端,在后续请求中,客户端会将该Cookie返回到服务端,服务端根据该Cookie确定请求会话。默认情况下,Cookie的名称为JSESSIONID,可以通过的sessionCookieName属性或者的name属性修改
  • URL:当客户端不支持Cookie时,可以采用URL重写的方式。当采用URL追踪模式时,请求路径需要包含会话标识信息,Servlet容器会根据路径中的会话标识设置请求的会话信息,参数名为jessionid
  • SSL:对于SSL请求,通过SSL会话标识确定请求会话标识

Servlet声明和映射

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<servlet>
<servlet-name>ad</servlet-name>
<servlet-class>com.zhanghe.servlet.MyMainServlet</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>value</param-value>
</init-param>
<!-- 当值为0或大于0时,在应用启动时就加载这个servlet,且数值越小,优先级越高;当值为负数或没有指定时,该servlet被使用时才会加载 -->
<load-on-startup>1</load-on-startup>
<!-- 上传文件配置 -->
<multipart-config>
<max-file-size>1024</max-file-size>
<max-request-size>2048</max-request-size>
<!-- 文件超过该大小后将写入磁盘 -->
<file-size-threshold>0</file-size-threshold>
<!-- 标识该Servlet是否启用 -->
<enabled>true</enabled>
<!-- 安全角色的引用 -->
<security-role-ref>
<!-- 代码中使用的角色名称 -->
<role-name>admin</role-name>
<!-- 对应tomcat-user.xml中服务器配置角色名称 -->
<role-link>manager</role-link>
</security-role-ref>
</multipart-config>
</servlet>
<servlet-mapping>
<servlet-name>ad</servlet-name>
<url-pattern>*.*</url-pattern>
<url-pattern>/myapp/*</url-pattern>
</servlet-mapping>

应用生命周期监听器

监听器要实现javax.servlet.ServletContextListener接口,执行顺序与web.xml的配置顺序一致,停止时与启动顺序相反

1
2
3
<listener>
<listener-class>com.zhanghe.listener.MyListener</listener-class>
</listener>

Filter定义和映射

过滤器,用于过滤资源请求及响应

1
2
3
4
5
6
7
8
9
10
11
12
13
<filter>
<filter-name>myFilter</filter-name>
<filter-class>com.zhanghe.filter.MyFilter</filter-class>
<init-param>
<param-name>name</param-name>
<param-value>value</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>myFilter</filter-name>
<!-- 也可以设置servlet-name -->
<url-pattern>/myApp/*</url-pattern>
</filter-mapping>

MIME类型映射

设定某类型的扩展名文件使用何种应用程序打开

1
2
3
4
<mime-mapping>
<extension>doc</extension>
<mime-type>application/msword</mime-type>
</mime-mapping>

欢迎文件列表

1
2
3
4
5
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

错误页面

1
2
3
4
5
6
7
8
<error-page>
<error-code>404</error-code>
<localtion>/404.html</localtion>
</error-page>
<error-page>
<error-type>java.lang.Exception</error-type>
<localtion>/error.html</localtion>
</error-page>

本地化及编码映射

1
2
3
4
5
6
<locale-encoding-mapping-list>
<locale-encoding-mapping>
<locale>zh</locale>
<encoding>UTF-8</encoding>
</locale-encoding-mapping>
</locale-encoding-mapping-list>

安全配置

为web应用增加页面的访问权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!-- 针对符合url-pattern的请求进行安全约束 -->
<security-constraint>
<display-name>user</display-name>
<web-resource-collection>
<web-resource-name>user</web-resource-name>
<url-pattern>*.html</url-pattern>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>user</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<!-- 为web应用添加角色 -->
<security-role>
<user-name>user</user-name>
</security-role>
<!-- 用于指定对于security-constraint中http-method中未指定的请求是否允许访问 -->
<deny-uncovered-http-methods></deny-uncovered-http-methods>
<!-- 认证方式 BASIC/DIGEST/FORM/CLIENT-CERT -->
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.html</form-login-page>
<form-error-page>/error.html</form-error-page>
</form-login-config>
</login-config>

欢迎关注我的其它发布渠道