I would like to eliminate the HttpSession completely – can I do this in web.xml? I’m sure there are container specific ways to do it (which is what crowds the search results when I do a Google search).
P.S. Is this a bad idea? I prefer to completely disable things until I actually need them.
You can’t entirely disable it. All you need to do is to just not to get a handle of it by either
request.getSession()orrequest.getSession(true)anywhere in your webapplication’s code and making sure that your JSPs don’t implicitly do that by setting<%@page session="false"%>.If your main concern is actually disabling the cookie which is been used behind the scenes of
HttpSession, then you can in Java EE 5 / Servlet 2.5 only do so in the server-specific webapp configuration. In for example Tomcat you can set thecookiesattribute tofalsein<Context>element.Also see this Tomcat specific documentation. This way the session won’t be retained in the subsequent requests which aren’t URL-rewritten –only whenever you grab it from the request for some reason. After all, if you don’t need it, just don’t grab it, then it won’t be created/retained at all.
Or, if you’re already on Java EE 6 / Servlet 3.0 or newer, and really want to do it via
web.xml, then you can use the new<cookie-config>element inweb.xmlas follows to zero-out the max age:If you want to hardcode in your webapplication so that
getSession()never returns aHttpSession(or an “empty”HttpSession), then you’ll need to create a filter listening on anurl-patternof/*which replaces theHttpServletRequestwith aHttpServletRequestWrapperimplementation which returns on allgetSession()methodsnull, or a dummy customHttpSessionimplementation which does nothing, or even throwsUnsupportedOperationException.If you don’t need them, just don’t use them. That’s all. Really 🙂