Servlet常用的监听器
ServletContextAttributeListener
用来感知ServlerContext
对象属性变化,比如添加或删除属性变化
ServletContextListener
用来感知ServlerContext
对象的创建和销毁的
ServletRequestListener
可以用来监听感知ServletRequest
对象的创建和销毁的
ServletRequestAttributeListener
用来感知ServletRequest
属性变化,如何添加或删除属性还是替换
HttpSessionListener
监听session的变化,常用于统计在线人数
HttpSessionAttributeListener
用来监听Session属性变化
HttpSessionBindingListener
用来监听把一个数据绑定到Session对象 感知监听器
HttpSessionActivationListener
用来监听session绑定的对象钝化(把对象持久化磁盘)
常用的有前6个
package com.sparrow.listener;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* @Author: 诉衷情の麻雀
* @Description: 当一个类实现了ServletContextListener 是一个监听器,该类可以监听的事件
* 由该类实现的监听接口决定 该类可以监听ServletContext的创建和销毁
* 当web应用启动时,就会产生ServletContextEvent事件,会调用监听器的对应事件处理方法contextInitialized,同时会传递事件对象
* 我们可以通过ServletContextEvent事件对象,来获取需要的信息,然后再进行业务处理
* 需要在web.xml配置监听器这样才能使tomcat知道,底层使用反射机制
* @DateTime: 2023/7/5 9:57
**/
public class SparrowServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext servletContext = sce.getServletContext();
System.out.println("SparrowServletContextListener监听到" + servletContext+ " 被创建...");
//如果我们获取到ServletContext对象进行业务处理
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
ServletContext servletContext = sce.getServletContext();
System.out.println("SparrowServletContextListener监听到" + servletContext+ " 被销毁...");
//比如可以对servletContext数据进行处理 或者日志的管理
System.out.println("进行处理");
}
}
/**
* @Author: 诉衷情の麻雀
* @Description: TODO
* @DateTime: 2023/7/5 10:10
**/
public class SparrowServletContextAttributeListener implements ServletContextAttributeListener {
@Override
public void attributeAdded(ServletContextAttributeEvent scae) {
System.out.println("SparrowServletContextAttributeListener监听到"+ scae.getName());
}
@Override
public void attributeRemoved(ServletContextAttributeEvent scae) {
System.out.println("SparrowServletContextAttributeListener监听到:" + scae.getValue());
}
@Override
public void attributeReplaced(ServletContextAttributeEvent scae) {
}
}
ServletContextAttributeListener属性监听器
package com.sparrow.listener;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
/**
* @Author: 诉衷情の麻雀
* @Description: TODO
* @DateTime: 2023/7/5 10:10
**/
public class SparrowServletContextAttributeListener implements ServletContextAttributeListener {
@Override
public void attributeAdded(ServletContextAttributeEvent scae) {
System.out.println("SparrowServletContextAttributeListener监听到添加属性:"+ scae.getName()+"=" + scae.getValue());
}
@Override
public void attributeRemoved(ServletContextAttributeEvent scae) {
System.out.println("SparrowServletContextAttributeListener监听到删除属性:" + scae.getName() + "="+ scae.getValue());
}
@Override
public void attributeReplaced(ServletContextAttributeEvent scae) {
System.out.println("SparrowServletContextAttributeListener监听到修改/替换属性:" + scae.getName() + "="+ scae.getValue());
}
}
配置一个HelloServlet
/**
* @Author: 诉衷情の麻雀
* @Description: TODO
* @DateTime: 2023/7/18 19:59
**/
public class HelloServlet 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 {
System.out.println("helloServlet被调用了");
ServletContext servletContext = req.getServletContext();
servletContext.setAttribute("name", "诉衷情の麻雀");
servletContext.setAttribute("name", "诉衷情の麻雀");
servletContext.removeAttribute("name");
}
}
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.sparrow.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/helloServlet</url-pattern>
</servlet-mapping>
HttpSessionListener
/**
* @Author: 诉衷情の麻雀
* @Description: TODO
* @DateTime: 2023/7/5 10:25
**/
public class SparrowHttpSessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
HttpSession session = se.getSession();
//当session创建时设置一个生命周期
session.setMaxInactiveInterval(30);
System.out.println("SparrowHttpSessionListener监听到session创建= " + session.getId());
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
HttpSession session = se.getSession();
System.out.println("SparrowHttpSessionListener监听到session销毁= " + session.getId());
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取session
HttpSession session = req.getSession(); //如果没有就创建
}
配置监听器
<!--配置监听器-->
<listener>
<listener-class>com.sparrow.listener.SparrowHttpSessionListener</listener-class>
</listener>
<listener>
<listener-class>com.sparrow.listener.SparrowServletContextListener</listener-class>
</listener>
<listener>
<listener-class>com.sparrow.listener.SparrowServletContextAttributeListener</listener-class>
</listener>
<listener>
<listener-class>com.sparrow.listener.SparrowRequestListener</listener-class>
</listener>
HttpSessionAttributeListener
package com.sparrow.listener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
/**
* @Author: 诉衷情の麻雀
* @Description: TODO
* @DateTime: 2023/7/18 20:26
**/
public class SparrowHttpSessionAttributeListener implements HttpSessionAttributeListener {
@Override
public void attributeAdded(HttpSessionBindingEvent se) {
System.out.println("SparrowHttpSessionAttributeListener 监听到session添加属性了" + se.getName() + "=" + se.getValue() );
}
@Override
public void attributeRemoved(HttpSessionBindingEvent se) {
System.out.println("SparrowHttpSessionAttributeListener 监听到session删除属性了" + se.getName());
}
@Override
public void attributeReplaced(HttpSessionBindingEvent se) {
System.out.println("SparrowHttpSessionAttributeListener 监听到session修改属性了" + se.getName() + "=" + se.getValue() );
}
}
ServletRequestListener监听器
作用:监听Request对象的创建和销毁
常用于监控某个IP访问网站频率/用户访问的页面、资源(日志)
public class SparrowRequestListener implements ServletRequestListener {
@Override
public void requestInitialized(ServletRequestEvent sre) {
System.out.println("监听到request对象创建");
ServletRequest servletRequest = sre.getServletRequest();
System.out.println("访问IP=" + servletRequest.getRemoteAddr());
System.out.println("访问的资源=" + ((HttpServletRequest) servletRequest).getRequestURL());
}
@Override
public void requestDestroyed(ServletRequestEvent sre) {
System.out.println("监听到request对象被销毁");
}
}
ServletRequestAttributeListener监听器
作用:监听Request属性变化
public class SparrowRequestAttributeListener implements ServletRequestAttributeListener {
@Override
public void attributeAdded(ServletRequestAttributeEvent srae) {
}
@Override
public void attributeRemoved(ServletRequestAttributeEvent srae) {
ServletRequestAttributeListener.super.attributeRemoved(srae);
}
@Override
public void attributeReplaced(ServletRequestAttributeEvent srae) {
ServletRequestAttributeListener.super.attributeReplaced(srae);
}
}