Java 为什么我的 Servlet 会创建一个 JSESSIONID cookie?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21583462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:30:23  来源:igfitidea点击:

Why does my Servlet create a JSESSIONID cookie?

javaservletsjsessionid

提问by user3092110

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

我正在使用 Servlets 开发一些东西。我正在这个程序中创建 cookie,并且JSESSIONID正在创建一个名为 as 的 cookie ,但是当我注释掉所有代码时,即使创建了 cookie。这是我的代码:

CookieDemoServlet.java:

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?

可能是什么原因?

回答by A Paul

JSESSIONIDcookie 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.

JSESSIONID创建会话时创建/发送cookie。会话是在您的代码调用request.getSession()request.getSession(true)第一次调用时创建的。这不是你能控制的。如果您创建会话,则将创建此 cookie。

回答by Koitoer

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

JSESSIONID 由 J2EE 应用服务器管理,在应用服务器活跃的每个会话中创建,是 Servlet API 使用的会话跟踪机制之一

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

有了这个,我们可以知道哪些会话(对象)属于特定用户。

Check this.

检查这个。