https://www.cnblogs.com/zhai1997/p/11651085.html摘抄
监听器即监视域对象的创建与销毁以及域对象的属性的变化。
监听ServletContext域对象的创建与销毁:实现ServletContextListener 接口
监听HttpSession域对象的创建与销毁:实现HttpSessionListener 接口(在Session被创建的时候监听器被创建)
监听ServletRequest域对象的创建与销毁:实现ServletRequesttListener 接口
监听ServletContext域对象属性:实现ServletContextAttributeListener 接口
监听HttpSession域对象属性:实现HttpSessionAttributeListener 接口
监听ServletRequest域对象属性:实现ServletRequestAttributetListener 接口
两种方式:
1)直接new listener
2)在web.xml中配置
net.pp.FirstServletContextListener
FirstServletContextListener.java
package net.pp;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class FirstServletContextListener implements ServletContextListener{
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("Servletcontext初始化了");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("Servletcontext销毁了");
}
}
FirstHttpSessionListener.java
package net.pp;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionBindingEvent;
@WebListener()
public class FirstHttpSessionListener implements HttpSessionListener{
// Public constructor is required by servlet spec
public FirstHttpSessionListener() {
}
public void sessionCreated(HttpSessionEvent se) {
System.out.println(se.getSession().getId());
}
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("session销毁了!");
}
}
2、监听三大域对象的属性变化
与session中绑定的对象相关。
User.java
package net.pp.bean;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
public class User implements HttpSessionBindingListener{
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("对象被绑定了!");
}
@Override
public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("对象被解除绑定了!");
}
}
TestBindingServlet.java
package net.pp.servlet;
import net.pp.bean.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(name = "TestBindingServlet", urlPatterns = "/testBind")
public class TestBindingServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
User user = new User();
user.setId(1);
user.setName("wen");
//绑定对象
session.setAttribute("user",user);
//解除绑定
session.removeAttribute("user");
}
}