Loading
 
What's Next: View All Tutorial →

Sharing same cookie between domain and subdomain including JSESSIONID

In this article, we will see how to share the same cookie between domain and subdomain. In Tomcat, We can add   <!-- src\main\webapp\META-INF\context.xml --> <?xml version="1.0" encoding="UTF-8"?> <C...

Published Date: 11/01/2015  Last Modified Date: 11/01/2015    New Post



In this article, we will see how to share the same cookie between domain and subdomain.

In Tomcat, We can add
 
<!-- src\main\webapp\META-INF\context.xml -->

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/" sessionCookieDomain=".catchmycity.com" sessionCookiePath="/">
    
</Context>

sessionCookieDomain=".catchmycity.com" sessionCookiePath="/"  will set the JSESSION cookie in .catchmycity.com

So both www.catchmycity.com and http://catchmycity.com will use the same login session.


Setting Java Cookie with subdomain

 
//import javax.servlet.http.Cookie;

Cookie cityCookie = new Cookie("cityId", Utils.getStr(cityMap.get("id")));
cityCookie.setMaxAge(3000000);
cityCookie.setDomain("catchmycity.com");
cityCookie.setPath(request.getContextPath() + Constants.FORWARD_SLASH);
response.addCookie(cityCookie);
 

cityCookie.setDomain  with subdomain will be shared set the cookie in .catchmycity.com.  So both www.catchmycity.com and http://catchmycity.com will use the same login session.
Awaiting for Administrator approval





Tags: Issue and Solutions

← Back to list


Related Post




×