views:

839

answers:

4

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).

Thanks, LES

P.S. Is this a bad idea? I prefer to completely disable things until I actually need them.

A: 

It's not actually wise to disable HttpSession (if that's possible). The reason is that your application server handles HTTP request by storing connection information on Http Session in form of cookies. Unless you really don't want server side cookies (which will require you to do self management of http sessions) then it's advisable to keep HttpSession.

The Elite Gentleman
so if you're developing a restful application with zero session usage, you still have to have http session enabled?
LES2
You get HttpSession by calling `HttpServletRequest.getSession()`. If you have RESTful application, why worry about getting HttpSession when you obviously know that you only want the request? It shouldn't matter to, Sun just create a Servlet Specification which allows us to have session from ServletRequest. Take it or leave it, it's up to you!
The Elite Gentleman
actually, don't JSP pages create sessions automatically unless you specify session=false in the page directive? i'm not so much worried about myself using the session - i know better. it's kinda like defensive programming. i could simply document that a particular array shouldn't be modified after passed to my method ... or i can make a defensive copy and not worry about other devs screwing up my code. i am going to accept one of the highest voted answers shortly, however - they are generally helpful and informative. thanks!!!!
LES2
No...the `session="false"` in the page directives doesn't allow you to use sessions created by the container e.g. `<jsp:useBean scope="session">`, if that was created with session=false, then an error will occur. You can still get your `request.getSession()` objects from the JSP. What the page directive is doing is not to allow you to create session on page scope level.
The Elite Gentleman
A: 

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

I don't think so. Disabling the HttpSession would be a violation of the Servlet spec which states that HttpServletRequest#getSession should return a session or create one. So I wouldn't expect a Java EE container to provide such a configuration option (that would make it non compliant).

Is this a bad idea? I prefer to completely disable things until I actually need them.

Well, I don't really get the point, just don't put anything in the session if you don't want to use it. Now, if you really want to prevent the use of the session, you can use a Filter to replace the request with a implementation of HttpServletRequestWrapper overriding getSession(). But I wouldn't waste time implementing this :)

Update: My initial suggestion was not optimal, the "right" (cough) way would be to replace the request.

Pascal Thivent
A: 

Rather than disabling you can rewrite the URL using a URL rewrite filter eg tuckey rewrite filter. This will give Google friendly results but still allow cookie based session handling.

However, you should probably disable it for all responses as it's worse than just search engine unfriendly. It exposes the session ID which can be used for certain security exploits.

Example config for Tuckey filter:

<outbound-rule encodefirst="true">
  <name>Strip URL Session ID's</name>
  <from>^(.*?)(?:\;jsessionid=[^\?#]*)?(\?[^#]*)?(#.*)?$</from>
  <to>$1$2$3</to>
</outbound-rule>
Pool
+1  A: 

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() or request.getSession(true) anywhere in your webapplication's code.

If your main concern is actually disabling the cookie which is been used behind the scenes of HttpSession, then you can do so in the webapp's Context element. In for example Tomcat you can set cookies attribute to false.

<Context cookies="false">

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.

If you want to hardcode in your webapplication so that getSession() never returns a HttpSession (or an "empty" HttpSession), then you'll need to create a filter listening on an url-pattern of /* which replaces the HttpServletRequest with a HttpServletRequestWrapper implementation which returns null or throws UnsupportedOperationException on all getSession() methods.

P.S. Is this a bad idea? I prefer to completely disable things until I actually need them.

If you don't need them, just don't use them. That's all. Really :)

BalusC
@balus...+1 for a great explanation:)
Suraj Chandran