简体   繁体   English

Java Servlets attributeRemoved()不触发

[英]Java Servlets attributeRemoved() doesn't trigger

I'm new to servlets, I'm following this tutorial everything works fine, but when I remove an attribute from request it doesn't trigger the proper event here is my code. 我是servlet的新手,我正在阅读教程,一切正常,但是当我从请求中删除属性时,它不会触发适当的事件,这是我的代码。
I couldn't find similar case on SO. 我在SO上找不到类似的情况。

Servlet Servlet的

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
            ServletContext ctx = request.getServletContext();
    ctx.setAttribute("User", "Pankaj");
            String user = (String) ctx.getAttribute("User");
            System.out.println("removing attr");
            ctx.removeAttribute("User");   
            HttpSession session = request.getSession();
            session.invalidate();
            PrintWriter out = response.getWriter();
            out.write("Hi "+user);
    }

Listener 倾听者

@WebListener
public class AppContextAttributeListener implements
        ServletRequestAttributeListener
{

    public void attributeAdded(ServletRequestAttributeEvent arg0) {
        System.out.println("ServletContext attribute added::{"
                + arg0.getName() + ","
                + arg0.getValue() + "}");

    }
    public void attributeRemoved(ServletRequestAttributeEvent arg0) {
        System.out.println("here");
        System.out.println("ServletContext attribute removed::{"
                + arg0.getName() + ","
                + arg0.getValue() + "}");
        }
    public void attributeReplaced(ServletRequestAttributeEvent arg0) {
        System.out.println("ServletContext attribute replaced::{"
                + arg0.getName() + ","
                + arg0.getValue() + "}");
    }
}

expected output 预期产量

ServletRequest initialized. Remote IP=0:0:0:0:0:0:0:1%0
ServletContext attribute added::{User,Pankaj}
removing attr
here
ServletContext attribute removed::{User,Pankaj}
Session Created:: ID=8805E7AE4CCCF98AFD60142A6B300CD6
Session Destroyed:: ID=8805E7AE4CCCF98AFD60142A6B300CD6
ServletRequest destroyed. Remote IP=0:0:0:0:0:0:0:1%0

my output 我的输出

ServletRequest initialized. Remote IP=0:0:0:0:0:0:0:1%0
ServletContext attribute added::{User,Pankaj}
removing attr
Session Created:: ID=8805E7AE4CCCF98AFD60142A6B300CD6
Session Destroyed:: ID=8805E7AE4CCCF98AFD60142A6B300CD6
ServletRequest destroyed. Remote IP=0:0:0:0:0:0:0:1%0

I can't get to trigger attributeRemoved() though attributeAdded() is printing which means listener is well defined! 尽管attributeAdded()正在打印,但我无法触发attributeRemoved(),这意味着侦听器定义正确!
What's wrong here? 怎么了 I'm using tomcat 7 and servlets 3 我正在使用tomcat 7和servlets 3

Methods from ServletRequestAttributeListener are called when you add, remove or replace attribute in request eg: 在请求中添加,删除或替换属性时,将调用ServletRequestAttributeListener中的方法,例如:

request.removeAttribute("User");

They are not called when you remove attribue from servletContext: 从ServletContext中删除属性时,不会调用它们:

ctx.removeAttribute("User");

So you should use ServletContextAttributeListener , because methods of this filter are called when attribute has been added/removed/replaced to the ServletContext. 因此,您应该使用ServletContextAttributeListener ,因为当属性已添加/删除/替换到ServletContext时,将调用此过滤器的方法。

Thera are three types of filters to listen attributes change: Thera是用于侦听属性更改的三种类型的过滤器:

  1. ServletContextAttributeListener - receives notification that an attribute has been added/removed/replaced to the ServletContext. ServletContextAttributeListener-接收有关已将属性添加/删除/替换到ServletContext的通知。
  2. ServletRequestAttributeListener - receives notification that an attribute has been added/removed/replaced to the ServletRequest. ServletRequestAttributeListener-接收有关已将属性添加/删除/替换到ServletRequest的通知。
  3. HttpSessionAttributeListener - receives notification that an attribute has been added/removed/replaced from a session. HttpSessionAttributeListener-接收有关已从会话中添加/删除/替换属性的通知。

Thera are another three filter to listen lifecycle changes ServletContext, ServletRequest and HttpSession: Thera是另外三个过滤器,用于监听ServletContext,ServletRequest和HttpSession的生命周期变化:

  1. ServletContextListener - receiving notification events about ServletContext when it is initializated or destroyed. ServletContextListener-在初始化或销毁ServletContext时接收有关ServletContext的通知事件。
  2. ServletRequestListener - receiving notification events about requests coming into and going out ServletRequestListener-接收有关进出请求的通知事件
  3. HttpSessionListener - receiving notification events about HttpSession when it is created or destroyed. HttpSessionListener-在创建或销毁HttpSession时接收有关HttpSession的通知事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM