Servlet Life Cycle

Posted zno2

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.1 18:09:55,436 INFO [stdout] (ServerService Thread Pool -- 59) [email protected] 18:09:55,437 INFO [stdout] (ServerService Thread Pool -- 59) org.apache.jasper.Constants.CODE_SOURCE_ATTRIBUTE_NAME 18:09:55,438 INFO [stdout] (ServerService Thread Pool -- 59) org.apache.jasper.JSP_TAG_LIBRARIES 18:09:55,438 INFO [stdout] (ServerService Thread Pool -- 59) org.apache.tomcat.InstanceManager 18:09:55,438 INFO [stdout] (ServerService Thread Pool -- 59) javax.faces.validator.beanValidator.ValidatorFactory 18:09:55,439 INFO [stdout] (ServerService Thread Pool -- 59) org.apache.jasper.JSP_PROPERTY_GROUPS 18:09:55,439 INFO [stdout] (ServerService Thread Pool -- 59) org.jboss.as.jsf.FACES_ANNOTATIONS 18:09:55,439 INFO [stdout] (ServerService Thread Pool -- 59) io.undertow.websockets.jsr.WebSocketDeploymentInfo 18:09:55,439 INFO [stdout] (ServerService Thread Pool -- 59) org.apache.jasper.Constants.PERMISSION_COLLECTION_ATTRIBUTE_NAME 18:09:55,440 INFO [stdout] (ServerService Thread Pool -- 59) javax.websocket.server.ServerContainer 18:09:55,440 INFO [stdout] (ServerService Thread Pool -- 59) com.sun.faces.AnnotatedClasses 18: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.1 18:09:55,443 INFO [stdout] (ServerService Thread Pool -- 59) [email protected] 18: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.1 18:09:55,486 INFO [stdout] (ServerService Thread Pool -- 59) [email protected] 18: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.1 18:09:55,513 INFO [stdout] (ServerService Thread Pool -- 59) [email protected] 18:09:55,513 INFO [stdout] (ServerService Thread Pool -- 59) com.sun.faces.InitFacesContext // 添加上下文属性 18:09:55,542 INFO [stdout] (ServerService Thread Pool -- 59) 2.1 18:09:55,543 INFO [stdout] (ServerService Thread Pool -- 59) [email protected] 18:09:55,543 INFO [stdout] (ServerService Thread Pool -- 59) com.sun.faces.patternCache // 添加上下文属性 18:09:55,546 INFO [stdout] (ServerService Thread Pool -- 59) 2.1 18:09:55,547 INFO [stdout] (ServerService Thread Pool -- 59) [email protected] 18: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.2 18: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的主要内容,如果未能解决你的问题,请参考以下文章

life cycle与四个事件

优维低代码:Brick Life Cycle 构件生命周期

JavaWeb之Servlet

Maven - Maven Build Life Cycle - maven tutorial

ASP.NET Application Life Cycle

Life Cycle Stages of IIS 7.0