logo

HttpSession Interface


Show

In such a case, the container creates a consultation identityentification for every user.The container makes use of this identityentification to become aware of the precise user. An item of HttpSession may be used to carry out tasks:

  1. bind objects
  2. view and manage statistics approximately a consultation, along with the consultation identifier, introduction time, and final accessed time.

How to get Https Session Object?

There are two methods provided by the HttpServletRequest interface to get the object of httpSession:

  1. public HttpSession getSession(): Returns the modern session related to this request, or if the request no longer has a session, creates one.
  2. public HttpSession getSession(boolean create): Returns the modern HttpSession related to this request or, if there's no modern session and create is true, returns a brand new session.

Commonly used ways of HttpSession interface:

  1. public String getId(): Returns a string containing the particular identifier value.
  2. public lengthy getCreationTime(): Returns the time while this consultation turned into created, measured in milliseconds considering that middle of the night January 1, 1970 GMT.
  3. public lengthy getLastAccessedTime(): Returns the ultimate time the customer despatched a request related to this consultation, because the wide variety of milliseconds considering that middle of the night January 1, 1970 GMT.
  4. public void invalidate(): Invalidates this consultation then unbinds any gadgets sure to it.

Example of using HttpSession:

In this example, we're putting the characteristic withinside the consultation scope in a single servlet and getting that cost from the consultation scope in some other servlet. To set the characteristic withinside the consultation scope, we've got used the setAttribute() technique of HttpSession interface and to get the characteristic, we've got used the getAttribute technique.

Index.html

<form action="servlet1">  
Name:<input type="text" name="userName"/><br/>  
<input type="submit" value="go"/>  
</form> 

FirstServlet.java

import java.io.*;  
import javax.servlet.*;  
import javax.servlet.http.*;   

public class FirstServlet extends HttpServlet {    

public void doGet(HttpServletRequest request, HttpServletResponse response){  
        try{  

        response.setContentType("text/html");  
        PrintWriter out = response.getWriter();          

        String n=request.getParameter("userName");  
        out.print("Welcome "+n);          

        HttpSession session=request.getSession();  
        session.setAttribute("uname",n);   

        out.print("<a href='servlet2'>visit</a>");                  

        out.close();   

                }catch(Exception e){System.out.println(e);}  
    }    
}  

SecondServlet.java

import java.io.*;  
import javax.servlet.*;  
import javax.servlet.http.*;  

public class SecondServlet extends HttpServlet {  

public void doGet(HttpServletRequest request, HttpServletResponse response)  
        try{  

        response.setContentType("text/html");  
        PrintWriter out = response.getWriter();           

        HttpSession session=request.getSession(false);  
        String n=(String)session.getAttribute("uname");  
        out.print("Hello "+n);  

        out.close();  
  
                }catch(Exception e){System.out.println(e);}  
    }         

}  

Web.xml

<web-app>  

<servlet>  
<servlet-name>s1</servlet-name>  
<servlet-class>FirstServlet</servlet-class>  
</servlet>   

<servlet-mapping>  
<servlet-name>s1</servlet-name>  
<url-pattern>/servlet1</url-pattern>  
</servlet-mapping>   

<servlet>  
<servlet-name>s2</servlet-name>  
<servlet-class>SecondServlet</servlet-class>  
</servlet>   

<servlet-mapping>  
<servlet-name>s2</servlet-name>  
<url-pattern>/servlet2</url-pattern>  
</servlet-mapping>    

</web-app>

download this example (developed using Myeclipse IDE)

download this example (developed using Eclipse IDE)

download this example (developed using Netbeans IDE)