Servlet监听器

 

监听器

监听器就是一个实现特定接口的普通java程序,这个程序专门用于监听另一个java对象的方法调用或属性改变,当被监听对象发生上述事件后,监听器某个方法将立即被执行。

Servle监听器

在Servlet规范中定义了多种类型的监听器,它们用于监听的事件源分别为 ServletContext, HttpSession 和 ServletRequest 这三个域对象。

Servlet规范针对这三个对象上的操作,又把这多种类型的监听器划分为三种类型。
监听三个域对象创建和销毁的事件监听器
监听域对象中属性的增加和删除的事件监听器
监听绑定到 HttpSession 域中的某个对象的状态的事件监听器。

监听servletContext域对象创建和销毁

ServletContextListener 接口用于监听 ServletContext 对象的创建和销毁事件。
当 ServletContext 对象被创建时,激发contextInitialized (ServletContextEvent sce)方法
当 ServletContext 对象被销毁时,激发contextDestroyed(ServletContextEvent sce)方法。

servletContext域对象创建:服务器启动针对每一个web应用创建servletcontext。销毁:服务器关闭前先关闭代表每一个web应用的servletContext。

监听HttpSession域对象创建和销毁

HttpSessionListener接口用于监听HttpSession的创建和销毁
创建一个Session时,sessionCreated(HttpSessionEvent se) 方法将会被调用。
销毁一个Session时,sessionDestroyed (HttpSessionEvent se) 方法将会被调用。

Session域对象创建和销毁的时机创建:用户每一次访问时,服务器创建session。销毁:如果用户的session 30分钟没有使用,服务器就会销毁session,我们在web.xml里面也可以配置session失效时间。

监听HttpRequest域对象创建和销毁
ServletRequestListener 接口用于监听ServletRequest 对象的创建和销毁。
Request 对象被创建时,监听器的requestInitialized方法将会被调用。
Request对象被销毁时,监听器的requestDestroyed方法将会被调用。

servletRequest域对象创建和销毁的时机:
创建:用户每一次访问,都会创建一个reqeust。
销毁:当前访问结束,request对象就会销毁。
监听三个域对象属性变化

Servlet规范定义了监听 ServletContext, HttpSession, HttpServletRequest 这三个对象中的属性变更信息事件的监听器。
这三个监听器接口分别是ServletContextAttributeListener, HttpSessionAttributeListener ServletRequestAttributeListener
这三个接口中都定义了三个方法来处理被监听对象中的属性的增加,删除和替换的事件,同一个事件在这三个接口中对应的方法名称完全相同,只是接受的参数类型不同。

attributeAdded 方法

当向被监听器对象中增加一个属性时,web容器就调用事件监听器的 attributeAdded 方法进行相应,这个方法接受一个事件类型的参数,监听器可以通过这个参数来获得正在增加属性的域对象和被保存到域中的属性对象

各个域属性监听器中的完整语法定义为:
public void attributeAdded(ServletContextAttributeEvent scae)
public void attributeReplaced(HttpSessionBindingEvent hsbe)
public void attributeRmoved(ServletRequestAttributeEvent srae)
attributeRemoved 方法

当删除被监听对象中的一个属性时,web 容器调用事件监听器的这个方法进行相应
各个域属性监听器中的完整语法定义为:
public void attributeRemoved(ServletContextAttributeEvent scae)
public void attributeRemoved (HttpSessionBindingEvent hsbe)
public void attributeRemoved (ServletRequestAttributeEvent srae)

attributeReplaced 方法

当监听器的域对象中的某个属性被替换时,web容器调用事件监听器的这个方法进行相应
各个域属性监听器中的完整语法定义为:
public void attributeReplaced(ServletContextAttributeEvent scae)
public void attributeReplaced (HttpSessionBindingEvent hsbe)
public void attributeReplaced (ServletRequestAttributeEvent srae)

感知 Session 绑定的事件监听器:保存在 Session 域中的对象可以有多种状态:绑定到 Session 中;从 Session 域中解除绑定;随 Session 对象持久化到一个存储设备中;随 Session 对象从一个存储设备中恢复。 Servlet 规范中定义了两个特殊的监听器接口来帮助 JavaBean 对象了解自己在 Session 域中的这些状态:HttpSessionBindingListener接口和HttpSessionActivationListener接口 ,实现这两个接口的类不需要 web.xml 文件中进行注册。

HttpSessionBindingListener接口

实现了HttpSessionBindingListener接口的 JavaBean 对象可以感知自己被绑定到 Session 中和从 Session 中删除的事件
当对象被绑定到 HttpSession 对象中时,web 服务器调用该对象的 void valueBound(HttpSessionBindingEvent event) 方法
当对象从 HttpSession 对象中解除绑定时,web 服务器调用该对象的 void valueUnbound(HttpSessionBindingEvent event)方法

HttpSessionActivationListener接口

实现了HttpSessionActivationListener接口的 JavaBean 对象可以感知自己被活化和钝化的事件
当绑定到 HttpSession 对象中的对象将要随 HttpSession 对象被钝化之前,web 服务器调用如下方法sessionWillPassivate(HttpSessionBindingEvent event) 方法
当绑定到 HttpSession 对象中的对象将要随 HttpSession 对象被活化之后,web 服务器调用该对象的 void sessionDidActive(HttpSessionBindingEvent event)方法

显示登陆用户列表,并实现踢人功能:

用户:login.jsp à loginServlet à index.jsp

管理员: listUser.jsp à kickUserServlet

监听器:SessionAttributeListener

Bean: User

cn.class.domain.User

package cn.class3g.domain;

public class User {

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 password) {

this.password = password;

}

}

login.jsp

<form method="post" action="${pageContext.request.contextPath }/servlet/LoginServlet">

用户名:<input type="text" name="username" /><br/>

密码: <input type="password" name="password" /><br/>

<input type="submit" value="登陆" /><br/>

</form>

cn.class.web.servlet.loginServlet.java

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String username = request.getParameter("username");

String password = request.getParameter("password");

User user = new User();

user.setUsername(username);

user.setPassword(password);

request.getSession().setAttribute("user", user);

response.sendRedirect(request.getContextPath()+"/index.jsp");

}

index.jsp

欢迎您:${user.username }

cn.class.web.listener.SessionAttributeListener

//监听sessionuser对象的加入

public class SessionAttributeListener implements HttpSessionAttributeListener {

public void attributeAdded(HttpSessionBindingEvent arg0) {

Object obj = arg0.getValue();

if(obj instanceof User){

HttpSession session = arg0.getSession();

Map map = (Map) session.getServletContext().getAttribute("map");

if(map == null){

map = new HashMap();

session.getServletContext().setAttribute("map", map);

}

User user = (User) obj;

map.put(user.getUsername(), session);

}

}

public void attributeRemoved(HttpSessionBindingEvent arg0) {}

public void attributeReplaced(HttpSessionBindingEvent arg0) {}

}

listUser.jsp

当前登陆用户有:<br/>

<c:forEach var="me" items="${map }" >

${me.key } <a href="${pageContext.request.contextPath }

/servlet/kickUserServlet?username=${me.key}" >踢死你</a>

</c:forEach>

cn.class.web.servlet.kickUserServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String username = request.getParameter("username");

Map map = (Map) this.getServletContext().getAttribute("map");

HttpSession session = (HttpSession) map.get(username);

if(session != null){

session.invalidate();

map.remove(username);

}

request.getRequestDispatcher("/listUser.jsp").forward(request, response);

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值