File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes is HttpSession created automatically? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "is HttpSession created automatically?" Watch "is HttpSession created automatically?" New topic

is HttpSession created automatically?

Tom Nicolas

Joined: Dec 30, 2005
Posts: 4
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

Joined: Mar 22, 2005
Posts: 42965
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

Joined: Jan 10, 2002
Posts: 63856

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.

[Asking smart questions] [About Bear] [Books by Bear]
Mukesh Mittal

Joined: Dec 29, 2005
Posts: 16
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

Joined: Dec 29, 2005
Posts: 16
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.

I agree. Here's the link:
subject: is HttpSession created automatically?
It's not a secret anymore!