This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Getting HttpSession from the init method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Getting HttpSession from the init method" Watch "Getting HttpSession from the init method" New topic
Author

Getting HttpSession from the init method

igwe kalu kalu ogba
Ranch Hand

Joined: Feb 03, 2005
Posts: 133
Hello, I know that the normal way to get the httpsession is by using:

HttpSession session = request.getSession(true);
String dbName = (String) session.getAttribute("dbName");

inside a doGet or doPost method.

However is it possible to get the session object from the servlet's init method. I know init looks like this : init().
Can I override it? maybe init (HttpServlet request) or something?
If not how else can I get the session in a servlet.
My servlet is using init because it loads on startup . Many thanks.
Ola Daniel
Ranch Hand

Joined: Jul 27, 2005
Posts: 105
Igwe, When a servlet loads at startup, that means an instance of the servlet is started as soon as the web app is deployed. No session can exist at this stage.

Can someone please confirm that.

Can you use the Application Context for whatever you are considering setting in session instead?

eg...


This response is based on the assumption that yu want to instatiate a session to store objects.


SCJP 1.4, SCWCD 1.4
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler et al, Refactoring: Improving the Design of Existing Code, 1999
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41141
    
  45
A session is used to identify clients across requests. At startup there is no request, so Ola is correct in pointing out that there can be no session. That's also the reason why you get a session through Request, and not ServletContext.
[ August 26, 2005: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you want to pre-load each session with certain information when it's created, you could use an HttpSessionListener
If this information is always going to be the same for all session, then it would make more sense, as Ola suggested, to store this information in context scope instead.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting HttpSession from the init method
 
Similar Threads
Init Params doubt
getInitParameter
How to call service() from init()
ServletContext Null pointer Exception
getInitParameter() ?