于HttpServletRequest 相关的监听有 ServletRequestListener,ServletRequestAttributeListener也就是servletRequest对象生命周期的监听和request对象中属性的添加,修改,移除等属性值变动的监听
通过这request 生命周期的监听我们可以知道 servletRequest 的生命周期,也就是一个请求,当我们请求到达服务器,而服务器响应之后一个request 就结束了。
一、ServletRequestListener
1、ServletRequestListener 是request请求的生命周期的监听
2、 配置方式和之前的一样有两种,第一使用javax.servlet.annotation.WebListener; 第二种配置方式是web.xml 配置:
</session-config>
<listener>
<listener-class>soufun.com.HttpServletRequestL</listener-class>
</listener>
实例: @WebListener()
package soufun.com;
importjavax.servlet.ServletRequest;
importjavax.servlet.ServletRequestEvent;
importjavax.servlet.ServletRequestListener;
importjavax.servlet.annotation.WebListener;
importjavax.servlet.http.HttpServletRequest;
/**
*@author WHD
*data 2015年8月16日
*/
@WebListener()
public classHttpServletRequestL implements ServletRequestListener{
@Override
publicvoid requestDestroyed(ServletRequestEvent arg0) {
ServletRequest res=arg0.getServletRequest();
res.removeAttribute("name");
System.out.println("ServletRequest 销毁");
}
@Override
publicvoid requestInitialized(ServletRequestEvent arg0) {
ServletRequest res=arg0.getServletRequest();
res.setAttribute("name","okrequest");
System.out.println("ServletRequest 初始化");
}
}
实例: web.xml配置
package soufun.com;
importjavax.servlet.ServletRequest;
importjavax.servlet.ServletRequestEvent;
importjavax.servlet.ServletRequestListener;
importjavax.servlet.annotation.WebListener;
importjavax.servlet.http.HttpServletRequest;
/**
*@author WHD
*data 2015年8月16日
*/
public classHttpServletRequestL implements ServletRequestListener{
@Override
publicvoid requestDestroyed(ServletRequestEvent arg0) {
ServletRequest res=arg0.getServletRequest();
res.removeAttribute("name");
System.out.println("ServletRequest 销毁");
}
@Override
publicvoid requestInitialized(ServletRequestEvent arg0) {
ServletRequest res=arg0.getServletRequest();
res.setAttribute("name","okrequest");
System.out.println("ServletRequest 初始化");
}
}
二、ServletRequestAttributeListener
1、当request中的属性有变化的时候会调用相应的实现类的方法
2、配置方式同上有两:@WebListener() 和web.xml 配置
3、实例
(1)、@WebListener()
package soufun.com;
importjavax.servlet.ServletRequestAttributeEvent;
importjavax.servlet.ServletRequestAttributeListener;
importjavax.servlet.annotation.WebListener;
/**
*@author WHD
*data 2015年8月16日
*/
@WebListener()
public classHttpServletRequestAl implements ServletRequestAttributeListener{
@Override
publicvoid attributeAdded(ServletRequestAttributeEvent arg0) {
//TODO Auto-generated method stub
System.out.println("request 中添加属性的时候调用即 request.setAttribute(key,value)");
}
@Override
publicvoid attributeRemoved(ServletRequestAttributeEvent arg0) {
//TODO Auto-generated method stub
System.out.println("request 中移除 属性的时候调用即 request.removeAttribute(key)");
}
@Override
publicvoid attributeReplaced(ServletRequestAttributeEvent arg0) {
//TODO Auto-generated method stub
System.out.println("request 中替换属性的时候调用即 request.setAttribute(key,value)");
}
}
(2)、web.xml 配置
package soufun.com;
importjavax.servlet.ServletRequestAttributeEvent;
importjavax.servlet.ServletRequestAttributeListener;
importjavax.servlet.annotation.WebListener;
/**
*@author WHD
*data 2015年8月16日
*/
public classHttpServletRequestAl implements ServletRequestAttributeListener{
@Override
publicvoid attributeAdded(ServletRequestAttributeEvent arg0) {
//TODO Auto-generated method stub
System.out.println("request 中添加属性的时候调用即 request.setAttribute(key,value)");
}
@Override
publicvoid attributeRemoved(ServletRequestAttributeEvent arg0) {
//TODO Auto-generated method stub
System.out.println("request 中移除 属性的时候调用即 request.removeAttribute(key)");
}
@Override
publicvoid attributeReplaced(ServletRequestAttributeEvent arg0) {
//TODO Auto-generated method stub
System.out.println("request 中替换属性的时候调用即 request.setAttribute(key,value)");
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。