当对域属性进行增、删、改时,执行的监听器一共有三个:
l ServletContextAttributeListener:在ServletContext域进行增、删、改属性时调用下面方法。
- public void attributeAdded(ServletContextAttributeEvent evt)
- public void attributeRemoved(ServletContextAttributeEvent evt)
- public void attributeReplaced(ServletContextAttributeEvent evt)
l HttpSessionAttributeListener:在HttpSession域进行增、删、改属性时调用下面方法
- public void attributeAdded(HttpSessionBindingEvent evt)
- public void attributeRemoved (HttpSessionBindingEvent evt)
- public void attributeReplaced (HttpSessionBindingEvent evt)
l ServletRequestAttributeListener:在ServletRequest域进行增、删、改属性时调用下面方法
- public void attributeAdded(ServletRequestAttributeEvent evt)
- public void attributeRemoved (ServletRequestAttributeEvent evt)
- public void attributeReplaced (ServletRequestAttributeEvent evt)
下面对这三个监听器的事件对象功能进行介绍:
l ServletContextAttributeEvent
- String getName():获取当前操作的属性名;
- Object getValue():获取当前操作的属性值;
- ServletContext getServletContext():获取ServletContext对象。
l HttpSessionBindingEvent
- String getName():获取当前操作的属性名;
- Object getValue():获取当前操作的属性值;
- HttpSession getSession():获取当前操作的session对象。
l ServletRequestAttributeEvent
- String getName():获取当前操作的属性名;
- Object getValue():获取当前操作的属性值;
- ServletContext getServletContext():获取ServletContext对象;
- ServletRequest getServletRequest():获取当前操作的ServletRequest对象。
public class MyListener implements ServletContextAttributeListener, ServletRequestAttributeListener, HttpSessionAttributeListener { public void attributeAdded(HttpSessionBindingEvent evt) { System.out.println("向session中添加属性:" + evt.getName() + "=" + evt.getValue()); }
public void attributeRemoved(HttpSessionBindingEvent evt) { System.out.println("从session中移除属性:" + evt.getName() + "=" + evt.getValue()); }
public void attributeReplaced(HttpSessionBindingEvent evt) { System.out.println("修改session中的属性:" + evt.getName() + "=" + evt.getValue()); }
public void attributeAdded(ServletRequestAttributeEvent evt) { System.out.println("向request中添加属性:" + evt.getName() + "=" + evt.getValue()); }
public void attributeRemoved(ServletRequestAttributeEvent evt) { System.out.println("从request中移除属性:" + evt.getName() + "=" + evt.getValue()); }
public void attributeReplaced(ServletRequestAttributeEvent evt) { System.out.println("修改request中的属性:" + evt.getName() + "=" + evt.getValue()); }
public void attributeAdded(ServletContextAttributeEvent evt) { System.out.println("向context中添加属性:" + evt.getName() + "=" + evt.getValue()); }
public void attributeRemoved(ServletContextAttributeEvent evt) { System.out.println("从context中移除属性:" + evt.getName() + "=" + evt.getValue()); }
public void attributeReplaced(ServletContextAttributeEvent evt) { System.out.println("修改context中的属性:" + evt.getName() + "=" + evt.getValue()); } } |
public class ListenerServlet extends BaseServlet { public String contextOperation(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context = this.getServletContext(); context.setAttribute("a", "a"); context.setAttribute("a", "A"); context.removeAttribute("a"); return "/index.jsp"; }
///////////////////////////////
public String sessionOperation(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); session.setAttribute("a", "a"); session.setAttribute("a", "A"); session.removeAttribute("a"); return "/index.jsp"; }
///////////////////////////////
public String requestOperation(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setAttribute("a", "a"); request.setAttribute("a", "A"); request.removeAttribute("a"); return "/index.jsp"; } } |
<body> <a href="<c:url value='/ListenerServlet?method=contextOperation'/>">SevletContext操作属性</a> <br/> <a href="<c:url value='/ListenerServlet?method=sessionOperation'/>">HttpSession操作属性</a> <br/> <a href="<c:url value='/ListenerServlet?method=requestOperation'/>">ServletRequest操作属性</a> | </body> |