❮ Previous Next ❯

HttpSession

HttpSession is an inbuilt interface present in javax.servlet.http package, which is another technique used for session management. HttpSession object is used to store entire session with a specific client. We can store, retrieve and remove attribute from HttpSession object. Here when a request is coming from client to the main server, then main server forwards the request to the web container, then web container creates a unique session id for request and gives it back to the client with response. This is a temporary session created by web container. The web container uses this id to identify the particular user. The client sends back the session ID with each request.HttpSession object can be used to perform bind objects, view and manipulate information about a session, such as the session identifier, creation time, and last accessed time.

http_session

How to get HttpSession object:

HttpSession session=request.getSession();

When getSession() method invoking by request object, then it returns HttpSession object. If the session already exists then it returns the existing session otherwise it will create a new session.

How to destroy a session:

session.invalidate();

there is a method invalidate() when invoked by the session object, then a session will be destroyed.

Let's see an example of session management using HttpSession interface for better understanding:

Required files:

Index.html

<form action="MyServlet1">
  User Name:<input type="text" name="uname"/><br/>
  Password:<input type="password" name="pass"/><br/>
  <input type="submit" value="submit"/>
</form>

MyServlet1.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet1 extends HttpServlet {
   public void doGet(HttpServletRequest request, HttpServletResponse response){
     try{
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();

      String name = request.getParameter("uname");
      String password = request.getParameter("pass");
      out.print("Hello "+name);
      out.print("Your Password is: "+password);

      HttpSession session=request.getSession();
      session.setAttribute("user",name);
      session.setAttribute("upass",password);

      out.print("<a href='welcome'>view details</a>");
      out.close();
    }catch(Exception exp){
       System.out.println(exp);
     }
  }
}

MyServlet2.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet2 extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response){
  try{
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      HttpSession session=request.getSession(false);
      String myName=(String)session.getAttribute("user");
      String myPass=(String)session.getAttribute("upass");
      out.print("Name: "+myName+" Pass: "+myPass);
      out.close();
  }catch(Exception exp){
      System.out.println(exp);
   }
  }

}
❮ Previous Next ❯

Follow Us





  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved