HttpSessionListener.sessionCreated()未被调用 [英] HttpSessionListener.sessionCreated() not being called
问题描述
我有一个非常简单的Servlet和一个非常简单的HttpSessionListener:
I have a very simple Servlet and a very simple HttpSessionListener:
@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
getServletContext().setAttribute("applicationHits", new AtomicInteger(0));
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("get");
((AtomicInteger) request.getServletContext().getAttribute("applicationHits")).incrementAndGet();
((AtomicInteger) request.getSession(true).getAttribute("sessionHits")).incrementAndGet();
request.setAttribute("requestHits", 0);
getServletContext().getRequestDispatcher("/view/HelloWorld.jsp").forward(request, response);
}
}
@WebListener
public class SessionListener implements HttpSessionListener {
public SessionListener() {
}
public void sessionCreated(HttpSessionEvent arg0) {
System.out.println("session listener");
arg0.getSession().setAttribute("sessionHits", new AtomicInteger(0));
}
public void sessionDestroyed(HttpSessionEvent arg0) {
}
}
我的 HttpSessionListener.sessionCreated()
方法永远不会被调用(没有日志输出),我最终得到一个<我正在调用getSession()
My HttpSessionListener.sessionCreated()
method is never called (no log output), and I end up getting a NullPointerException
on the line where I'm calling getSession()
((AtomicInteger) request.getSession(true).getAttribute("sessionHits")).incrementAndGet();
request.setAttribute("requestHits", 0);
我试过调用 getSession()
而不用 true
以及同样的问题。
I tried calling getSession()
without true
as well, but same problem.
我不明白 - 是不是 @WebListener
注释足以调用我的监听器? Eclipse甚至在部署描述符/监听器
下将其显示为侦听器。
I don't get it - isn't the @WebListener
annotation enough to invoke my listener? Eclipse even displays it as a listener under Deployment Descriptor/Listeners
.
推荐答案
原来这是一个愚蠢的Eclipse问题......
Turns out it was one of those stupid Eclipse issues...
项目 - >清理......然后重启Tomcat就行了。
Project->Clean... and restarting Tomcat did the trick.
这篇关于HttpSessionListener.sessionCreated()未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!