Servlet、Filter 和 Listener 调用顺序、生命周期的实验分析

xiaoxiao2021-03-01  16

通过实验来验证Servlet、Filter和Listener的调用顺序、生命周期。 1、示例项目 目录结构: web.xml <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"><web-app> <listener> <listener-class>edu.shao.webapp.sample.listener.MyServletRequestListener</listener-class> </listener> <listener> <listener-class>edu.shao.webapp.sample.listener.MyServletContextListener</listener-class> </listener> <filter> <filter-name>ResponseFilter</filter-name> <filter-class>edu.shao.webapp.sample.filter.ResponseFilter</filter-class> </filter> <filter-mapping> <filter-name>ResponseFilter</filter-name> <servlet-name>hello_world</servlet-name> </filter-mapping> <filter> <filter-name>LogFilter</filter-name> <filter-class>edu.shao.webapp.sample.filter.LogFilter</filter-class> </filter> <filter-mapping> <filter-name>LogFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>hello_world</servlet-name> <servlet-class>edu.shao.webapp.sample.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello_world</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <servlet> <servlet-name>early_servlet</servlet-name> <servlet-class>edu.shao.webapp.sample.EarlyServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>early_servlet</servlet-name> <url-pattern>/early</url-pattern> </servlet-mapping></web-app> MyServletContextListener类: public class MyServletContextListener implements ServletContextListener { public static Logger logger=LogManager.getLogger(MyServletContextListener.class); @Override public void contextInitialized(ServletContextEvent sce) { logger.info("Servlet context initialized."); } @Override public void contextDestroyed(ServletContextEvent sce) { logger.info("Servlet context destroyed."); }} MyServletRequestListener类: public class MyServletRequestListener implements ServletRequestListener { public static Logger logger=LogManager.getLogger(MyServletRequestListener.class); @Override public void requestDestroyed(ServletRequestEvent sre) { logger.info("Servlet request destroyed."); } @Override public void requestInitialized(ServletRequestEvent sre) { logger.info("Servlet request initialized."); }} ResponseFilter类: public class ResponseFilter implements Filter { public static Logger logger=LogManager.getLogger(ResponseFilter.class); @Override public void init(FilterConfig arg0) throws ServletException { logger.info("Response Filter initialized."); } @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { chain.doFilter(req, res); res.getWriter().write("--@2013 Mr.Shao"); } @Override public void destroy() { logger.info("Response Filter destroyed."); }} LogFilter类: public class LogFilter implements Filter { public static Logger logger=LogManager.getLogger(LogFilter.class); @Override public void init(FilterConfig filterConfig) throws ServletException { logger.info("Log Filter initialized."); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpServletRequest = (HttpServletRequest)request; logger.info("Intercept Url="+httpServletRequest.getRequestURI()); long start=System.currentTimeMillis(); chain.doFilter(request, response); long end=System.currentTimeMillis(); logger.info(request.getRemoteAddr()+": time used :"+(end-start)); } @Override public void destroy() { logger.info("Log Filter destroyed."); }} HelloServlet类: public class HelloServlet extends HttpServlet { public static Logger logger=LogManager.getLogger(HelloServlet.class); @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { logger.debug("Do Get Method."); resp.getWriter().print("Hello World"); } public void init() throws ServletException { logger.debug("Hello Servlet initialized."); } public void destroy() { logger.debug("Hello Servlet destroyed."); }} EarlyServlet类: public class EarlyServlet extends HttpServlet { public static Logger logger=LogManager.getLogger(EarlyServlet.class); @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { logger.debug("Do Get Method."); resp.getWriter().print("This message is created in EarlyServlet"); } public void init() throws ServletException { logger.debug("Early Servlet initialized."); } public void destroy() { logger.debug("Early Servlet destroyed."); }} 2、测试操作 1、启动tomcat服务 2、访问http://10.130.29.198:8080/test/ (访问index.html) 3、访问http://10.130.29.198:8080/test/hello 4、访问http://10.130.29.198:8080/test/early 5、停止tomcat服务 3、Log输出 (根据输出的时间,人为的把日志分割开来,便于观察) 1、启动tomcat服务 2013-03-28 18:14:08,954 INFO - [MyServletContextListener - contextInitialized] Servlet context initialized. 2013-03-28 18:14:08,983 INFO - [LogFilter - init] Log Filter initialized. 2013-03-28 18:14:08,984 INFO - [ResponseFilter - init] Response Filter initialized. 2013-03-28 18:14:08,991 DEBUG - [EarlyServlet - init] Early Servlet initialized. 2、访问http://10.130.29.198:8080/test/ (访问index.html) 2013-03-28 18:14:18,946 INFO - [MyServletRequestListener - requestInitialized] Servlet request initialized. 2013-03-28 18:14:18,957 INFO - [LogFilter - doFilter] Intercept Url=/test/ 2013-03-28 18:14:18,962 INFO - [LogFilter - doFilter] 10.130.29.198: time used :4 2013-03-28 18:14:18,963 INFO - [MyServletRequestListener - requestDestroyed] Servlet request destroyed. 3、访问http://10.130.29.198:8080/test/hello 2013-03-28 18:14:34,713 INFO - [MyServletRequestListener - requestInitialized] Servlet request initialized. 2013-03-28 18:14:34,714 DEBUG - [HelloServlet - init] Hello Servlet initialized. 2013-03-28 18:14:34,716 INFO - [LogFilter - doFilter] Intercept Url=/test/hello 2013-03-28 18:14:34,717 DEBUG - [HelloServlet - doGet] Do Get Method. 2013-03-28 18:14:34,720 INFO - [LogFilter - doFilter] 10.130.29.198: time used :3 2013-03-28 18:14:34,721 INFO - [MyServletRequestListener - requestDestroyed] Servlet request destroyed. 4、访问http://10.130.29.198:8080/test/early 2013-03-28 18:14:42,124 INFO - [MyServletRequestListener - requestInitialized] Servlet request initialized. 2013-03-28 18:14:42,127 INFO - [LogFilter - doFilter] Intercept Url=/test/early 2013-03-28 18:14:42,128 DEBUG - [EarlyServlet - doGet] Do Get Method. 2013-03-28 18:14:42,129 INFO - [LogFilter - doFilter] 10.130.29.198: time used :1 2013-03-28 18:14:42,130 INFO - [MyServletRequestListener - requestDestroyed] Servlet request destroyed. 5、停止tomcat服务 2013-03-28 18:14:49,723 DEBUG - [EarlyServlet - destroy] Early Servlet destroyed. 2013-03-28 18:14:49,724 DEBUG - [HelloServlet - destroy] Hello Servlet destroyed. 2013-03-28 18:14:49,724 INFO - [LogFilter - destroy] Log Filter destroyed. 2013-03-28 18:14:49,726 INFO - [ResponseFilter - destroy] Response Filter destroyed. 2013-03-28 18:14:49,727 INFO - [MyServletContextListener - contextDestroyed] Servlet context destroyed. 4、分析 1、在Tomcat(Servlet容器)启动时,Listener和 ServletContext 最先初始化。因为最先输出的日志是:   2013-03-28 18:14:08,954 INFO - [MyServletContextListener - contextInitialized] Servlet context initialized. 2、Filter在Tomcat(Servlet容器)启动时初始化。   2013-03-28 18:14:08,983 INFO - [LogFilter - init] Log Filter initialized.   2013-03-28 18:14:08,984 INFO - [ResponseFilter - init] Response Filter initialized. 3、如果某个Servlet配置了 <load-on-startup >1 </load-on-startup >,该Servlet也是在Tomcat(Servlet容器)启动时初始化。例如 EarlyServlet:   2013-03-28 18:14:08,991 DEBUG - [EarlyServlet - init] Early Servlet initialized. 4、每次请求, Request都会被初始化,响应请求后,请求被销毁:   2013-03-28 18:14:18,946 INFO - [MyServletRequestListener - requestInitialized] Servlet request initialized.   ......   2013-03-28 18:14:18,963 INFO - [MyServletRequestListener - requestDestroyed] Servlet request destroyed. 5、Filter在请求执行前后,都能做一些处理。   (1)请求执行前:2013-03-28 18:14:34,716 INFO - [LogFilter - doFilter] Intercept Url=/test/hello   (2)执行请求:2013-03-28 18:14:34,717 DEBUG - [HelloServlet - doGet] Do Get Method.   (3)请求执行后:2013-03-28 18:14:34,720 INFO - [LogFilter - doFilter] 10.130.29.198: time used :3 6、如果Servlet没有配置<load-on-startup >1 </load-on-startup >,该Servlet不会在Tomcat启动时初始化,而是在请求到来时初始化。例如 HelloServlet:   2013-03-28 18:14:34,714 DEBUG - [HelloServlet - init] Hello Servlet initialized. 7、Servlet初始化后,将不会随着请求的结束而注销。HelloServlet 初始化,当请求结束了,HelloServlet 的 destroy() 没有被调用。 8、关闭Tomcat时,Servlet、Filter、Listener依次被注销。
转载请注明原文地址: https://www.6miu.com/read-4050013.html

最新回复(0)