Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Kevin Pang
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to get a session object in the init() method of a servlet?
help! many thanks!
 
Sarath Mohan
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it is not possible.
How do you get a session while a servlet is being initialised?
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic