案例:监听域对象的属性变更

创建TestAttributeServlet用于测试

@WebServlet(name = "TestAttributeServlet", value = "/TestAttributeServlet")
public class TestAttributeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write("<h3>这是一个测试域对象属性变更事件监听器的页面</h3>");
        getServletContext().setAttribute("username", "itcast");
        getServletContext().setAttribute("username", "itheima");
        getServletContext().removeAttribute("username");
        HttpSession session = request.getSession();
        session.setAttribute("username", "itcast");
        session.setAttribute("username", "itheima");
        session.removeAttribute("username");
        request.setAttribute("username", "itcast");
        request.setAttribute("username", "itheima");
        request.removeAttribute("username");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

创建监听器MyAttributeListener

@WebListener
public class MyAttributeListener implements ServletContextAttributeListener,
        HttpSessionAttributeListener, ServletRequestAttributeListener {
    public void attributeAdded(ServletContextAttributeEvent sae) {
        String name = sae.getName();
        System.out.println("an attribute has been added to the ServletContext:" + name + "="
                + sae.getServletContext().getAttribute(name));
    }
    public void attributeRemoved(ServletContextAttributeEvent sae) {
        String name = sae.getName();
        System.out.println("an attribute has been removed from the ServletContext:" + name);
    }
    public void attributeReplaced(ServletContextAttributeEvent sae) {
        String name = sae.getName();
        System.out.println("an attribute has been replaced in the ServletContext:" + name + "="
                + sae.getServletContext().getAttribute(name));
    }
    public void attributeAdded(HttpSessionBindingEvent hbe) {
        String name = hbe.getName();
        System.out.println("an attribute has been added to a session:" + name + "="
                + hbe.getSession().getAttribute(name));
    }
    public void attributeRemoved(HttpSessionBindingEvent hbe) {
        String name = hbe.getName();
        System.out.println("an attribute has been removed from a session:" + name);
    }
    public void attributeReplaced(HttpSessionBindingEvent hbe) {
        String name = hbe.getName();
        System.out.println("an attribute has been replaced in a session:" + name + "="
                + hbe.getSession().getAttribute(name));
    }
    public void attributeAdded(ServletRequestAttributeEvent sra) {
        String name = sra.getName();
        System.out.println("an attribute has been added to the ServletRequest:" + name + "="
                + sra.getServletRequest().getAttribute(name));
    }
    public void attributeRemoved(ServletRequestAttributeEvent sra) {
        String name = sra.getName();
        System.out.println("an attribute has been removed from the ServletRequest:" + name);
    }
    public void attributeReplaced(ServletRequestAttributeEvent sra) {
        String name = sra.getName();
        System.out.println("an attribute has been replaced on the ServletRequest:" + name + "="
                + sra.getServletRequest().getAttribute(name));
    }
}

启动Tomcat,测试

a ServletRequest is about to come into scope of the web application.
an attribute has been replaced on the ServletRequest:org.apache.catalina.ASYNC_SUPPORTED=false
a session has been created.
an attribute has been added to the ServletContext:username=itcast
an attribute has been replaced in the ServletContext:username=itheima
an attribute has been removed from the ServletContext:username
an attribute has been added to a session:username=itcast
an attribute has been replaced in a session:username=itheima
an attribute has been removed from a session:username
an attribute has been added to the ServletRequest:username=itcast
an attribute has been replaced on the ServletRequest:username=itheima
an attribute has been removed from the ServletRequest:username
a ServletRequest is about to go out of scope of the web application.

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值