HttpSessionListener.sessionCreated() not being called

后端 未结 3 833
遥遥无期
遥遥无期 2021-01-16 07:32

I have a very simple Servlet and a very simple HttpSessionListener:

@WebServlet(\"/HelloWorld\")
public class HelloWorld extends HttpServlet {
    private st         


        
相关标签:
3条回答
  • 2021-01-16 08:11

    Well, I'll try to go a bit deeper here. This problem (sessionCreated(..) not being invoked) may occur even without any correlation with [any] IDE. To be honest, I think, that cleaning the project (and the like) just coincided with problem solving (which is - cookies) and it's not related to the cleaning of project per se.

    I ran into same problem and was resolving for about an hour or so.

    Important clue and the actual problem here is that your browser may have stored Cookies it received in the previous runtime of the same application from the server, and those cookies aren't timed-out, killed or destroyed anyhow, hence, session will not get created with .getSession(true) because every time you issue the request, you send cookie along.

    If, during the runtime of the application, you'll open dev-tools (or respective utility in other browser than Chrome) and clear cookies, and then re-try to access URL servlet of which creates the session, you will see that container will invoke sessionCreated method, as new session will be created.

    0 讨论(0)
  • 2021-01-16 08:18

    Turns out it was one of those stupid Eclipse issues...

    Project->Clean... and restarting Tomcat did the trick.

    0 讨论(0)
  • 2021-01-16 08:30

    If you're not using @WebListener make sure your listeners are referenced in the web.xml.

    <web-app>...
        <listener>
            <listener-class>com.yoursite.YourSessionListener</listener-class>
        </listener>
    

    In order to receive these notification events, the implementation class must be either declared in the deployment descriptor of the web application, annotated with WebListener, or registered via one of the addListener methods defined on ServletContext.

    0 讨论(0)
提交回复
热议问题