Servlet - life cycle and listener events
- 博客分类:
- java
import database.BookDBAO; import javax.servlet.*; import util.Counter; import javax.ejb.*; import javax.persistence.*; public final class ContextListener implements ServletContextListener { private ServletContext context = null; @PersistenceUnit EntityManagerFactory emf; public void contextInitialized(ServletContextEvent event) { context = event.getServletContext(); try { BookDBAO bookDB = new BookDBAO(emf); context.setAttribute("bookDB", bookDB); } catch (Exception ex) { System.out.println( "Couldn’t create database: " + ex.getMessage()); } Counter counter = new Counter(); context.setAttribute("hitCounter", counter); counter = new Counter(); context.setAttribute("orderCounter", counter); } public void contextDestroyed(ServletContextEvent event) { context = event.getServletContext(); BookDBAO bookDB = context.getAttribute("bookDB"); bookDB.remove(); context.removeAttribute("bookDB"); context.removeAttribute("hitCounter"); context.removeAttribute("orderCounter"); } }
Servlet Life Cycle
The life cycle of a servlet is controlled by the container in which the servlet has been deployed. When a request is mapped to a servlet, the container performs the following steps.
-
If an instance of the servlet does not exist, the web container
-
Loads the servlet class.
-
Creates an instance of the servlet class.
-
Initializes the servlet instance by calling the init method. Initialization is covered in Initializing a Servlet.
-
-
Invokes the service method, passing request and response objects. Service methods are discussed in Writing Service Methods.
If the container needs to remove the servlet, it finalizes the servlet by calling the servlet’s destroy method. Finalization is discussed inFinalizing a Servlet.
Handling Servlet Life-Cycle Events
You can monitor and react to events in a servlet’s life cycle by defining listener objects whose methods get invoked when life-cycle events occur. To use these listener objects you must define and specify the listener class.
Defining the Listener Class
You define a listener class as an implementation of a listener interface. Table 4-2 lists the events that can be monitored and the corresponding interface that must be implemented. When a listener method is invoked, it is passed an event that contains information appropriate to the event. For example, the methods in the HttpSessionListener interface are passed an HttpSessionEvent, which contains an HttpSession.
Web context (seeAccessing the Web Context) |
Initialization and destruction |
javax.servlet.ServletContextListener and ServletContextEvent |
Attribute added, removed, or replaced |
javax.servlet.ServletContextAttributeListener and ServletContextAttributeEvent |
|
Session (See Maintaining Client State) |
Creation, invalidation, activation, passivation, and timeout |
javax.servlet.http.HttpSessionListener,javax.servlet.http.HttpSessionActivationListener, and HttpSessionEvent |
Attribute added, removed, or replaced |
javax.servlet.http.HttpSessionAttributeListener and HttpSessionBindingEvent |
|
Request |
A servlet request has started being processed by web components |
javax.servlet.ServletRequestListener and ServletRequestEvent |
Attribute added, removed, or replaced |
javax.servlet.ServletRequestAttributeListener and ServletRequestAttributeEvent
|
发表评论
-
Classloader
2011-11-22 14:09 720The class loader: BootStrap c ... -
JUnit Knowledges
2011-10-25 23:09 712mock objects: http://bigwhite. ... -
Java跨平台程序设计规则
2011-09-20 12:38 922不是说所有的Java程序都具有Java跨平台的特性,事实上,相 ... -
Common Gateway Interface
2011-08-25 15:20 1364A web server that supports ... -
什么时候使用属性或者元素(Designing XML document)
2011-08-19 11:34 981Properties 是平面结构,想要表达结构复杂的信息有一定 ... -
Java - Generic
2011-07-19 12:35 689Generics <!--[if !suppor ... -
Java Exception Study
2011-07-13 16:37 816Java Exception <!--[if ! ... -
面向对象的设计的四个主要原则
2011-07-11 14:22 13141. ISP (Interface Segregation ... -
Design Pattern
2011-07-11 11:52 625Mediator 一、引子 中介在现实 生活中并 ... -
Java Generic, Association, Composition, Aggregate
2011-07-05 16:20 1126UML中依赖泛化关联实现聚合组合的Java实现 ... -
ETL
2011-06-30 11:36 827ETL 对于数据仓库以及ETL的知识,我基本上是个 ... -
Java Collection Frameworks 学习
2011-06-30 11:17 670Java 集合总结 <!--[if !su ... -
Java Clone
2011-06-29 16:07 774Clone interface: 如果一个实现了Cl ... -
Credit Card 的工具类
2010-05-10 15:24 849这是一个非常有用的工具类用于验证credit card, 是a ... -
实例化抽象类时实现抽象类里的抽象方法
2010-04-16 09:18 980EccTrxCmd logWSCall = new EccTr ... -
Java reflect
2010-03-09 16:21 788【转】Java反射经典实例 Java Reflection C ...
相关推荐
servlet--listener 各种接口的简单例子 ,将每个接口的实现都简单的举例,特别容易学会,下载后直接 放到工作空间后运行
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>test</servlet-name> <servlet-class>org.springframework.web.servlet....
Servlet中的八大Listener.docServlet中的八大Listener.docServlet中的八大Listener.docServlet中的八大Listener.docServlet中的八大Listener.docServlet中的八大Listener.docServlet中的八大Listener.docServlet中的...
<listener-class>test.ListenerDemo</listener-class> </listener> <!--定义了Web应用程序中会话的超时时限--> <session-config> <session-timeout>10</session-timeout> </session-config>
ORA-12541 TNSno listener 的解决方案 ORA-12541 TNSno listener 的解决方案
<listener-class> org.apache.axis.transport.http.AxisHTTPSessionListener </listener-class> </listener> <servlet> <servlet-name>AxisServlet</servlet-name> <servlet-class> org.apache....
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class> </listener> <servlet> <servlet-name>Resteasy</servlet-name> <servlet-class>org.jboss.resteasy....
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <servlet> <servlet-name>CXFService</servlet-name> <servlet-class>org.apache.cxf.transport...
making-sense-dependency-injection-test-execution-listener-源码.rar
jmeter-datadog-backend-listener-0.3.1.zip
很多小伙伴在测试过程中发现,测试数据没有写入到influxdb内,那是因为jmeter的backend listener需要一个支持influxdb2.x的jar文件。下载该资源,将其放入jmeter安装目录下,/apache-jmeter-5.5/lib/ext内。有很多...
官方离线安装包,亲测可用
tc-init-health-check-listener.zip,检查服务器的tomcat服务器生命周期侦听器
<listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <filter-name>encodingFilter</filter-name> <filter-class> org.springframework.web....
dockerhub-webhook-listener, 侦听 DockerHub webhook调用 DockerHub Webhook侦听器这只是一个简单的HTTP服务器设置,用来监听和处理 DockerHub webhook调用。简单的Logger 处理程序和一个稍微复杂的Mailgun 处理...
〜700B React Click Away侦听器安装yarn add react-click-away-listener 它的体积很小。 它是用TypeScript构建的。 它支持鼠标和触摸事件。 它可以与Portal(> = v2)一起使用。用法import ClickAwayListener from '...
spring-kafka顺序消费。 kafka顺序消费。 主要是为博客上传完整代码
kktix事件监听器 我的模块 安装 $ npm install kktix-event-listener 用法 var kktixEventListener = require ( 'kktix-event-... kktix-event-listener http://reactjs.kktix.cc/events/react9 Options --duratio
详细的解释了servlet filter listener 额知识,本文档适合初学者阅读