[英]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是用于侦听属性更改的三种类型的过滤器:
Thera are another three filter to listen lifecycle changes ServletContext, ServletRequest and HttpSession: Thera是另外三个过滤器,用于监听ServletContext,ServletRequest和HttpSession的生命周期变化:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.