首页 > 代码库 > Servlet Life Cycle

Servlet Life Cycle

Servlet Life Cycle

http://docs.oracle.com/javaee/5/tutorial/doc/bnafi.html

Servlet Filters and Event Listeners

http://docs.oracle.com/cd/B14099_19/web.1012/b14017/filters.htm

 

在web.xml 中出现的先后顺序

① listener

② filter

③ servlet

原因是:

监听器有六种监听,可以监听上下文属性变化,上下文初始化,request初始化(这些先于servlet),还可以监听request的session创建属性变化和销毁等

而过滤器就好比在servlet之前加基层过滤网

技术分享

 

项目实验:

─src └───main     ├───java     │   └───cn     │       └───zno     │           │   TestServlet.java     │           │     │           └───listner     │                   My1ServletContextListener.java     │                   My2ServletContextAttributeListener.java     │                   My3HttpSessionListener.java     │                   My4HttpSessionAttributeListner.java     │                   My5ServletRequestListner.java     │                   My6ServletRequestAttributeListner.java     │     ├───resources     └───webapp         │   index.jsp         │         └───WEB-INF                 web.xml

 

// 添加一些上下文属性
18:09:55,430 INFO [stdout] (ServerService Thread Pool -- 59) 2.118:09:55,436 INFO [stdout] (ServerService Thread Pool -- 59) cn.zno.listner.My2ServletContextAttributeListener@355255d718:09:55,437 INFO [stdout] (ServerService Thread Pool -- 59) org.apache.jasper.Constants.CODE_SOURCE_ATTRIBUTE_NAME18:09:55,438 INFO [stdout] (ServerService Thread Pool -- 59) org.apache.jasper.JSP_TAG_LIBRARIES18:09:55,438 INFO [stdout] (ServerService Thread Pool -- 59) org.apache.tomcat.InstanceManager18:09:55,438 INFO [stdout] (ServerService Thread Pool -- 59) javax.faces.validator.beanValidator.ValidatorFactory18:09:55,439 INFO [stdout] (ServerService Thread Pool -- 59) org.apache.jasper.JSP_PROPERTY_GROUPS18:09:55,439 INFO [stdout] (ServerService Thread Pool -- 59) org.jboss.as.jsf.FACES_ANNOTATIONS18:09:55,439 INFO [stdout] (ServerService Thread Pool -- 59) io.undertow.websockets.jsr.WebSocketDeploymentInfo18:09:55,439 INFO [stdout] (ServerService Thread Pool -- 59) org.apache.jasper.Constants.PERMISSION_COLLECTION_ATTRIBUTE_NAME18:09:55,440 INFO [stdout] (ServerService Thread Pool -- 59) javax.websocket.server.ServerContainer18:09:55,440 INFO [stdout] (ServerService Thread Pool -- 59) com.sun.faces.AnnotatedClasses18:09:55,440 INFO [stdout] (ServerService Thread Pool -- 59) org.apache.jasper.SERVLET_VERSION// 添加上下文属性 18:09:55,443 INFO [stdout] (ServerService Thread Pool -- 59) 2.118:09:55,443 INFO [stdout] (ServerService Thread Pool -- 59) cn.zno.listner.My2ServletContextAttributeListener@355255d718:09:55,443 INFO [stdout] (ServerService Thread Pool -- 59) javax.servlet.context.tempdir// 初始化上下文18:09:55,444 INFO [stdout] (ServerService Thread Pool -- 59) 1.1// 添加上下文属性18:09:55,485 INFO [stdout] (ServerService Thread Pool -- 59) 2.118:09:55,486 INFO [stdout] (ServerService Thread Pool -- 59) cn.zno.listner.My2ServletContextAttributeListener@355255d718:09:55,489 INFO [stdout] (ServerService Thread Pool -- 59) com.sun.faces.CONFIG_MANAGER_KEY// 添加上下文属性18:09:55,513 INFO [stdout] (ServerService Thread Pool -- 59) 2.118:09:55,513 INFO [stdout] (ServerService Thread Pool -- 59) cn.zno.listner.My2ServletContextAttributeListener@355255d718:09:55,513 INFO [stdout] (ServerService Thread Pool -- 59) com.sun.faces.InitFacesContext// 添加上下文属性18:09:55,542 INFO [stdout] (ServerService Thread Pool -- 59) 2.118:09:55,543 INFO [stdout] (ServerService Thread Pool -- 59) cn.zno.listner.My2ServletContextAttributeListener@355255d718:09:55,543 INFO [stdout] (ServerService Thread Pool -- 59) com.sun.faces.patternCache// 添加上下文属性18:09:55,546 INFO [stdout] (ServerService Thread Pool -- 59) 2.118:09:55,547 INFO [stdout] (ServerService Thread Pool -- 59) cn.zno.listner.My2ServletContextAttributeListener@355255d718:09:55,547 INFO [stdout] (ServerService Thread Pool -- 59) com.sun.faces.config.WebConfiguration// 移除某个上下文属性18:09:55,562 INFO [stdout] (ServerService Thread Pool -- 59) 2.218:09:55,562 INFO [stdout] (ServerService Thread Pool -- 59) 2.2// 初始化 request 对象18:10:17,349 INFO [stdout] (default task-2) 5.2// 创建session 对象18:10:21,922 INFO [stdout] (default task-2) 3.3// 添加session 属性18:10:21,923 INFO [stdout] (default task-2) 4.1// 覆盖session 属性18:10:21,924 INFO [stdout] (default task-2) 4.3// 移除 session 属性18:10:21,924 INFO [stdout] (default task-2) 4.2// 添加 request 属性18:10:21,925 INFO [stdout] (default task-2) 6.1// 覆盖request 属性18:10:21,925 INFO [stdout] (default task-2) 6.3// 移除request 属性18:10:21,926 INFO [stdout] (default task-2) 6.2// 销毁request 对象18:10:21,931 INFO [stdout] (default task-2) 5.1// 销毁session(根据timeout)18:10:32,429 INFO [stdout] (default task-3) 3.4

 

具体项目见:

zmvc-listeners

Servlet Life Cycle