Skip to main content

Change Tomcat JSESSION Cookie Name and Path


Sometime we have a scenario that two different tomcat application running on same domain,  and you want to share a session cookie in between them, there are two problem will come in this scenario i.e:

1. Session Cookie Name : As both the application have same cookie name i.e JSESSIONID , so its difficult to know which JSESSIONID belongs to which application , so in this scenario we need to change the Session cookie name.

2. Cookie Path : By default tomcat create a session cookie for the app context, due to which we can't share the cookies between two application , so we need to change the cookie path from  context to the root.

For doing the above point we can use ServletContextListener or web.xml file.

1. ServletContextListener


import javax.servlet.SessionCookieConfig;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyTestListener implements ServletContextListener 
{
    public void contextInitialized(ServletContextEvent sce) 
    {
        String comment = "This is my special cookie configuration";
        String domain = "root.com";
        String path = "/";
        boolean isSecure = true;
        boolean httpOnly = false;
        int maxAge = 30000;
        String cookieName = "MY_SESSION";
        SessionCookieConfig scf = sce.getServletContext().getSessionCookieConfig();
        scf.setComment(comment);
        scf.setDomain(domain);
        scf.setHttpOnly(httpOnly);
        scf.setMaxAge(maxAge);
        scf.setPath(path);                // ROOT path
        scf.setSecure(isSecure);
        scf.setName(cookieName); //JSESSION Cookie name

        }
    }
    public void contextDestroyed(ServletContextEvent sce) 
    {
    }
}

2. Web.xml

<session-config>
          <cookie-config> 
                <comment>This is my special cookie configuration</comment> 
                <domain> root.com</domain> 
                <http-only>false</http-only>
                <max-age>30000</max-age> 
                <path>/</path>                      // ROOT path
                <secure>true</secure> 
               <name>MY_SESSION</name> //JSESSION Cookie name
           </cookie-config> 
</session-config>




Comments

Popular posts from this blog

Setup Jenkins in Tomcat Webserver

Jenkins is a software that allows continuous integration. Jenkins will be installed on a server where the central build will take place. System Requirements: 1. JDK 2. Tomcat Java installation : 1. Download the latest JDK from Oracle's site 2. CentOS look for the package jdk-7u17-linux-x64.rpm (64-bit architecture) or jdk-7u17-linux-i586.rpm (32-bit architecture) 3. Once you download the installation package, use the command rpm -ivh 4. Confirm you have the correct Java by running java -version Tomcat installation: 1. Download Apache Tomcat archive file from Apache Tomcat official download page. You can use below wget command to download it. cd /tmp wget http://www.us.apache.org/dist/tomcat/tomcat-7/v7.0.54/bin/apache-tomcat-7.0.54.tar.gz 2. After competed download extract archive file in /tmp directory and move to proper location as per your need. We are placing this under /usr/local directory. tar xzf apache-tomcat-7.0.54.tar.gz mv apache-t...

Encode from JavaScript and Decode at Java.

if you have a senerio like you want to send encoded parameter values from your web page  and decode them  at server side then this blog may be help you. JavaScript: Before submit the form I am encoding all the value entered by user so that it can not be read by humanly, for encoding the values I have written a method named  encodeBase64 <script> function encodeBase64(input){         var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";         input = escape(input);         var output = "";         var chr1, chr2, chr3 = "";         var enc1, enc2, enc3, enc4 = "";         var i = 0;              do {             chr1 = input.charCodeAt(i++);             chr2 = input.charCodeAt(i++);     ...

Apache and Tomcat Inegration in CentOS:

Apache  the most popular open source web server and Tomcat  the most popular open source application server for the Java application (JSP & Servlets). Benefits  to have a Apache Layer over the tomcat : Static Content – Apache serves static content better than Tomcat and supports better caching mechanisms. Availability – Apache allows load balancing and clustering of multiple Tomcat servers behind it, thus providing high availability. Security – Apache protects Tomcat through its built-in security features and through advanced third-party modules such as ModSecurity. Extensibility – Apache provides an abundance of modules for just about anything from URL rewriting (ModRewrite) to GeoIP services. With Apache you can use these modules to extend Tomcat's functionality. Java installation : 1 . download the latest JDK from  Oracle's site 2. CentOS look for the package jdk-7u17-linux-x64.rpm (64-bit architecture) or jdk-7u17-linux-i5...