????????ServletRequestListener接口定义了ServletRequest(是HttpServletRequest接口的父接口类型)对象生命周期的监听行为。
void requestInitialized(ServletRequestEvent sre)
??????? HttpServletRequest对象创建后会触发该监听器方法,并将已创建HttpServletRequest对象传递到该方法中
void requestDestoryed(HttpSessionEvent sre)
??????? HttpServletRequest对象在销毁之前会触发该监听方法,并将要销毁的HttpServletRequest对象传递到该方法中
实现:
package cn.itcast.listener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;
public class HttpServletRequestLifecycleListener implements ServletRequestListener {
@Override
public void requestDestroyed(ServletRequestEvent sre) {
System.out.println("-------------Start requestDestroyed---------------");
System.out.println((HttpServletRequest)sre.getServletRequest());
System.out.println("-------------End requestDestroyed---------------");
}
@Override
public void requestInitialized(ServletRequestEvent sre) {
System.out.println("-------------Start requestInitialized---------------");
System.out.println((HttpServletRequest)sre.getServletRequest());
System.out.println("-------------End requestInitialized---------------");
}
}
ServletRequestAttributeListener接口定义了对于HttpServletRequest对象属性操作的监听行为。
void attributeAdded(ServletRequestAttributeEvent srae)
????????向HttpServletRequest对象中添加属性是会触发该监听方法,并将HttpServletRequest对象传递到该方法中,触发事件的方法HttpServletRequest.setAttribute("key","value")
void attributeRemove(ServletRequestAttributeEvent srae)
????????HttpServletRequest对象中添加属性是会触发该监听方法,并将HttpServletRequest对象传递到该方法中,触发事件的方法HttpServletRequest.removeAttribute("key","value")
void attributeReplaced(ServletRequestAttributeEvent srae)
????????HttpServletRequest对象中添加属性是会触发该监听方法,并将HttpServletRequest对象传递到该方法中,触发事件的方法HttpServletRequest.setAttribute("key","value")
?实例代码:
servlet:
package cn.itcast.listener;
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 java.io.IOException;
@WebServlet("/requestAttr.do")
public class HttpServletRequestAttrServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("key","test");
req.setAttribute("key","test2");
req.removeAttribute("key");
}
}
监听:
package cn.itcast.listener;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
public class HttpServletRequestAttrListener implements ServletRequestAttributeListener {
@Override
public void attributeRemoved(ServletRequestAttributeEvent srae) {
System.out.println("-------HttpServletRequestAttrListener---Start attributeRemoved----------");
System.out.println("Name:"+srae.getName()+" Value:"+srae.getValue());
System.out.println("-------HttpServletRequestAttrListener---End attributeRemoved----------");
}
@Override
public void attributeReplaced(ServletRequestAttributeEvent srae) {
System.out.println("-------HttpServletRequestAttrListener---Start attributeReplaced----------");
System.out.println("Name:"+srae.getName()+" Value:"+srae.getValue());
System.out.println("-------HttpServletRequestAttrListener---End attributeReplaced----------");
}
@Override
public void attributeAdded(ServletRequestAttributeEvent srae) {
System.out.println("-------HttpServletRequestAttrListener---Start attributeAdded----------");
System.out.println("Name:"+srae.getName()+" Value:"+srae.getValue());
System.out.println("-------HttpServletRequestAttrListener---End attributeAdded----------");
}
}
输出:
-------HttpServletRequestAttrListener---Start attributeAdded----------
Name:key Value: test
-------HttpServletRequestAttrListener---End attributeAdded----------
HttpServletRequestAttrListener---Start attributeReplaced----------
Name:key Value: test
-------HttpServletRequestAttrListener---End attributeReplaced----------
HttpServletRequestAttrListener---Start attributeRemoved----------
Name:key Value: test2
-------HttpServletRequestAttrListener---End attributeRemoved----------