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.
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?
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
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 ]
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.