Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is HttpSession created automatically?

 
Tom Nicolas
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is my comprehension about HttpSession,am i right?

'when the client visit the web application first time,the JVM create the HttpSession object for the client. so it created automatically '
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Where did you get that statement? In servlets, sessions are not automatically created, you have to ask for them using HttpServletRequest.getSession. In JSP pages the default is indeed to create sessions, although that can be turned off by a page directive.

(Just to nitpick: it is not the JVM that creates a session, it is the servlet engine)
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64608
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to disagree with Ulf, though it may just be a matter of semantics.

Regardless of whether you are using JSPs or servlets, the session itself is created automatically by the servlet container. In a JSP, a reference to the session is automatically created as an implicit variable. In a servlet, you explicitly obtain a reference to the session via the mechanism Ulf describes.

In both cases however, the session itself is automatically created on your behalf.
 
Mukesh Mittal
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hava a question regarding that.

I created one index.jsp page and then call Servlet. In servlet I am printing all the headers. I am geting session id in that (not used request.getSession()). I have not created that session and session is old.


Shall I consider as you mentioned, container created that session.
 
Mukesh Mittal
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I understand that. When you call your application through index.html and then call Servlet. If you print header, you are not going to get JSessionID.

If you access through index.jsp, because of implicit variable in JSP, you will get that JSessionID.



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic