The HttpSession Interface in Servlet

Code for Creating an User Session and accessing the methods of HttpSession Object


<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="login" method="post">
<label>Enter your User Name</label>
<input type = "text" name="username" required> <br>
<label>Enter your User Password</label>
<input type="password" name="password" required><br>
<input type ="submit" name="submit" value="submit">
</form>
</body>
</html>

Login Validation Servlet Code

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
* Servlet implementation class Logincheck
*/
@WebServlet("/Logincheck")
public class Logincheck extends HttpServlet {
private static final long serialVersionUID = 1L;
      
  /**
    * @see HttpServlet#HttpServlet()
    */
  public Logincheck() {
      super();
      // TODO Auto-generated constructor stub
  }

/**
  * @see Servlet#init(ServletConfig)
  */
public void init(ServletConfig config) throws ServletException {
  // TODO Auto-generated method stub
}

/**
  * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
  */
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  doPost(request,response);
}

/**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  response.getWriter().append("Served at: ").append(request.getContextPath());
}

/**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  response.setContentType("text/html");
  String inputname = request.getParameter("username");
  String inputpassword = request.getParameter("password");
  PrintWriter out = response.getWriter();
  if(inputname.startsWith("S") && inputpassword.startsWith("a"))
  {
    HttpSession user = request.getSession();
    user.setAttribute("username", inputname);
    /*out.println("Your Session id" +user.getId());
    out.println("Your Session active Interval"+ user.getMaxInactiveInterval());
    out.println("Your Session Creation Time" +user.getCreationTime());
    out.println("Your Session last accessed time" +user.getLastAccessedTime());*/
    out.println("Welcome Dr. "+user.getAttribute("username")+" "+"<a href='ClasstimingsServlet'>Check Class Timings</a>");
    out.println("<a href='LogoutServlet'>Logout</a>");
  }
  else
  {
    out.println("Check your Username/Password");
  }
  
}
} 

Demo Code used in the video lecture


  Classtimings Display Servlet 
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
  * Servlet implementation class ClasstimingsServlet
  */
@WebServlet("/ClasstimingsServlet")
public class ClasstimingsServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
        
    /**
      * @see HttpServlet#HttpServlet()
      */
    public ClasstimingsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

  /**
    * @see Servlet#init(ServletConfig)
    */
  public void init(ServletConfig config) throws ServletException {
    // TODO Auto-generated method stub
  }

  /**
    * @see Servlet#destroy()
    */
  public void destroy() {
    // TODO Auto-generated method stub
  }

  /**
    * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
    */
  protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doPost(request, response);
  }

  /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.getWriter().append("Served at: ").append(request.getContextPath());
  }

  /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.setContentType("text/html");
    HttpSession user = request.getSession();
    PrintWriter out = response.getWriter();
    out.println("Welcome Dr. "+user.getAttribute("username"));
    if(user.getAttribute("username").equals("Sam"))
    {
      out.println("Your class timings are Monday 11 to 11:50 AM");
    }else if(user.getAttribute("username").equals("Satish")) 
    {
      out.println("Your class timings are Tuesday 1 to 2:30 PM");
    }
    else
    {
      out.println("You have no classes .. just vacation");
    }
    out.println("Logout");
    }

}

Logout Servlet 
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
  * Servlet implementation class LogoutServlet
  */
@WebServlet("/LogoutServlet")
public class LogoutServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
        
    /**
      * @see HttpServlet#HttpServlet()
      */
    public LogoutServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

  /**
    * @see Servlet#init(ServletConfig)
    */
  public void init(ServletConfig config) throws ServletException {
    // TODO Auto-generated method stub
  }

  /**
    * @see Servlet#destroy()
    */
  public void destroy() {
    // TODO Auto-generated method stub
  }

  /**
    * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
    */
  protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doPost(request, response);
  }

  /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.getWriter().append("Served at: ").append(request.getContextPath());
  }

  /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    HttpSession user = request.getSession();
    user.invalidate();
    PrintWriter out = response.getWriter();
    out.println("You are Logged Out. Thanks for using our software");
  }
}