How to configure sessioncookie at Tomcat server level OR at Application level (web.xml)

How to configure session cookie at Tomcat server level or at Application level (web.xml)

In this post we will see how to configure session cookie at Tomcat server level or at application level using web.xml

You can use either way to set a cookie.

  • Tomcat server

Go to -> conf directory -> open context.xml -> add below lines:-

<Context sessionCookieName="<yourCookieName>" sessionCookiePath="<yourCookiePath>" sessionCookieDomain="<yourDomainName>">

 

  • Application level

To configure cookie at application level using web.xml requires servlet 3.0

    
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
	      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	      version="3.0">
<session-config>
        <session-timeout>
            30
        </session-timeout>
	<cookie-config>
	   <name>MSESSIONID</name>
	   <path>/anoop</path>
	    <http-only>true</http-only>
    </cookie-config>
</session-config>
</web-app>

 

Note that to use servlet 3.0 spec we included http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd in the web-app xml namespace

Leave a Reply

Your email address will not be published. Required fields are marked *