2

I got a different requirement which I haven't tried before. Let's say I have two web applications which will run on same browser with localhost as a host. Can it possible to set the cookie value in my first web application and get the respective cookie value in my second web application?

If possible, How can I do the same?

I tried as follows, But I am getting Cookie value as null.

In my First Web application,

Cookie ck = new Cookie("PortalUser", uName);
ck.setDomain("localhost");
ck.setMaxAge(30 * 60);
response.addCookie(ck);

In my Second Web application,

HttpSession mySession = request.getSession();
System.out.println(mySession.getAttribute("PortalUser"));//Value is printing null
13
  • Have you tried to achieve it the same way you fetch out cookies in a single web application ? Mar 17, 2015 at 14:20
  • Well as long as the domain is same, you can access the cookies but it depends on how you're storing them. If they're HttpOnly cookies, then they're cleared for every 302 page redirects
    – Arkantos
    Mar 17, 2015 at 14:22
  • 1
    session.setAttribute(key,value) will store the value in the corresponding HttpSession associated with the current request. That's not the way to store cookies. Have a look at Cookie API here and here
    – Arkantos
    Mar 17, 2015 at 14:33
  • 1
    Do you've javax.servlet-api.jar in your build path ? If you're using Maven, add that dependency in your pom.xml, if not you can download it from here
    – Arkantos
    Mar 17, 2015 at 15:41
  • @Arkantos, I tried by adding jar file. But I am getting cookie value as null. I have updated my question. Can you please correct me what's wrong with my code
    – Vinod
    Mar 17, 2015 at 16:11

1 Answer 1

0

You can try using CookieManager and set its Cookie Policy to ACCEPT_ALL and then use CookieHandler to get the Cookie store.

CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cookieManager);
// Creates url for the given string
URL url = null;
try {
    url = new URL("http://localhost/");
    // Opens a connection with the url specified and returns URLConnection object
    URLConnection urlConnection = url.openConnection();
    // Gets the contents from this url specifies
    urlConnection.getContent();
} catch (MalformedURLException | IOException e) {
    e.printStackTrace();
}
// Returns the cookie store(bunch of cookies)
CookieStore cookieStore = cookieManager.getCookieStore();
// Getting cookies which returns in the form of List of type HttpCookie
List<HttpCookie> listOfcookies = cookieStore.getCookies();
for (HttpCookie httpCookie: listOfcookies) {
    System.out.println("Cookie Name : " + httpCookie.getName() + " Cookie Value : " + httpCookie.getValue());
}
5
  • Thank you. I will try and let you know the status
    – Vinod
    Mar 19, 2015 at 16:59
  • Do I need to add any jars? Got an an error, CookieManager is not resolved a type
    – Vinod
    Mar 19, 2015 at 18:43
  • It's of java.net.CookieHandler package so you don't need to import any jars.
    – SSC
    Mar 20, 2015 at 10:50
  • It is not returning any cookie values, It is showing cookie list size as 0. Any suggestions ?
    – Vinod
    Mar 23, 2015 at 7:13
  • That means you have no cookies at all.
    – SSC
    Apr 17, 2015 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.