12、Servlet监听器
来源:互联网 发布:淘宝茶叶散装也需要qs 编辑:程序博客网 时间:2024/05/16 15:04
Listener是Servlet的监听器,它可以监听客户端的请求、服务端的操作等。通过监听器,可以自动激发一些操作,比如监听在线用户的数量。
当增加一个HttpSession时,就激发sessionCreated(HttpSessionEvent se)方法,这样就可以给在线人数加1.
1、Servlet常用的监听接口:
-ServletContextAttributeListener监听对ServletContext属性的操作,比如增加、删除、修改属性:attributeAdded(),attributeRemoved()、attributeReplaced()
-ServletContextListener监听ServletContext,当创建ServletContext时,激发contextInitialized(ServletContextEvent sce)方法;当销毁ServletContext时,激发contextDestroyed(ServletContextEvent sce)方法
2、一个监听器实例:监听ServletContext的创建与销毁
import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;public class MyServletContextListener implements ServletContextListener{public void contextDestroyed(ServletContextEvent sce){System.out.println("destroyed" + sce.getServletContext());}public void contextInitialized(ServletContextEvent sce){System.out.println("initialized " + sce.getServletContext());}}
监听器同Servlet和Filter一样,也需要在Web.xml中进行部署,上面的监听器部署如下:
<listener> <listener-class>com.cdtax.listener.MyServletContextListener</listener-class> </listener>
一个监听器:对ServletContext的属性进行监听:
import javax.servlet.ServletContextAttributeEvent;import javax.servlet.ServletContextAttributeListener;public class MyServletContextAttributeListener implementsServletContextAttributeListener{public void attributeAdded(ServletContextAttributeEvent scab){System.out.println("attribute added");System.out.println(scab.getName() + ":" + scab.getValue());}public void attributeRemoved(ServletContextAttributeEvent scab){System.out.println("attribute removed");System.out.println(scab.getName() + ":" + scab.getValue());}public void attributeReplaced(ServletContextAttributeEvent scab){System.out.println("attribute replace");System.out.println(scab.getName() + ":" + scab.getValue());}}
web.xml中部署:
<listener> <listener-class>com.cdtax.listener.MyServletContextAttributeListener</listener-class> </listener>
使用jsp页面改变ServletContext属性进行试验:
<body> <% application.setAttribute("testaa","aaaaaa"); %> <% application.setAttribute("testaa","bbbbbb"); %> <% application.setAttribute("testaa","cccccc"); %> <% application.setAttribute("testaa","dddddd"); %> </body>
<body> <% application.removeAttribute("testaa"); %> </body>
3、HttpSessionListener监听HttpSession的操作。当创建一个Session时,激发sessionCreated(HttpSessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed(HttpSessionEvent se)方法。
HttpSessionAttributeListener监听HttpSession中的属性的操作。当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se)方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se)方法。
监听session创建:
import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;public class MySessionListener implements HttpSessionListener{public void sessionCreated(HttpSessionEvent se){System.out.println("session created");System.out.println(se.getSession());}public void sessionDestroyed(HttpSessionEvent se){System.out.println("session destroyed");System.out.println(se.getSession());}}
<listener> <listener-class>com.cdtax.listener.MySessionListener</listener-class> </listener>
监听session属性:
import javax.servlet.http.HttpSessionAttributeListener;import javax.servlet.http.HttpSessionBindingEvent;public class MySessionAttributeListener implements HttpSessionAttributeListener{public void attributeAdded(HttpSessionBindingEvent se){System.out.println("attribute added");System.out.println(se.getName() + ":" + se.getValue());}public void attributeRemoved(HttpSessionBindingEvent se){System.out.println("attribute removed");System.out.println(se.getName() + ":" + se.getValue());}public void attributeReplaced(HttpSessionBindingEvent se){System.out.println("attribute replaced");System.out.println(se.getName() + ":" + se.getValue());}}
web.xml中部署:
<listener> <listener-class>com.cdtax.listener.MySessionAttributeListener</listener-class> </listener>
测试jsp
<body> <% session.setAttribute("aa","bb"); session.setAttribute("aa","cc"); session.setAttribute("aa","dd"); session.removeAttribute("aa"); %> </body>
- 12、Servlet监听器
- Servlet监听器
- servlet监听器
- Servlet监听器
- Servlet监听器
- servlet 监听器
- servlet监听器
- Servlet监听器
- servlet监听器
- Servlet监听器
- Servlet监听器
- Servlet监听器
- Servlet监听器
- Servlet监听器
- Servlet监听器
- servlet监听器
- servlet监听器
- servlet监听器
- java clone方法使用详解
- hdu2154(跳舞毯)
- 构建自己的不可替代性 肖舸
- 认识创建型模式---工厂方法模式
- light oj 1231-1232 - 1233- Coin Change 背包
- 12、Servlet监听器
- fedora 10 下LaTeX-CJK和中文字体的安装 2/2
- 编程之美2.10 寻找数组中的最大值和最小值
- Android 数据库打包发布
- SQL Server2008修改默认端口方法及修改后远程连接
- find grep 的时候过滤某个目录
- ubuntu server自动校正时间
- 长方体类
- hdu1022火车(模似栈)