gpt4 book ai didi

java - 在不同的应用程序中写入和读取 Cookie

转载 作者:行者123 更新时间:2023-11-30 07:22:27 26 4
gpt4 key购买 nike

我在 Jboss 中运行了两个应用程序,我可以在一个应用程序中写入 cookie 并在另一个应用程序中读取吗?

例如,如果我有两个 Servlet: - localhost:8080/Application1 上的 WriterCookieServlet - 本地主机上的 ReaderCookieServlet:8080/Application2

WriterCookieServlet:

 Cookie cookie = new Cookie("cookie", "cookieValue");
response.addCookie(cookie);
response.sendRedirect("localhost:8080/Application2");

然后我想在 ReaderCookieServlet 上读取这个 cookie。

最佳答案

我认为@galuano1 是对的:这确实是可能的。不过,请确保路径 是正确的。它用于确定某个 cookie 是否对网络应用程序可见。

来自Cookie Javadoc :

The cookie is visible to all the pages in the directory you specify, and all the pages in that directory's subdirectories. A cookie's path must include the servlet that set the cookie, for example, /catalog, which makes the cookie visible to all directories on the server under /catalog.

Consult RFC 2109 (available on the Internet) for more information on setting path names for cookies.

我会说你应该使用 / 作为目录,因为两个应用程序将有不同的上下文根。

关于java - 在不同的应用程序中写入和读取 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12727199/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com