Listener监听器----HttpServletRequest对象的生命周期监听器

发布时间:2024年01月08日
一、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

-------HttpServletRequestAttrListener---End attributeRemoved----------

文章来源:https://blog.csdn.net/weixin_45889291/article/details/135457656
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。