4

I am developing something using Servlets. I am creating cookies in this program, and a cookie named as JSESSIONID is being created, but when I comment out all the code even then the cookie is created. Here is my code:

CookieDemoServlet.java:

public class CookieDemoServlet extends HttpServlet {

    public void service(HttpServletRequest req, HttpServletResponse res) throws
            ServletException, IOException {

        /*String em = req.getParameter("email");
        Cookie ck[] = req.getCookies();
        if (ck != null) {
            if (ck.length != 0) {
                for (Cookie c : ck) {
                    String cn = c.getName();

                    if (cn.equals("JSESSIONID")) {

                        System.out.println("You are the Old User");
                        String cv = c.getValue();
                        String d = c.getDomain();

                        System.out.println(cn + "\t:" + cv + "\t:" + d);
                    }

                } else {
                    System.out.println("Sorry,No Cookies Found");
                }
            }

            HttpSession session = req.getSession();
            boolean b = session.isNew();
            if (b) {
                System.out.println("You are the New user");
            } else {
                System.out.println("You are the Old User");
            }

            Cookie c1 = new Cookie("Email", em);
            res.addCookie(c1);
            Cookie c2 = new Cookie("Phone", "99999");
            res.addCookie(c2);*/

            RequestDispatcher rd = req.getRequestDispatcher("cookiedemo.jsp");
            rd.forward(req, res);

        }

    }
}

What could be the reason?

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
user3092110
  • 43
  • 1
  • 1
  • 4

2 Answers2

3

JSESSIONID is managed by J2EE Application servers, it is created in each session that the application server has active, is one of the session tracking mechanism that is used by Servlet API

With this, we can know which sessions (objects) belong to a specific user.

Check this.

Koitoer
  • 18,778
  • 7
  • 63
  • 86
  • I am commenting the request dispatcher line also in the code so that page will not be transferred to the JSP page..then also cookie is created and after converting html file into JSP file in some programs i am not getting any cookie..I am not getting this concept. – user3092110 Feb 05 '14 at 17:20
  • There is a very good explanantion about this in Head First Servet and JSP, taking from there: You do have to tell the Container that you want to create or use a session, but the Container takes care of generating the session ID, creating a new Cookie object, stuffing the session ID into the cookie, and setting the cookie as part of the response. And on subsequent requests, the Container gets the session ID from a cookie in the request, matches the session ID with an existing session, and associates that session with the current request. – Koitoer Feb 05 '14 at 17:42
  • 1
    Somehow I accidentally clicked the down vote button and cannot undo it any more :( I just get the following error message: "You last voted on this answer 12 mins ago. You r vote is now locked in unless this answer is edited." Sorry, for that. – kon Feb 20 '18 at 08:52
2

JSESSIONID cookie is created/sent when session is created. Session is created when your code calls request.getSession() or request.getSession(true) for the first time. It is not something you can control. If you create session this cookie will get created.

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
A Paul
  • 8,113
  • 3
  • 31
  • 61