JSP的Web监听器(Listener),java入门书籍下载

}

}

然后在web.xml中配置该监听器,在web-app中添加:

com.test.MyListener

在JSP中添加访问人数:

在线人数:<%=session.getAttribute(“userNumber”) %>

当我使用我的浏览器访问时,执行结果如下:

在这里插入图片描述

当打开另一个浏览器访问时:

在这里插入图片描述

监听器的分类

====================================================================

  1. 按照监听的对象划分:

按照监听对象的不同可以划分为三种:

ServletContext监控:对应监控application内置对象的创建和销毁。

当web容器开启时,执行contextInitialized方法;当容器关闭或重启时,执行contextDestroyed方法。

实现方式:直接实现ServletContextListener接口:

public class MyServletContextListener implements ServletContextListener{

public void contextDestroyed(ServletContextEvent sce) {

}

public void contextInitialized(ServletContextEvent sce) {

}

}

HttpSession监控:对应监控session内置对象的创建和销毁。

当打开一个新的页面时,开启一个session会话,执行sessionCreated方法;当页面关闭session过期时,或者容器关闭销毁时,执行sessionDestroyed方法。

实现方式:直接实现HttpSessionListener接口:

public class MyHttpSessionListener implements HttpSessionListener{

public void sessionCreated(HttpSessionEvent arg0) {

}

public void sessionDestroyed(HttpSessionEvent arg0) {

}

}

ServletRequest监控:对应监控request内置对象的创建和销毁。

当访问某个页面时,出发一个request请求,执行requestInitialized方法;当页面关闭时,执行requestDestroyed方法。

实现方式,直接实现ServletRequestListener接口:

public class MyServletRequestListener implements ServletRequestListener{

public void requestDestroyed(ServletRequestEvent arg0) {

}

public void requestInitialized(ServletRequestEvent arg0) {

}

}

  1. 按照监听事件划分:

监听事件自身的创建和销毁:同上面的按对象划分。

监听属性的新增、删除和修改也是划分成三种,分别针对于ServletContext、HttpSession、ServletRequest对象:

ServletContext,实现ServletContextAttributeListener接口:

通过调用ServletContextAttribtueEvent的getName方法可以得到属性的名称。

public class MyServletContextAttrListener implements ServletContextAttributeListener{

public void attributeAdded(ServletContextAttributeEvent hsbe) {

System.out.println("In servletContext added :name = "+hsbe.getName());

}

public void attributeRemoved(ServletContextAttributeEvent hsbe) {

System.out.println("In servletContext removed :name = "+hsbe.getName());

}

public void attributeReplaced(ServletContextAttributeEvent hsbe) {

System.out.println("In servletContext replaced :name = "+hsbe.getName());

}

}

HttpSession,实现HttpSessionAttributeListener接口:

public class MyHttpSessionAttrListener implements HttpSessionAttributeListener{

public void attributeAdded(HttpSessionBindingEvent hsbe) {

System.out.println("In httpsession added:name = "+hsbe.getName());

}

public void attributeRemoved(HttpSessionBindingEvent hsbe) {

System.out.println("In httpsession removed:name = "+hsbe.getName());

}

public void attributeReplaced(HttpSessionBindingEvent hsbe) {

System.out.println("In httpsession replaced:name = "+hsbe.getName());

}

}

ServletRequest,实现ServletRequestAttributeListener接口:

public class MyServletRequestAttrListener implements ServletRequestAttributeListener{

public void attributeAdded(ServletRequestAttributeEvent hsbe) {

System.out.println("In servletrequest added :name = "+hsbe.getName());

}

public void attributeRemoved(ServletRequestAttributeEvent hsbe) {

System.out.println("In servletrequest removed :name = "+hsbe.getName());

}

public void attributeReplaced(ServletRequestAttributeEvent hsbe) {

System.out.println("In servletrequest replaced :name = "+hsbe.getName());

}

}

  1. 监听对象的状态:

针对某些POJO类,可以通过实现HttpSessionBindingListener接口,监听POJO类对象的事件。例如:

public class User implements HttpSessionBindingListener,Serializable{

private String username;

private String password;

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String passwor

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

d) {

this.password = password;

}

public void valueBound(HttpSessionBindingEvent hsbe) {

System.out.println("valueBound name: "+hsbe.getName());

}

public void valueUnbound(HttpSessionBindingEvent hsbe) {

System.out.println("valueUnbound name: "+hsbe.getName());

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值