File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

is HttpSession created automatically?

Tom Nicolas
Greenhorn

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
Marshal

Joined: Mar 22, 2005
Posts: 41101
    
  45
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)


Ping & DNS - my free Android networking tools app
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

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] [Bear's FrontMan] [About Bear] [Books by Bear]
Mukesh Mittal
Greenhorn

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
Greenhorn

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.



 
It is sorta covered in the JavaRanch Style Guide.
 
subject: is HttpSession created automatically?
 
Similar Threads
Hibernate Session Best Practice
HttpSession and servlets
SFSB or SLSB?
HttpSession
is session different for same user