Disabling Session Cookies in Jetty

November 26, 2006

When testing Java web applications, the preferred setting for session handling is url rewriting rather than the use of a browser cookie. This configuration allows the developer to maintain isolated sessions across various tabs and windows, a fairly common practice in pre-production environments. While there are many references available on the web that explain how to configure Jetty 5.x to make this switch, I found it much more difficult to pinpoint instructions for how to do it in the Jetty 6.x series. To save folks the headache, I am documenting the configuration below. Create the file jetty-web.xml and place it in the WEB-INF/ directory. Populate that file with the following contents.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN"
    "http://jetty.mortbay.org/configure.dtd">
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
    <Get name="sessionHandler">
        <Get name="sessionManager">
            <Set name="usingCookies" type="boolean">false</Set>
        </Get>
    </Get>
</Configure>

The syntax for the tags used in this file are detailed on the Jetty Wiki. Much like the Spring configuration, it allows declarative assignment of properties via accessor methods.

By the way, if you aren't using Jetty to deploy your Maven 2 powered application, then you aren't getting the most out of your development time. Take a couple of minutes and follow this maven-jetty-plugin howto to get setup before another minute of hacking goes into the books.

Posted at 05:25 PM in Java | Permalink Icon Permalink

4 Comments from the Peanut Gallery

1 | Posted by Peter Motyka on September 10, 2007 at 04:37 PM EST

Any idea how to disable the WebAppContext's sessionHandler all together?

2 | Posted by qnaguru on November 20, 2008 at 05:26 AM EST

"Any idea how to disable the WebAppContext's sessionHandler all together?"

Well, just disable the cookies, it achieves what you want!

3 | Posted by robertgass on December 23, 2010 at 02:14 PM EST

Thanks! You saved soooo much time for me trying to figure this out! The only small change I had to make was to use the eclipse version instead of the mortbay version.

Thanks again!

4 | Posted by Frank Simon on January 15, 2013 at 03:08 PM EST

We use jetty 6.1.8 and tty this. No effect, cookies will be still used. Any idea, what the reason can be ?

Frank