Tomcat应用配置之web.xml

web.xml简介web.xml是web应用的基础配置文件,但又不是必须的 。web.xml主要用来配置Filter、Listener、Servlet等 。我们常用的框架多数都要通过web.xml文件进行配置后才能引入并使用 。
加载web.xml过程(1)启动一个应用,web容器会读取它的配置文件web.xml,读取<listener>和<context-param>两个结点
(2)创建一个ServletContext,这个web项目的所有部分都将共享这个上下文
(3)容器将<context-param>转换为键值对,并交给ServletContext
(4)容器创建<listener>中的类实例,根据配置的class类路径<listener-class>来创建监听,在监听中会有contextInitialized(ServletContextEvent args)初始化方法,启动Web应用时,系统调用Listener的该方法
(5)容器会读取<filter></filter>,根据指定的类路径来实例化过滤器
(6)以上是应用完全启动起来的时候就已经完成的工作 。如果系统中有Servlet,则Servlet是在第一次发起请求的时候被实例化的,而且一般不会被容器销毁,它可以服务于多个用户的请求 。
(7)总的来说,web.xml的加载顺序是:ServletContext -> context-param -> listener -> filter -> servlet,不会因为元素在文件中的前后顺序而改变 。如果web.xml中出现了相同的元素,则按照在配置文件中出现的先后顺序来加载 。
web.xml文件元素1.web-App部署描述符的根元素是<web-app>,写法如下:
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4"xmlns="http://JAVA.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"></web-app>2.display-name<display-name>test-hwp-web-application</display-name>定义了web应用的名称,可以在http://localhost:8080/manager/html中显示 。如下所示:

Tomcat应用配置之web.xml

文章插图
 
3.welcome-file-list<welcome-file-list><welcome-file>index1.jsp</welcome-file><welcome-file>index2.jsp</welcome-file></welcome-file-list>该标签专门是配置应用首页的,从根目录开始依次查找 。
4.context-param<context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/web-context.xml</param-value></context-param><context-param><param-name>log4jConfigLocation</param-name><param-value>classpath:log4j.properties</param-value></context-param>该标签专门是配置应用范围内的初始化参数 。
5.filter<filter><filter-name>hiddenHttpMethodFilter</filter-name><filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class></filter><filter-mapping><filter-name>hiddenHttpMethodFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>该标签专门配置filter过滤器,filter-class指定使用的过滤器,此处我使用的HiddenHttpMethodFilter过滤器就是将页面中的请求转化为Rest风格的Http请求 。url-pattern主要是过滤的表达式 。
6.servlet<servlet><servlet-name>grhc</servlet-name><servlet-class>com.hlw.ssm.web.servlet.MyDispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/web-context.xml</param-value></init-param><load-on-startup>1</load-on-startup><async-supported>true</async-supported></servlet><servlet-mapping><servlet-name>grhc</servlet-name><url-pattern>/*</url-pattern></servlet-mapping>servlet-name:指定servlet应用的名称 。
init-param:指的是初始化参数,包含参数名和参数值 。
load-on-startup:意思是在容器启动的时候是否就加载servlet,即初始化servlet并且执行init方法 。该值大于0就代表容器启动的时候就加载servlet,而小于0就代表使用servlet时才加载 。
7.listener<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>监听器用于监听HttpSession、ServletRequest等域对象的创建与销毁事件 。此处用得spring监听器ContextLoaderListener目得是在容器启动的时候,自动加载ApplicationContext配置信息 。
8.session-config<session-config><session-timeout>30</session-timeout></session-config>该标签专门配置session失效的时间,也可以通过代码request.getSession.setMaxInactiveInterval来实现的 。
9.error-page<error-page><error-code>404</error-code><location>/error/404</location> </error-page> <error-page><error-code>500</error-code><location>/error/500</location> </error-page><error-page><exception-type>java.lang.Exception</exception-type><location>/error/500</location> </error-page>


推荐阅读