Servlet高级部分Listener

监听器的使用场景:

  ①:统计在线人数   ②:实现单一登录【一个账号只能在一台机器上登录】

Servlet中的8大监听器:

1.         ServletContextListener

[接口方法] contextInitialized()contextDestroyed()

[接收事件] ServletContextEvent

[触发场景] 在Container加载Web应用程序时(例如启动Container之后),会调用contextInitialized(),而当容器移除Web应用程序时,会调用contextDestroy()方法。

 

2.         ServletContextAttributeListener

[接口方法] attributeAdded()attributeReplaced()attributeRemoved()

[接收事件] ServletContextAttributeEvent

[触发场景] 若有对象加入为applicationServletContext)对象的属性,则会调用attributeAdded(),同理在置换和移除属性时,会分别调用attributeReplaced()attributeRemoved()

 

3.         HttpSessionListener

[接口方法] sessionCreated()sessionDestroyed()

[接收事件] HttpSessionEvent

[触发场景] 在session(HttpSession)对象建立或者消亡时,会分别调用这两个方法。

 

4.         HttpSessionAttributeListener

[接口方法] attributeAdded()attributeReplaced()attributeRemoved()

[接收事件] HttpSessionBindingEvent

[触发场景] 若有对象加入为sessionHttpSession)对象的属性,则会调用attributeAdded(),同理在置换和移除属性时,会分别调用attributeReplaced()attributeRemoved()

 

5.         ServletRequestListener

[接口方法] requestInitialized()requestDestroyed()

[接收事件] RequestEvent

[触发场景] 在request(HttpServletRequest)对象建立或者消亡时,会分别调用这两个方法。

 

6.         ServletRequestAttributeListener

[接口方法] attributeAdded()attributeReplaced()attributeRemoved()

[接收事件] HttpSessionBindingEvent

[触发场景] 若有对象加入为requestHttpServletRequest)对象的属性,则会调用attributeAdded(),同理在置换和移除属性时,会分别调用attributeReplaced()attributeRemoved()

 

7.         HttpSessionBindingListener

[接口方法] valueBound()valueUnbound()

[接收事件] HttpSessionBindingEvent

[触发场景] 实现HttpSessionBindingListener接口的类别,其实例如果被加入到session(HttpSession)对象的属性中,则会调用valueBound(),如果从session(HttpSession)对象的属性中移除,则会调用valueUnbound(),实现HttpSessionBindingListener接口的类别不需要在web.xml中设定。

 

8.         HttpSessionActivationListener

[接口方法] sessionDidActivate()sessionWillPassivate()

[接收事件] HttpSessionEvent

[触发场景] ActivatePassivate是用于置换对象的动作,当session对象为了资源利用或者负载均衡等原因而必须暂时储存至硬盘或其他存储器时(通过对象序列化),所作的动作称为Passivate,而硬盘或者储存器上的session对象重新加载JVM时所采的动作称之为Activate,所以sessionDidActivate()sessionWillPassivate()分别于Activate后和Passivate前调用。

 

除了HttpSessionBindingListenerHttpSessionActivationListener外,必须在web.xml中向容器注册,容器才会在对应的事件发生时调用对应的类别。

代码演示:

TestServlet.java

 1 public class TestServlet extends HttpServlet {
 2 
 3     /**
 4      * 
 5      */
 6     private static final long serialVersionUID = 1L;
 7 
 8     @Override
 9     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
10         System.out.println("执行get请求");
11     }
12 
13     @Override
14     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
15         System.out.println("执行post请求");
16     }
17 }

TestListener.java

 1 public class TestListener implements ServletRequestListener{
 2 
 3     @Override
 4     public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
 5         System.out.println("request销毁");
 6     }
 7 
 8     @Override
 9     public void requestInitialized(ServletRequestEvent servletRequestEvent) {
10         System.out.println("request初始化");
11     }    
12 }

web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 3   <display-name>ListenerDemo</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.html</welcome-file>
 6     <welcome-file>index.htm</welcome-file>
 7     <welcome-file>index.jsp</welcome-file>
 8     <welcome-file>default.html</welcome-file>
 9     <welcome-file>default.htm</welcome-file>
10     <welcome-file>default.jsp</welcome-file>
11   </welcome-file-list>
12   
13   <servlet>
14       <servlet-name>testServlet</servlet-name>
15       <servlet-class>cn.woo.servlet.TestServlet</servlet-class>
16   </servlet>
17   
18   <servlet-mapping>
19       <servlet-name>testServlet</servlet-name>
20       <url-pattern>/servlet/testServlet</url-pattern>
21   </servlet-mapping>
22   
23   <!-- 配置Listener:监听request的初始化和销毁 -->
24   <listener>
25       <listener-class>cn.woo.listener.TestListener</listener-class>
26   </listener>
27   
28 </web-app>

通过:http://localhost:8080/ListenerDemo/servlet/testServlet 访问Servlet

控制台打印:

猜你喜欢

转载自www.cnblogs.com/wooyoohoo/p/9504984.html
今日推荐