Skip to content

Write and Read Cookies in different Applications

I have two applications running in Jboss, can I write a cookie in a application and read in another?

For example, if I have two Servlets: – WriterCookieServlet on localhost:8080/Application1 – ReaderCookieServlet on localhost:8080/Application2

WriterCookieServlet:

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

Then on ReaderCookieServlet I want read this cookie.

Answer

I think @galuano1 is right: this should indeed be possible. Make sure the path is correct, though. It is used to determine whether a certain cookie is visible to a web-application.

From the 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.

I’d say you should use / for directory, since both applications will have a different context root.