20040122 Thursday January 22, 2004

Turn off sessions in your JSPs One of the tips I picked up in J2EE Design and Development was to turn off automatic session-creation in your JSPs. So I tried this today on my project, and it doesn't seem to work. Basically this involved putting the following at the top of your JSPs:

<%@ page session="false"%>

I did this by adding this to my taglibs.jsp, which is included in every other JSP - so it should affect all my JSPs and prevent a session from being created on my welcome-file - right? Not according to Tomcat's Manager application - it still increments the number of sessions as I hit the welcome page with different browsers. Am I doing something wrong?

I'd love to get this working - so sessions are only created when a user logs in. Those JSPs that need a session, I'll override the default with session="true". I often see a whole bunch of sessions on the wiki and roller apps on this site - and in reality - a session should never be created, unless I login. At least I think that's how this is designed to work... Posted in Java at Jan 22 2004, 11:23:18 AM MST 4 Comments

Comments:

I used this once in iPlanet and it worked - I included the line directly in each page. During load testing, the maximum number of allowable sessions would be reached quickly and subsequent client connections would be refused. Most of the pages did not need sessions, so turning them off where not needed allowed a larger number of client requests to be handled. I believe I also tested this successfully in Tomcat but I'm not positive.

Posted by Gary Blomquist on January 22, 2004 at 02:13 PM MST #

This "feature" is definitely container specific. OrionServer just got around to fixing it recently - it used to have no effect on the compiled pages. And then when it finally did, it made the internal session object null, so it broke a bunch of pages that used it without initializing first (back when I was using a lot of scriptlets). If you're using Tomcat, I wonder if this is a general Jasper issue? -R

Posted by Russ on January 22, 2004 at 04:14 PM MST #

Just exactly what is this suppose to do? Presumably a jsp will simply include session = request.getSession(false) whenever this attribute is true. So why would this have any impact on the number of sessions in use at any given time? I thought it was just there to populate the session local of the generated servlet from the jsp. Sessions are only created via a getSession(true). Without specifying true or false the default in tomcat produces session = pageContext.getSession(); this returns the current value of the session.

Posted by Robert Nicholson on January 23, 2004 at 02:48 AM MST #

how do you include "taglibs.jsp"? if you use <jsp:include... the including page is handled seperately from the include, also regarding session handling. if you use the compile-time <%@ include... it might be too late for this instruction or even illegal depending on the server you use. or maybe it conflicts with some settings in your web.xml? here are some things to try out: http://www.caucho.com/resin-2.1/ref/caching.xtp

Posted by andi on January 23, 2004 at 08:41 AM MST #

Post a Comment:
  • HTML Syntax: Allowed