Why is my servlet creating a JSESSIONID cookie?

I am developing something using Servlets. I create cookies in this program and a cookie is created with the name JSESSIONID, but when I comment on all the code, even then a 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?

+3
source share
2 answers

The JSESSIONID is controlled by the J2EE application servers that are created in every session that the application server is active, is one of the session tracking mechanisms that the Servlet API uses.

At the same time, we can know which sessions (objects) belong to a specific user.

Mark it.

+2
source

JSESSIONID cookie / . , request.getSession() request.getSession(true) . , . , cookie .

+1

All Articles