# HttpSession

In 
Published 2022-12-03

This tutorial explains to you what a HttpSession object is in Java.

The HttpSession object is used for session management. A session contains information specific to a particular user across the whole application. Session in Java Servlet are managed through different ways, such as Cookies, HttpSession API, URL rewriting, etc.

Here is an example of using HttpSession object.

For this example I will create a Dynamic Web Application using Eclipse. This Web Application will have 2 servlets:

  • MyJavaServlet : will set a parameter on the session level
  • MyJavaServlet2 : will read that parameter (on the session level)
MyJavaServlet.java
package servlets;
 
import java.io.IOException;
import java.io.PrintWriter;
 
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 MyJavaServlet
 */
@WebServlet("/MyJavaServlet")
public class MyJavaServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
 
    public MyJavaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
 
        HttpSession session=request.getSession();  
        session.setAttribute("user_name", "Paul");  
         
        PrintWriter myResponse = response.getWriter();
        String message = "This servlet set a session variable using HttpSession object. ";
             
     
        myResponse.println( "\n" +"<title>This servlet set a session variable using HttpSession object.</title>\n" +
             "\n" +
             "<h2 align="\"center\"">" + message + "</h2>\n" +
             "<p>   </p>  <p>Link to MyJavaServlet2 servlet page : "+
             "  <a href="http://localhost:8080/JAVA_servlet_example/MyJavaServlet2">link</a></p>"+
             "");
         
         
    }
}
package servlets;
 
import java.io.IOException;
import java.io.PrintWriter;
 
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 MyJavaServlet
 */
@WebServlet("/MyJavaServlet2")
public class MyJavaServlet2 extends HttpServlet {
    private static final long serialVersionUID = 1L;
        
    public MyJavaServlet2() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
 
        HttpSession session=request.getSession();  
        String uname = (String) session.getAttribute("user_name");  
         
        PrintWriter myResponse = response.getWriter();
        String message = "This servlet read a session variable using HttpSession object. ";
             
     
        myResponse.println( "\n" +"<title>This servlet read a session variable using HttpSession object.</title>\n" +
             "\n" +
             "<h2 align="\"center\"">" + message + "</h2>\n" +
             "<p>   </p>  <p> The User Name read from the HttpSession object is "+ uname+ ".</p>"+
             "");
    }
}

The following screen is the result of MyJavaServlet servlet:

The following screen is the result of MyJavaServlet2 servlet:

NOTE:

  1. If you have an architecture with 2 or more web servers in a cluster (not only 1 web container), the values stored in the session object of one server (container), might not be available in the session object on the other server (container). This is because a web container can not see the attributes from another (remote) container (which run on another JVM instance).

  2. To avoid this problem, you have to :

  • store the session variable into a database
  • use sticky session : the user requests will be sent to the same container (server) for the same session