Followers

JSP: Session tracking using HttpSession


The following example demonstrate how to use HttpSession tracking in servlet application.

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>


index.html
=======

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

FirstServlet.java
===========

import java.io.*;
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 name=request.getParameter("userName");
                   out.print("Welcome "+name);
                   out.println("<br /><br />");
                   HttpSession session=request.getSession();
                   /*
                   Where getSession() is the method of HttpServletRequest interface that will create an HttSession interface with respect to the address details of the client which is available in the request object. So the signature of the method is
                  
                   public HttpSession getSession();
                  
                   If the session  object is already created for the requested client then it will return the reference of the existing object.
                   The server will assign the unique id number for every session object
                   The server will maintain the references of all the session objects based on the address details of the requested clients.
                   All the session objects are automatically destroyed when we shut down the server.
                  
                   */
        out.println("You are session id is "+session.getId());
                   /*
                   Where getId() is the method of HttpSession interface that will return the Id number of the session object as a String.
                   public String getId();
                  
                   */
                   session.setAttribute("uname",name);
                   /*
                   The will store the name value of the client into the session object with attribute name as uname.
                   */
        //session.invalidate();
        out.println("<br /><br />");              
                   out.print("<a href='servlet2'>See my details here</a>");                                    
                   out.close();
                }catch(Exception e){System.out.println(e);}
          }
}

SecondServlet.java
=================

import java.io.*;
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);
                   /*
                   This will return the reference of the session object which is already created for the requested client based on the address details of the requested client.                 
                   */
        out.println("You are session id is "+session.getId());
        out.println("<br />");
                   String n=(String)session.getAttribute("uname");
                   out.print("Hello "+n);
                   out.close();
                }catch(Exception e){System.out.println(e);}
          }
}








No comments:

Post a Comment