javasessiontomcatservlets

Session is lost and created as new in every servlet request


I have this big issue. My current session is gone every time I made a new request to Server.

I have checked in a lot of places. I can't find what's the problem. I also have included session-config in web.xml both in tomcat and application. I also enabled to accept cookies to my browsers. Tested in every browser. It's not working.

I am just developing a simple java ee applcation using JSP/Servlet. I am facing the problem only after I have deployed to tomcat in server machine.


Solution

  • After years, I never posted the answer back here. At that time I was busy and forgot about this question. But, today I am looking for a solution in Stackoverflow as usual and saw this notification mentioning I am getting points from this Question. Seems like other developers are facing the same issue. So, I tried to recall how I solved the issue. And yes, I solved by manually put back the session id to track/maintain the session id.

    Please see the code that I manually put back jsessionid inside the servlet.

    HttpSession session = request.getSession();
    if (request.getParameter("JSESSIONID") != null) {
        Cookie userCookie = new Cookie("JSESSIONID", request.getParameter("JSESSIONID"));
        response.addCookie(userCookie);
    } else {
        String sessionId = session.getId();
        Cookie userCookie = new Cookie("JSESSIONID", sessionId);
        response.addCookie(userCookie);
    }