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 how to get a session object in the init() method of a servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to get a session object in the init() method of a servlet?" Watch "how to get a session object in the init() method of a servlet?" New topic
Author

how to get a session object in the init() method of a servlet?

Kevin Pang
Ranch Hand

Joined: Mar 01, 2005
Posts: 38
how to get a session object in the init() method of a servlet?
help! many thanks!
Sarath Mohan
Ranch Hand

Joined: Mar 17, 2001
Posts: 213
I think it is not possible.
How do you get a session while a servlet is being initialised?


Sarath Mohan
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

It's not possible. A session combines a series of requests from a certain source into a single unit. But when the servlet's init() method is running, there have not been any requests yet. So the concept of session does not apply there.
Harpreet Hira
Ranch Hand

Joined: Sep 27, 2001
Posts: 72
Session is taken from the request, and request is not processed until init() has executed completely.

Session is generally managed using Cookies or URL rewritting. Both of these are a part of request (Cookies come as header and URL encoding is part of URI) and request is processed by service() method of servlet.

If the session is created(although it is NOT possible) in init(), there will be only one session for all the requests processed by that servlet becase in most (if not all) web applications, there is only instance of servlet, which serves all the clients. This means the init() is executed only once, which in turn would mean that there will be ONLY one session. In nutshell, session can no longer have any meaning as session's main objective is to identify clients. If ALL are treated as ONE, we won't need sessions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to get a session object in the init() method of a servlet?