一、HttpServletRequest对象的生命周期监听器
ServletRequestListener接口定义了ServletRequest(是HttpServletRequest接口的父接口类型)对象生命周期的监听行为。
void requestInitialized(ServletRequestEvent sre)
HttpServletRequest对象创建后会触发该监听器方法,并将已创建HttpServletRequest对象传递到该方法中
void requestDestoryed(HttpSessionEvent sre)
HttpServletRequest对象在销毁之前会触发该监听方法,并将要销毁的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---------------");
}
}
三、创建能够监听 HttpServletRequest对象的属性操作监听器
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文章来源:https://www.toymoban.com/news/detail-816568.html
-------HttpServletRequestAttrListener---End attributeRemoved----------文章来源地址https://www.toymoban.com/news/detail-816568.html
到了这里,关于Listener监听器----HttpServletRequest对象的生命周期监听器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!