Search code examples
jsfservletslifecyclehttpsession

How to get a Http Session while a servlet is being initialised?


we can get the session if we can access to HTTP request object like below:

HttpSession session = request.getSession();

But, How to retrieve the HTTP session inside init method of Servlet ?


If the presentation Layer is powered by JSF , Can servlet access to FacesContext to get the HTTP Session ?


Solution

  • You can't. The init() method is invoked when the application is deployed and the servlet is initialized. No one has connected yet to the application at this time, and the code is thus not executing as part of any request handling. So there's no session.

    That's like wanting to get the name of the customer being served at the bakery while you're still building the bakery. No bakery, no customer being served.