Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Do JSPs always create a session?  RSS feed

 
Andy Hahn
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have heard a couple times now that JSPs will always create a session whether you want one or not. Is that true?

http://www.techper.net/2008/07/21/a-jsp-file-always-creates-a-session/
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be honest I'm not sure, and I haven't checked the spec to see whether any behaviour is mandated.
Certainly a session must be created if you refer to the implicit session object, but whether one is still created if you don't use that object is not clear.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you care?
 
Andy Hahn
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I care because I dont want a session created unless I specify that I want one created. I work hard to write sessionless applications because I want every grain of memory available that I can get.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How much memory have you determined that an unused session consumes?
 
Andy Hahn
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's probably not that much memory individually, but if my app gets 1,000,000 unique visitors a day, then it all adds up. I'm not out to change the servlet spec, mostly just curious.
 
vk jain
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On jsp page a session object will be created always. There is no way to stop that. If you use page directive as <@page session="false"> then session will be created but will not be available on jsp page.

You can place a filter in your application and check for session.isNew(). This will show you if the session is created is new one or old one. You can perform your working on based of this check.

May be this will be helpful to you.
 
Carey Evans
Ranch Hand
Posts: 225
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vk jain:
On jsp page a session object will be created always. There is no way to stop that. If you use page directive as <@page session="false"> then session will be created but will not be available on jsp page.


I think this must depend on the application server. I have JSPs running on Jetty 6.1 where no session is created if I set <@page session="false">.
 
Devi Taruni
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A page directive session attribute's default value is true, now.. it means the predefined variable session should be bound to the existing session if one exists, otherwise a new session should be created and bound to it.

I think this must depend on the application server. I have JSPs running on Jetty 6.1 where no session is created if I set <@page session="false">.


I totally disagree with your statement because the session is created no matter whichever application server you use.
with <@page session="false"> you just mean that no sessions will be used, and attempts to access the variable session will result in errors.

You can't do this...

<% if (something) {
session.setAttribute(...); // use session here
} else {
// code that does NOT use session here
}
%>
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Javaranch Sch´┐Żn Sinn ,

I agree with you, behavior of JSP is independent of the application server or the JSP container as long as the server follows the specification
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!