Reading – Java™ Servlet Specification Version 2.4 – Application Lifecycle Events

规范定义了一系列的 event listener 用于监视 ServletContext, HttpSession and ServletRequest 等接口的实例的状态变化.

Event Type Description Listener Interface
Servlet Context Events
Lifecycle The servlet context has just been created and is available to service its first request, or the servlet context is about to be shut down. javax.servlet.
ServletContextListener
Changes to attributes Attributes on the servlet context have been added, removed, or replaced. javax.servlet.
ServletContextAttributeListener
HTTP Session Events
Lifecycle An HttpSession has been created, invalidated, or timed out. javax.servlet.http.
HttpSessionListener
Changes to attributes Attributes have been added, removed, or replaced on an HttpSession. javax.servlet.
HttpSessionAttributeListener
Session migration HttpSession has been activated or passivated. javax.servlet.
HttpSessionActivationListener
Object binding Object has been bound to or unbound from Httpsession javax.servlet.
HttpSessionBindingListener
Servlet Request Events
Lifecycle A servlet request has started being processed by Web components. javax.servlet.
ServletRequestListener
Changes to attributes Attributes have been added, removed, or replaced on a ServletRequest. javax.servlet.
ServletRequestAttributeListener

每个 listener class 必须有一个无参数的 public constructor. listener classes 被打包在 WAR 里, 或者在 WEB-INF/classes, 或者在 WEB-INF/lib 中的 JAR 里.
listener class 在部署描述符声明, 按照在部署描述符中的顺序被调用.

Listener Registration

Web container 在应用程序处理第一个请求之前为每个 listener class 创建一个实例并将其注册用于事件通知. Web container 根据它们所实现的接口和它们在部署描述符中出现的顺序注册这些 listener 实例. 在 Web application 执行期间, listeners 按照它们的注册顺序被调用.

Notifications At Shutdown

当 application shutdown 时, listeners 按照声明它们的相反顺序获得通知, 且 session listeners 先于 context listeners. Session listeners 必须先于 context listener 得到应用程序关闭通知之前被通知 session 失效.

Listener Instances and Threading

在一个 Web application 开始处理第一个请求之前, container 必须将该 Web application 中的 listener classes 完全实例化. container 必须维护每个 listener 引用, 直至为该 Web application 处理最后一个请求.
Attribute changes to ServletContext and HttpSession objects may occur concurrently. The container is not required to synchronize the resulting notifications to attribute listener classes. Listener classes that maintain state are responsible for the integrity of the data and should handle this case explicitly.

Session Events

Listener classes provide the Developer with a way of tracking sessions within a Web application. It is often useful in tracking sessions to know whether a session became invalid because the container timed out the session, or because a Web component within the application called the invalidate method. The distinction may be determined indirectly using listeners and the HttpSession API methods.

By javafuns on January 7, 2009 at 22:30 · Views: 495 · Permalink
Categorized in: Java · Tagged with: ,
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Leave a Reply


  • Highest Rated

  • My PicasaPhotos

    IMG_0604.JPG

    IMG_0550.JPG

    IMG_0592.JPG

  • RSS My del.icio.us

  • My RSS