优先级顺序:
监听器>过滤器>servlet
web.xml配置监听器信息:
<listener> <listener-class>com.itheima.attribute.MyServletContextAttributeListener</listener-class> </listener>
session配置信息
<session-config> <session-timeout>30</session-timeout> <cookie-config> <name>JSESSIONID</name> <domain>example.org</domain> <path>/shop</path> <comment> <!-- some information --> </comment> <http-only>true</http-only> <secure>false</secure> <max-age>1800</max-age> </cookie-config> <tracking-mode>COOKIE</tracking-mode> <tracking-mode>URL</tracking-mode> <tracking-mode>SSL</tracking-mode> </session-config>
web监听器可以监听客户端的请求和服务端的操作。
监听对象:
1.ServletContext(application:上下文对象,一个web应用对应一个application)
2.session:对应会话,关闭web应用和浏览器时候清空session,session超时
3.request
作用:监听监听对象的创建和销毁,以及监听对象属性的变化发生修改的事件,在监听前和监听后进行一些处理
操作域属性的监听器(3个)
当对域属性进行增、删、改时,执行的监听器一共有三个:
ServletContextAttributeListener:在ServletContext域进行增、删、改属性时调用下面方法。
public void attributeAdded(ServletContextAttributeEvent evt)
public void attributeRemoved(ServletContextAttributeEvent evt)
public void attributeReplaced(ServletContextAttributeEvent evt)
HttpSessionAttributeListener:在HttpSession域进行增、删、改属性时调用下面方法
public void attributeAdded(HttpSessionBindingEvent evt)
public void attributeRemoved (HttpSessionBindingEvent evt)
public void attributeReplaced (HttpSessionBindingEvent evt)
ServletRequestAttributeListener:在ServletRequest域进行增、删、改属性时调用下面方法
public void attributeAdded(ServletRequestAttributeEvent evt)
public void attributeRemoved (ServletRequestAttributeEvent evt)
public void attributeReplaced (ServletRequestAttributeEvent evt)
操作属性监听器参数:事件对象功能进行介绍
ServletContextAttributeEvent
String getName():获取当前操作的属性名;
Object getValue():获取当前操作的属性值;
ServletContext getServletContext():获取ServletContext对象。
HttpSessionBindingEvent
String getName():获取当前操作的属性名;
Object getValue():获取当前操作的属性值;
HttpSession getSession():获取当前操作的session对象。
ServletRequestAttributeEvent
String getName():获取当前操作的属性名;
Object getValue():获取当前操作的属性值;
ServletRequest getServletRequest():获取当前操作的ServletRequest对象。
ServletContext getServletContext():获取ServletContext对象;