1.作用:监听Session创建或销毁,即生命周期监听
2.相关方法:
//void sessionCreated(HttpSessionEvent se):创建session时调用
//void sessionDestroyed(HttpSessionEvent se):销毁sessio时调用
3.使用场景:
和前面的ServletContextListener等一样,可以用于监控用户上线和离线
4.代码
HttpSessionListener监听器
package com.listener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class hspHttpSessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
HttpSession session = se.getSession();
//当session被创建时,设置一个统一的生命周期
session.setMaxInactiveInterval(30);
System.out.println("hspHttpSessionListener 监听到session创建="+
session.getId());
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
HttpSession session = se.getSession();
System.out.println("hspHttpSessionListener 监听到session销毁="+
session.getId());
}
}
实现类文章来源:https://www.toymoban.com/news/detail-757185.html
package com.servlet;
import javax.servlet.ServletContext;
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 = "HiServlet",urlPatterns = "/HiServlet")
public class HiServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取session对象
HttpSession session = request.getSession();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
HttpSessionAttributeListener监听器
1.作用:监听Session属性的变化,使用少
2.相关方法:
//void attributeAdded(ServletRequestAttritubeEvent srae):添加属性时
//void attributeReplaced(ServletRequestAttritubeEvent srae):替换属性时
//void attributeRemoved(ServletRequestAttritubeEvent srae):移除属性时
3.代码
监听器
package com.listener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
public class hspHttpServletAttributeListener implements HttpSessionAttributeListener {
@Override
public void attributeAdded(HttpSessionBindingEvent se) {
System.out.println("hspHttpServletAttributeListener 监听到session添加属性="+
se.getName()+"="+se.getValue());
}
@Override
public void attributeRemoved(HttpSessionBindingEvent se) {
System.out.println("hspHttpServletAttributeListener 监听到session删除属性="+
se.getName());
}
@Override
public void attributeReplaced(HttpSessionBindingEvent se) {
System.out.println("hspHttpServletAttributeListener 监听到session修改属性="+
se.getName()+"="+se.getValue());
}
}
实现类文章来源地址https://www.toymoban.com/news/detail-757185.html
package com.servlet;
import javax.servlet.ServletContext;
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 = "HiServlet",urlPatterns = "/HiServlet")
public class HiServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取session对象
HttpSession session = request.getSession();
//添加数据
session.setAttribute("age",100);
session.setAttribute("age",40);
session.removeAttribute("age");
System.out.println("HiServlet操作完毕...");
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
到了这里,关于HttpSessionListener监听器和HttpSessionAttributeListener监听器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!