0

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?

zepol
  • 187
  • 5
  • 20

1 Answers1

1

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

Svetlin Zarev
  • 14,713
  • 4
  • 53
  • 82
  • Well I tried sending cookies but it doesnt seem to work either I'm getting a different session – zepol Sep 26 '16 at 19:37
  • You must send the JSESSIONID cookie that you received in the response of the first request. – gsl Sep 28 '16 at 23:25