Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Session tracking in servlets by using HttpSession

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 459
    Comment on it

    HttpSession Interface

    It is one of the session tracking techniques.
    It basically creates a session for each user and is used to maintain the request data and time interval of a particular. A user may give more than one requests at a particular time, so all the requests should be maintained under a session.i.e previous request should not be destroyed when new request is generated.

    It creates a session Id for each user which serves as the identity of that particular user.
    A session object is created each id and it is destroyed after the session so that another user is not able to access that data.

    alt text

    Getting HttpSession object:

    public HttpSession getSession(): It is used to get the current session for a particular request or it creates a new session if session doesn't exist.

    Some commonly used methods for this are:

    • public String getId(): It returns the unique id associated with a session.
    • public void invalidate(): It is used to finish the session by destroying the session object.

    Example:

    index.html

    <form action="demo1">  
    User Name<input type="text" name="uname"/><br/>  
    <input type="submit" value="submit"/>  
    </form>  
    

    Demo1.java (Servlet 1)

    import java.io.*;  
    import javax.servlet.*;  
    import javax.servlet.http.*;  
    
    
    public class Demo1 extends HttpServlet {  
    
    public void doGet(HttpServletRequest request, HttpServletResponse response){  
           try{                  
             response.setContentType("text/html");  
             PrintWriter out = response.getWriter();   
            String name=request.getParameter("uname");              
            HttpSession session=request.getSession();  
            session.setAttribute("username",name);  
    
            out.print("<html><body><a href='demo2'>go</a></body></html>");  
    
            out.close();  
    
                    }catch(Exception e){System.out.println(e);}  
        }  
    
    }  
    

    Demo2.java( Second servlet)

    import java.io.*;  
    import javax.servlet.*;  
    import javax.servlet.http.*;  
    
    public class Demo2 extends HttpServlet {  
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)  
            try{  
    
            response.setContentType("text/html");  
            PrintWriter out = response.getWriter();  
    
            HttpSession session=request.getSession();  
            String name=(String)session.getAttribute("username");  
            out.print("Welcome "+name);  
    
            out.close();  
    
                    }catch(Exception e){System.out.println(e);}  
        }  
    
    
    }  
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: