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"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--<servlet>--> <!--<servlet-name>dispatcher</servlet-name>--> <!--<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>--> <!--<init-param>--> <!--<param-name>contextConfigLocation</param-name>--> <!--<param-value>/WEB-INF/dispatcher-servlet2.xml</param-value>--> <!--</init-param>--> <!--<load-on-startup>1</load-on-startup>--> <!--</servlet>--> <!--<servlet-mapping>--> <!--<servlet-name>dispatcher</servlet-name>--> <!--<url-pattern>/</url-pattern>--> <!--</servlet-mapping>--> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>CharacterEncodingFilter</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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
加载顺序:context-param -> listener -> filter -> servlet
Servlet不配置的话,并不会在Web启动的时候加载,而且即使配置了Servlet,没有load-on-startup节点或者配置的load-on-startup节点不是正整数,同样不会在启动的时候生效,而是在第一次访问(无论是否匹配相应的pattern)生效
自定义一个PersonServlet
package com.example.demo.controller; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class PersonServlet extends HttpServlet { private Log logger = LogFactory.getLog(PersonServlet.class); @Override public void init() throws ServletException { logger.info("personServlet被初始化了"); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // super.doGet(req, resp); System.out.println("personServlet被访问了"); } }
<?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"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--<servlet>--> <!--<servlet-name>dispatcher</servlet-name>--> <!--<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>--> <!--<init-param>--> <!--<param-name>contextConfigLocation</param-name>--> <!--<param-value>/WEB-INF/dispatcher-servlet2.xml</param-value>--> <!--</init-param>--> <!--<load-on-startup>1</load-on-startup>--> <!--</servlet>--> <!--<servlet-mapping>--> <!--<servlet-name>dispatcher</servlet-name>--> <!--<url-pattern>/</url-pattern>--> <!--</servlet-mapping>--> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>com.example.demo.controller.PersonServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>CharacterEncodingFilter</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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
与实际所想一致,PersonServlet在Web启动的时候进行了初始化。
本文版权归作者和博客园共有,来源网址:http://www.cnblogs.com/tq1226112215/
欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。