HttpSession


Prev Tutorial Next Tutorial

HttpSession

HttpSession is another kind of session management technique, In this technique create a session object at server side for each client.

Session is available until the session time out, until the client log out. The default session time is 30 minutes and can configure explicit session time in web.xml file.

Configure session time in web.xml

Example

<web-app>  
<session-config>  
<session-timeout>40</session-timeout>  
</session-config>  
</web-app>  

HttpSession Api

Http session is an interface define in java.http package.

Getting session object

Example

HttpSession hs=req.getSession();  // create new session object

Methods of HttpSession interface

MethodDescription
public HttpSession getSession():It returns the current session associated with this request, or if the request does not have a session, creates one.
public HttpSession getSession(boolean create)It returns the current HttpSession associated with this request or, if there is no current session and create is true, returns a new session.
public String getId()It returns a string containing the unique identifier value.
public long getCreationTime()It returns the time when this session was created, measured in milliseconds since midnight January 1, 1970 GMT.
public long getLastAccessedTime()It returns the last time the client sent a request associated with this session, as the number of milliseconds since midnight January 1, 1970 GMT.
public void invalidate()Invalidates this session then unbinds any objects bound to it.

Dis-Advantage of HttpSession

  • Http session objects allocate memory on the server so this increase burden on the server.
  • If cookies are restricted coming to browser window this technique fails to perform session tracking.

Example of session tracking by using httpsession

index.html

<form action="servlet1">
Name:<input type="text" name="userName"/> <br/>
<input type="submit" value="continue"/>
</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>  

Prev Tutorial Next Tutorial

Advertisements




Facebook Likes

Advertisements