Listener-监听器
在javaweb中,listener监听的事件源分为ServletContecxt,HttpSessoin,ServletRequest三个级别
ServletContext级别
Listener | 场景 |
---|---|
ServletContextListener | 响应ServletContext生命周期事件(创建/销毁在ServletContext创建或销毁时调用对应的方法) |
ServletContextAttributeListener | 响应ServletContext属性的添加/删除/替换事件 |
HttpSession级别
Listener | 场景 |
---|---|
HttpSessionListener | 响应HttpSession生命周期事件(创建/销毁) |
HttpSessionAttributeListener | 响应HttpSession属性的添加/删除/替换事件 |
HttpSessionBindingListener | 实现该接口的Java Bean会在被Session添加/删除时作出响应 |
HttpSessionActivationListener | 实现该接口的JavaBean会在被Session钝化/激活时作出响应 |
Servletrequest级别
Listener | 场景 |
---|---|
ServletrequestListener | 响应ServletRequest生命周期事件(创建/销毁) |
ServletRequestAttributeListerner | 响应ServletRequest属性添加/删除/替换事件 |
注册
创建监听器只需要实现相应的接口,但我们还需要将其注册到Servlet容器当中,只有这样才能在事件发生时监听器作出响应。监听器的注册方式有注解和部署描述符两种
@WebListener
在servlet3.0当中提供了@WebListener注解
“`java
@WebListener
public class ListenerClass implements ServletContextListener {
// ...
}
“`
部署描述符
<listener>
<listener-class>com.fq.web.listener.ListenerClass</listener-class>
</listener>
因为HttpSessionBindingListener和HttpSessionActivationListener是直接绑定到JavaBean当中而非绑定到seesion等域对象中,因此可以不用注册
示例
HttpSessionListener
@WebListener
public class HttpSessionListenerImpl implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
//监听HttpSesssion对象创建事件
//获取被创建的session
HttpSession session = httpSessionEvent.getSession();
}
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
//监听HttpSesssion对象销毁事件
//获取将要被销毁的Session
HttpSession session = httpSessionEvent.getSession();
}
}
````
<div class="se-preview-section-delimiter"></div>
#### HttpSessionAttributeListener
<div class="se-preview-section-delimiter"></div>
```java
@WebListener
public class HttpSessionAttributeImpl implements HttpSessionAttributeListener {
@Override
public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
//响应HttpSession对象添加事件
//获取HttpSession对象
HttpSession session = httpSessionBindingEvent.getSession();
//添加的属性名称
String name = httpSessionBindingEvent.getName();
//添加的属性值
Object value = httpSessionBindingEvent.getValue();
System.out.println("add attribute|"+name+":"+ value);
}
@Override
public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
//响应HttpSession对象删除事件
//获取HttpSession对象
HttpSession session = httpSessionBindingEvent.getSession();
//添加的属性名称
String name = httpSessionBindingEvent.getName();
//添加的属性值
Object value = httpSessionBindingEvent.getValue();
System.out.println("remove attribute|"+name+":"+ value);
}
@Override
public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
//响应HttpSession对象替换事件
//获取HttpSession对象
HttpSession session = httpSessionBindingEvent.getSession();
//添加的属性名称
String name = httpSessionBindingEvent.getName();
//添加的属性值
Object value = httpSessionBindingEvent.getValue();
System.out.println("replace attribute|"+name+":"+ value);
}
}
@WebServlet(urlPatterns = "/session")
public class SessionServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//get session
HttpSession session = req.getSession();
//添加属性
session.setAttribute("msg","add attribute");
//替换属性值
session.setAttribute("msg","msg has already replaced");
//删除属性msg
session.removeAttribute("msg");
}
}
HttpSessionBindingListener
public class Person implements Serializable,HttpSessionBindingListener {
private int id;
private String name;
private double weight;
public Person(int id,String name,double weight){
this.id = id;
this.name = name;
this.weight = weight;
}
/**
* 响应对象被添加到session事件
*/
@Override
public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("我被添加到sesssion中了");
}
/**
* 响应对象被session删除事件
* @param httpSessionBindingEvent
*/
@Override
public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("我被session删除了");
}
}
@WebServlet(urlPatterns = "/sessionBinding")
public class SessionBindingServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Person person = new Person(1,"luffy",50.1);
HttpSession session = req.getSession();
session.setAttribute("person",person);
session.removeAttribute("person");
}
}