web.xml :
是j2ee 项目最基本的配置文件,是服务器加载项目的入口,
当然3.0开始也可以在代码中中配置项目所需要的一系列参数。
在tomcat conf中存在web.xml,项目中的配置都会继承和覆盖该文件的配置。
基本配置:
1.欢迎文件设置:
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
这是tomcat 中web.xml的默认配置,在项目中定义会覆盖之。
2.错误处理设置:
<error-page>
<error-code>404</error-code>
<location>/notFileFound.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.NullPointerException</exception-type>
<location>/null.jsp</location>
</error-page>
如果某文件资源没有找到,服务器要报404错误,按上述配置则会调用\webapps\ROOT\notFileFound.jsp。
如果执行的某个JSP文件产生NullPointException ,则会调用\webapps\ROOT\null.jsp
3.回话超时设置:
<session-config>
<session-timeout>30</session-timeout>
</session-config>
设置session 的过期时间,单位是分钟,
tomcat 默认值是30min.
从servlet3.0 开始带了许多安全性的设置:
<session-config>
会话连接,保持状态的设置:
<cookie-config>
<secure>true</secure>
只允许在SSL下进行交互,默认值为false.
<http-only>true</http-only> 确保cookie不能被客户端脚本访问。这帮助减轻了一些常见的XSS攻击,默认值为false
<comment></comment> 会话备注
<domain></domain> 会话保存域
<max-age></max-age> 会话保存最大时间,默认值:-1
<name></name> 会话名称 默认值:JSESSIONID
<path></path> 会话保存路径
</cookie-config>
<tracking-mode>COOKIE</tracking-mode>
允许你定义JSESSIONID是存储在cookie中还是URL参数中
</session-config>
4.过滤器设置:
<filter>
<filter-name>openEntityManagerInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
<async-supported>true</async-supported>
</filter>
<filter-mapping>
<filter-name>openEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
1) 身份验证的过滤Authentication Filters
2) 日志和审核的过滤Logging and Auditing Filters
3) 图片转化的过滤Image conversion Filters
4) 数据压缩的过滤Data compression Filters
5) 加密过滤Encryption Filters
6) Tokenizing Filters
7) 资源访问事件触发的过滤Filters that trigger resource access events XSL/T 过滤XSL/T filters
8) 内容类型的过滤Mime-type chain Filter 注意监听器的顺序,如:先安全过滤,然后资源,
然后内容类型等,这个顺序可以自己定。
tomcat 内置filter:SSIFilter ,SetCharacterEncodingFilter, FailedRequestFilter ..
默认为开启。
5.Servlet 管理:
<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>
tomcat 内置servlet
有默认开启:DefaultServlet,JspServlet,
默认关闭:SSIServlet,CGIServlet
6.应用 图片表示:
<icon>
<small-icon>/images/small.gif</small-icon>
<large-icon>/images/large.gif</large-icon>
</icon>
small :16 X 16,large:32 X 32 。 后缀扩展名:.gif或.jpg.
7.应用全局上下文参数
<context-param>
<param-name>param_name</param-name>
<param-value>param_value</param-value>
</context-param>
8.监听器listener:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
监听器负责监听服务器和应用的各种事件状态。
tomcat 中内容的监听器有
AprLifecycleListener,JasperListener,JreMemoryLeakPreventionListener,
GlobalResourcesLifecycleListener,ThreadLocalLeakPreventionListener
这些监听器都默认配置在server.xml中。
9.媒体映射<mime-mapping>
<mime-mapping>
<extension>doc</extension>
<mime-type>application/msword</mime-type>
</mime-mapping>
在请求静态资源时,tomcat 会根据后缀名来映射媒体类型自动加上Content-Type 头。
tomcat 内置包含了很多映射配置,如果需要自定义在web.xml中追加。
备注:如果自定义代码下载文件一定得加上响应的Content-Type头。
10.jsp 配置 <jsp-config>
jsp-config元素主要用来设定JSP的相关配置,<jsp:config>包括<taglib>和<jsp-property-group>两个子元素。
<jsp-config>
<taglib>
taglib元素包含两个子元素taglib-uri和taglib-location.用来设定JSP网页用到的Tag Library路径.
<taglib-uri>URI</taglib-uri>
taglib-uri定义TLD文件的URI,JSP网页的taglib指令可以经由这个URI存取到TLD文件.
<taglib-location>/WEB-INF/lib/xxx.tld</taglib-laction>
TLD文件对应Web站台的存放位置.
</taglib>
<jsp-property-group>
<description>Description</descrition>
此设定的说明
<display-name>Name</display-name>
此设定的名称
<url-pattern>URL</url-pattern>
设定值所影响的范围,如:/CH2 或者/*.jsp
<el-ignored>true|false</el-ignored>
若为true,表示不支持EL语法.
<scripting-invalid>true|false</scripting-invalid>
若为true表示不支持<%scription%>语法.
<page-encoding>encoding</page-encoding>
设定JSP网页的编码
<include-prelude>.jspf</include-prelude>
设置JSP网页的抬头,扩展名为.jspf
<include-coda>.jspf</include-coda>
设置JSP网页的结尾,扩展名为.jspf
</jsp-property-group>
<jsp-config>
11、资源管理对象配置
<resource-env-ref>
<resource-env-ref-name>jms/StockQueue</resource-env-ref-name>
</resource-env-ref>
12、资源工厂配置
<resource-ref>
<res-ref-name>mail/Session</res-ref-name>
<res-type>javax.mail.Session</res-type>
<res-auth>Container</res-auth>
</resource-ref>
配置数据库连接池就可在此配置:
<resource-ref>
<description>JNDI JDBC DataSource of shop</description>
<res-ref-name>jdbc/sample_db</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
13、安全限制配置
<security-constraint>
<display-name>Example Security Constraint</display-name>
<web-resource-collection>
<web-resource-name>Protected Area</web-resource-name>
<url-pattern>/jsp/security/protected/*</url-pattern>
<http-method>DELETE</http-method>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>tomcat</role-name>
<role-name>role1</role-name>
</auth-constraint>
</security-constraint>
14、登陆验证配置
<login-config>
<auth-method>FORM</auth-method>
<realm-name>Example-Based Authentiation Area</realm-name>
<form-login-config>
<form-login-page>/jsp/security/protected/login.jsp</form-login-page>
<form-error-page>/jsp/security/protected/error.jsp</form-error-page>
</form-login-config>
</login-config>
15、安全角色:security-role元素给出安全角色的一个列表,这些角色将出现在servlet元素内的security-role-ref元素的role-name子元素中。
分别地声明角色可使高级IDE处理安全信息更为容易。
<security-role>
<role-name>tomcat</role-name>
</security-role>
16、Web环境参数:env-entry元素声明Web应用的环境项
<env-entry>
<env-entry-name>minExemptions</env-entry-name>
<env-entry-value>1</env-entry-value>
<env-entry-type>java.lang.Integer</env-entry-type>
</env-entry>
可参考:
http://www.cnblogs.com/JesseV/archive/2009/11/17/1605015.html
tomcat 如何就加载和解析web.xml?
加载顺序为:
context-param->listener->filter->servlet
可以参考一下:
http://www.csdn123.com/html/blogs/20130919/71581.htm
http://blog.csdn.net/insistgogo/article/details/22598001