The HttpSession Interface in Servlet :
In web terminology, a session is simply the limited interval of time in which
two systems communicate with each other.
The two systems can share a client-server or a peer-to-peer relationship.
However, in Http protocol, the state of the communication is not
maintained.
Hence, the web applications that work on http protocol use several
different technologies that comprise Session Tracking, which means
maintaining the state (data) of the user, in order to recognize him/her.
In order to achieve session tracking in servlets, cookies have been one of
the most commonly used tech. However, they have the following
disadvantages:
They can only keep textual information.
They’re browser dependent. Hence, if the client disables them, your
web application can’t make use of them
Individual cookie can contain not more than 4kb of information
How to create sessions with a unique session id for each user in java
servlet
For this, servlets provide an interface called ‘HttpSession’ Interface. The
following diagram explains how Http Sessions work in servlets:
Advantages of Http Sessions in Servlet
Any kind of object can be stored into a session, be it a text, database,
dataset etc.
Usage of sessions is not dependent on the client’s browser.
Sessions are secure and transparent
Example of Session tracking using HttpServlet Interface:
Index.html file
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method="Post" action="login">
Name:<input type="text" name="name"><br>
<input type="submit" value="Login">
</form>
</body>
</html>
LoginServlet.java File
package b.a.w;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import
jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import
jakarta.servlet.http.HttpServletRequest;
import
jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
@WebServlet("/login")
public class LoginServlet extends
HttpServlet{
@Override
protected void doPost(HttpServletRequest
req, HttpServletResponse resp) throws
ServletException, IOException {
resp.setContentType("text/html");
PrintWriter po=resp.getWriter();
String name=req.getParameter("name");
po.println("Hello "+name);
HttpSession htp=req.getSession();
htp.setAttribute("savedName", name);
po.println("<a href='home'>Go To Home
Page!!</a>");
}
}
HomeServlet.java File
package b.a.w;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import
jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import
jakarta.servlet.http.HttpServletRequest;
import
jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
@WebServlet("/home")
public class HomeServlet extends
HttpServlet{
@Override
protected void doGet(HttpServletRequest
req, HttpServletResponse resp) throws
ServletException, IOException {
// TODO Auto-generated method stub
resp.setContentType("text/html");
PrintWriter po=resp.getWriter();
HttpSession htp=req.getSession();
String sname=(String)
htp.getAttribute("savedName");
po.println("Welcome.."+sname+" You are on
HomePage!!");
po.println("<a href='product'>Go to
Product Page!!</a>");
}
}
ProductServlet.java file
package b.a.w;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import
jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import
jakarta.servlet.http.HttpServletRequest;
import
jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
@WebServlet("/product")
public class ProductServlet extends
HttpServlet {
@Override
protected void doGet(HttpServletRequest
req, HttpServletResponse resp) throws
ServletException, IOException {
resp.setContentType("text/html");
PrintWriter po=resp.getWriter();
HttpSession htp=req.getSession();
String sname=(String)
htp.getAttribute("savedName");
po.println("Welcome back !!"+sname+" You
are on product Page!!");
}
}