美文网首页
Listener、Filter、Servlet执行顺序

Listener、Filter、Servlet执行顺序

作者: jiangmo | 来源:发表于2017-11-30 22:25 被阅读51次

    结构图

    Listener:

    首先定义一个Listener,实现以下接口:
    HttpSessionListener(用来监控session的创建,销毁等)
    ServletRequestListener(用于监控servlet上下文request)
    ServletRequestAttributeListener(用于监控request中的attribute的操作)

    public class TestListener implements HttpSessionListener,ServletRequestListener,ServletRequestAttributeListener {  
        private Logger logger = LoggerFactory.getLogger(TestListener.class);  
          
        //sessionListener start!  
        public void sessionCreated(HttpSessionEvent arg0) {  
            logger.info(".......TestListener sessionCreated().......");  
        }  
      
        public void sessionDestroyed(HttpSessionEvent arg0) {  
            logger.info(".......TestListener sessionDestroyed().......");  
        }  
        //sessionListener end!  
          
        //requestListener start!  
        public void requestInitialized(ServletRequestEvent arg0) {  
            logger.info("......TestListener requestInitialized()......");  
        }  
      
        public void requestDestroyed(ServletRequestEvent arg0) {  
            logger.info("......TestListener requestDestroyed()......");  
        }  
        //requestListener end!  
          
        //attributeListener start!  
        public void attributeAdded(ServletRequestAttributeEvent srae) {  
            logger.info("......TestListener attributeAdded()......");  
        }  
      
        public void attributeRemoved(ServletRequestAttributeEvent srae) {  
            logger.info("......TestListener attributeRemoved()......");  
        }  
      
        public void attributeReplaced(ServletRequestAttributeEvent srae) {  
            logger.info("......TestListener attributeReplaced()......");  
        }  
        //attributeListener end!  
    } 
    

    Filter:

    public class TestFilter implements Filter {  
        private Logger logger = LoggerFactory.getLogger(TestFilter.class);  
      
        public void destroy() {  
            logger.info("..............execute TestFilter destory()..............");  
        }  
      
        public void doFilter(ServletRequest arg0, ServletResponse arg1,FilterChain arg2) throws IOException, ServletException {  
            logger.info("..............execute TestFilter doFilter()..............");  
            arg2.doFilter(arg0, arg1);  
        }  
      
        public void init(FilterConfig arg0) throws ServletException {  
            logger.info("..............execute TestFilter  init()..............");  
        }  
    }  
    

    Servlet

    public class TestServlet extends HttpServlet {  
      
        private Logger logger = LoggerFactory.getLogger(TestServlet.class);  
      
        /** 
         *  
         */  
        private static final long serialVersionUID = -4263672728918819141L;  
      
        @Override  
        public void init() throws ServletException {  
            logger.info("...TestServlet init() init..........");  
            super.init();  
        }  
          
        @Override  
        public void destroy() {  
            logger.info("...TestServlet init() destory..........");  
            super.destroy();  
        }  
        @Override  
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
                throws ServletException, IOException {  
            this.doPost(req, resp);  
        }  
          
        @Override  
        protected void doPost(HttpServletRequest request, HttpServletResponse response)  
                throws ServletException, IOException {  
            logger.info("...TestServlet doPost() start..........");  
            //操作attribute  
            request.setAttribute("a", "a");  
            request.setAttribute("a", "b");  
            request.getAttribute("a");  
            request.removeAttribute("a");  
            //操作session  
            request.getSession().setAttribute("a", "a");  
            request.getSession().getAttribute("a");  
            request.getSession().invalidate();  
            logger.info("...TestServlet doPost() end..........");  
        }  
    }  
    

    配置XML

    <!-- 测试filter -->  
    <filter>  
        <filter-name>TestFilter</filter-name>  
        <filter-class>com.xy.web.filter.TestFilter</filter-class>  
    </filter>  
    <filter-mapping>  
        <filter-name>TestFilter</filter-name>  
        <url-pattern>*.do</url-pattern>  
    </filter-mapping>  
    <!-- 测试servlet -->  
    <servlet>  
        <servlet-name>TestServlet</servlet-name>  
        <servlet-class>com.xy.web.servlet.TestServlet</servlet-class>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>TestServlet</servlet-name>  
        <url-pattern>/*</url-pattern>  
    </servlet-mapping>  
    <!-- 测试listener -->  
    <listener>  
        <listener-class>com.xy.web.listener.TestListener</listener-class>  
    </listener>  
    

    配置好以后启动项目。

    加载顺序

    从启动日志来看,
    启动的顺序为listener->Filter->servlet.
    简单记为:理(Listener)发(Filter)师(servlet).
    执行的顺序不会因为三个标签在配置文件中的先后顺序而改变。

    生命周期

    第一次访问:
    2016-01-14 00:03:03,991 INFO TestListener:26 - ......TestListener requestInitialized()......
    2016-01-14 00:03:04,001 INFO TestServlet:24 - ...TestServlet init() init..........
    2016-01-14 00:03:04,011 INFO TestFilter:23 - ..............execute TestFilter doFilter()..............
    2016-01-14 00:03:15,275 INFO TestServlet:42 - ...TestServlet doPost() start..........
    2016-01-14 00:03:16,255 INFO TestListener:36 - ......TestListener attributeAdded()......
    2016-01-14 00:03:16,853 INFO TestListener:44 - ......TestListener attributeReplaced()......
    2016-01-14 00:03:18,561 INFO TestListener:40 - ......TestListener attributeRemoved()......
    2016-01-14 00:03:20,065 INFO TestListener:16 - .......TestListener sessionCreated().......
    2016-01-14 00:03:22,908 INFO TestListener:20 - .......TestListener sessionDestroyed().......
    2016-01-14 00:03:25,624 INFO TestServlet:52 - ...TestServlet doPost() end..........
    2016-01-14 00:03:27,746 INFO TestListener:30 - ......TestListener requestDestroyed()......

    第二次访问:
    2016-01-14 00:04:08,908 INFO TestListener:26 - ......TestListener requestInitialized()......
    2016-01-14 00:04:08,909 INFO TestFilter:23 - ..............execute TestFilter doFilter()..............
    2016-01-14 00:04:14,385 INFO TestServlet:42 - ...TestServlet doPost() start..........
    2016-01-14 00:04:14,778 INFO TestListener:36 - ......TestListener attributeAdded()......
    2016-01-14 00:04:14,974 INFO TestListener:44 - ......TestListener attributeReplaced()......
    2016-01-14 00:04:15,342 INFO TestListener:40 - ......TestListener attributeRemoved()......
    2016-01-14 00:04:15,904 INFO TestListener:16 - .......TestListener sessionCreated().......
    2016-01-14 00:04:17,354 INFO TestListener:20 - .......TestListener sessionDestroyed().......
    2016-01-14 00:04:17,815 INFO TestServlet:52 - ...TestServlet doPost() end..........
    2016-01-14 00:04:19,044 INFO TestListener:30 - ......TestListener requestDestroyed()......

    相关文章

      网友评论

          本文标题:Listener、Filter、Servlet执行顺序

          本文链接:https://www.haomeiwen.com/subject/jkzpbxtx.html