zepol
zepol

Reputation: 309

HttpSession.getSession().isNew() is always true

I'm working with a HtppServlet and I need to use the session in order to know what I have to do (first time or not). But when I when the session it seems is always a different one, so the process always does the same it's supposed to do the first time. The code below:

        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                    throws ServletException, IOException {

                HttpSession session = request.getSession(true);
                response.setContentType("application/json");
                PrintWriter out = response.getWriter();

                try {
                       if (session.isNew()) {
                            String id=callMethod1();
                            session.setAttribute("ID",id);
                        } else {
                             callMethod2(session.getAttribute("ID"));
                        }
              }catch (Exception ex) {
                //Error handler
              } finally {
                out.close();
             }
        }

The thing is I need to call callMethod2() the second time, but it's always calling callMethod1(), I've tried using session.getSession(false) and session.getSession() I'm using curl to calling the Servlet and Google Chrome, does anyone have an idea what's happening or how can I solve this?

Upvotes: 0

Views: 1416

Answers (1)

Svetlin Zarev
Svetlin Zarev

Reputation: 15663

When you use curl you must send back the cookies you received. The most important cookie is the JSESSIONID cookie, which identifies your session. If you don;t send it back the server cannot know that the request is part of some session.

Here is a very nice tutorial about curl and cookies: https://curl.haxx.se/docs/http-cookies.html

Upvotes: 1

Related Questions