本文共 4902 字,大约阅读时间需要 16 分钟。
该接口继承于javax.servlet.http
HttpSessionListener 该接口是监听session对象的创建以及销毁
HttpSessionAttributeListener 则是对session对象属性改变的监听。 案例代码如下 1.0 继承接口类(实现其方法)package 监听器;import javax.servlet.annotation.WebListener;import javax.servlet.http.HttpSessionAttributeListener;import javax.servlet.http.HttpSessionBindingEvent;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;import javax.websocket.Session;public class HttpSession implements HttpSessionListener, HttpSessionAttributeListener { public void sessionCreated(HttpSessionEvent arg0) { System.out.println("创建session"); } public void sessionDestroyed(HttpSessionEvent arg0) { System.out.println("销毁session"); } public void attributeAdded(HttpSessionBindingEvent arg0) { System.out.println("给session添加属性:"); System.out.println("session对象名"+arg0.getName());//输出session对象的属性 System.out.println("session对象值"+arg0.getValue());//输出session对象的值 } public void attributeRemoved(HttpSessionBindingEvent arg0) { System.out.println("删除值"); System.out.println("(删除后)对象名:"+arg0.getName()); System.out.println("session的值(删除后):"+arg0.getSession().getAttribute(arg0.getName())); } public void attributeReplaced(HttpSessionBindingEvent arg0) { System.out.println("session对象名(修改后):"+arg0.getName()); System.out.println("session的值(修改后):"+arg0.getSession().getAttribute(arg0.getName())); }}
2.0 测试的servlet,对session对象的创建以及属性的修改
package 监听器;import java.io.IOException;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;@WebServlet("/testHttpSession")public class testHttpSession extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session=request.getSession(); //对session对象添加值,attributeAdded()方法 session.setAttribute("name", "ysdqh123"); //对session值进行修改,对应的是attributeReplaced()方法 session.setAttribute("name", "ysdqh456"); session.removeAttribute("name"); //删除值 } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
在web.xml配置文件当中添加此段代码,配置监听器代码
监听器2 监听器.HttpSession
ServletContextListener
在使用该接口的时候,需要实现其方法函数名 | 描述 |
---|---|
contextInitialized () | 监听器的创建 |
contextDestroyed() | 监听器的销毁 |
ServletContextAttributeListener
同理,在使用该接口的时候,需要实现其方法函数名 | 函数 |
---|---|
attributeAdded() | 属性添加将调用方法 |
attributeRemoved() | 属性删除将调用此方法 |
attributeReplaced() | 属性更新将调用此方法 |
在此下代码中,俩个接口,以及其共5个方法,皆已实现,代码如下:
package 监听器;import javax.servlet.ServletContextAttributeEvent;import javax.servlet.ServletContextAttributeListener;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import javax.servlet.annotation.WebListener;public class ServletContext implements ServletContextAttributeListener, ServletContextListener { public void attributeAdded(ServletContextAttributeEvent arg0) { System.out.println("属性添加方法"); System.out.println("对象名、即属性名 :"+arg0.getName()); System.out.println("对象的值 :"+arg0.getServletContext().getAttribute(arg0.getName())); } public void attributeRemoved(ServletContextAttributeEvent arg0) { System.out.println("属性删除方法"); System.out.println("(删除后)属性名:"+arg0.getName()); System.out.println("(删除后) 值:"+arg0.getServletContext().getAttribute(arg0.getName())); } public void contextDestroyed(ServletContextEvent arg0) { System.out.println("监听器销毁方法"); } public void attributeReplaced(ServletContextAttributeEvent arg0) { System.out.println("属性更新方法"); System.out.println("(修改后)属性名 :"+arg0.getName()); System.out.println("(修改后)值:"+arg0.getServletContext().getAttribute(arg0.getName())); } public void contextInitialized(ServletContextEvent arg0) { System.out.println("监听器创建方法"); } }
测试的servlet
package 监听器;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@WebServlet("/testcontext")public class testcontext extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.getServletContext().setAttribute("name", "yueyueniao1"); this.getServletContext().setAttribute("name", "yueyueniao2"); this.getServletContext().removeAttribute("name"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
在web.xml配置文件当中添加此段代码
监听器1 监听器.ServletContext
转载地址:http://samzi.baihongyu.com/