1. web.xml配置详解
<web-app>
<!--指定WEB应用的名字-->
<display-name>MyWeb</display-name>
<!--WEB应用描述信息-->
<description>MyWeb demo</description>
<!--web的初始化参数,通过ServletContextEvent.getServletContext().getInitParameter("field")获得value的值(ServletContextEvent通过listener获得)-->
<context-param>
<param-name>contextConfigLocation</para-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
<description>web的ApplicationContext上下问文件配置</description>
</context-param>
<!--filter 和filter-mapping 必须是成对出现-->
<!--拦截/*的路径,执行CharacterEncodingFilter的父类的OncePerRequestFilter的doFilter(doFilter中调用doFilterInternal方法,设置request和response的编码方式设置为UTF-8)-->
<filter>
<filter-name>utf8-encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>utf8-encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--servlet 监听配置,项目启动时执行contextInitialized(ServletContextEvent servletContextEvent)方法,项目停止时执行contextDestroyed(ServletContextEvent event)-->
<listener>
<listerner-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--servlet和servlet-mapping是成对出现的,服务启动时,执行HttpServlet.init(ServletConfig config)方法,当用户请求/myservlet/*路径时,执行HttpServlet.service(HttpServletRequest req, HttpServletResponse resp)方法-->
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>javax.servlet.http.HttpServlet</servlet-class>
<init-param>
<param-name>paramField</param-name>
<param-value>paramValue</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/myservlet/*</url-pattern>
</servlet-mapping>
<!--会话超时时间设置,单位是分钟-->
<session-config>
<session-timeout>10</session-timeout>
</session-config>
<!--mime-mapping元素将mime类型映射到扩展名, 用于规定下载格式-->
<mime-mapping>
<extension>htm</extension>
<mime-type>text/html</mime-type>
</mime-mapping>
<mime-mapping>
<extension>pdf</extension>
<mime-type>application/pdf</mime-type>
</mime-mapping>
<mime-mapping>
<extension>doc</extension>
<mime-type>application/msword</mime-type>
</mime-mapping>
<mime-mapping>
<extension>xls</extension>
<mime-type>application/msexcel</mime-type>
</mime-mapping>
<!--指定Web项目的欢迎页面-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!--当请求发生404错误时,跳转到404error.jsp页面-->
<error-page>
<error-code>404</error-code>
<location>/404error.jsp</location>
</error-page>
<!--当Web服务发生java.lang.NullException异常时,跳转到nullerror.jsp页面-->
<error-page>
<exception-type>java.lang.NullException</exception-type>
<location>/nullerror.jsp</location>
</error-page>
<!--对tag库文件名称。前端JSP可以通过<%@ taglib uri="http://jakarta.apache.org/tomcat/debug-taglib" prefix="myTag"%>配置使用tag库-->
<taglib>
<taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>
<taglib-location>/WEB-INF/tld/taglib.tld</taglib-location>
</taglib>
<!--配置资源相关的管理对象,可通过new InitialContext().lookup()获得值-->
<resource-env-ref>
<resource-env-ref-name>jms/StockQueue</resource-env-ref-name>
</resource-env-ref>
<!--设置factory的外部资源-->
<resource-ref>
<description>java JDBC DataSource factory</description>
<res-ref-name>jdbc/java_db</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>dataManager</res-auth>
</resource-ref>
<security-constraint></security-constraint>
<login-config></login-config>
<security-role></security-role>
<env-entry></env-entry>
</web-app>
2. web.xml加载过程:
1. Web项目启动的时候,容器(如:tomcat)读取webapp/WEB-INF/web.xml文件,读取<context-param>和<listener>;
2. 创建ServletContex,Web项目所有部分都可以使用该上下文ServletContex;
3. 容器将<context-param></context-param>解析为key-value对,并交给ServletContext;
4. 容器根据<listener></listener>中的类创建监听实例,即启动监听;
5. listener监听类中会contextInitialized(ServletContextEvent servletContextEvent)初始化方法,可通过ServletContextEvent.getServletContext().getInitParameter("field")获得value的值;
6. 解析<filters></filters>,并启动拦截器 拦截器开始起作用,当有请求进入时,执行Filter的doFilter方法;
7. 最后加载和初始化配置在load on startup的servlets;
8. 加载Spring,如果filter需要用到bean,但加载顺序是: 先加载filter 后加载spring,则filter中初始化操作中的bean为null.
如果过滤器中要使用到 bean,可以将spring 的加载 改成 Listener的方式 :
<listener>
<listerner-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>