Thursday, October 7, 2010

How to get session in any servlet?

How to get session in any servlet?
Ans:Here is two way to get the session in servlet
1-This common and most used technique in servlet.
  using request object
  HttpSession=request.getSession();
2-second is get from the session event object.
  HttpSessionEvent has getSession
  Suppose if you create any Listener class which is implement the HttpSessionListener Interface then you could get the session object

ServletConfig:

ServletConfig:
Package -javax.servlet
Interface/Class-interface
Total Method :4
1-java.lang.String getServletName() { }
  This method is return the name of the servlet.
2-javax.servlet.ServletContext getServletContext() { }
  This is return the Object of ServletContext
3-java.lang.String getInitParameter(java.lang.String p1) { }
  if you but any parameter in web.xml then using this method you retrive the value
4-java.util.Enumeration getInitParameterNames() { }

ServletConfig :it is one par servlet.When the container intitalize a servlet .it create a unique servletConfig for the servlet .

Example:How to put any value in the web.xml
Because the servletConfig is one par servlet so  the param value and param name tag should be with in servlet tag.

<init-param>
<init-name>
</init-name>
<init-value>
</init-value>