Updated March 28, 2023
Introduction to JSP Session
Jsp is the java server page; it helps to create dynamic web pages. The jsp has the session technique to handle the sessions in web-based applications jsp sessions have their own instance javax.servlet.http.HttpSession it has the html front end codes to connect with the back end controller logic codes and also connect with the database queries for each time the queries will be executed for every client request the session id will be created; the session is used for add the sessions, remove the sessions and the attributes we can get the user request pieces of information with the help of logs.
Syntax
The basic syntax of the jsp used session is below.
<html>
<body>
<%
---------some jsp logic codes----
session.setAttribute();//session instance created and call the sessions methods
session.getAttribute();();//session instance created and call the sessions methods
%>
</body>
</html>
The above codes are used to create the sessions in the jsp pages; whenever we access the applications, the user session will be stored; we can access and retrieve the information using session-id in the database.
Where to Use JSP Session?
The http is classified into two stateful and stateless protocols; in that stateless protocol, the user sends the request to the server, and the server sents the response to the client after retrieving the response in the web page after it connects to the stateless protocol client creates and opens a new connection to the web servers and also the server did not store any previous request datas in the client history. In that time, jsp used the concept like the session tracking mechanism it is used to maintain the state regarding the single user continuous request across some time durations.
Http is the stateless protocol, so the single user or multiple users sent the continuous request to the server, and the server does not know which client sent which request, so it will maintain the conversational state that time session tracking is needed for the jsp online shopping is the basic example the user will add the keep on items to their own carts the server identified using the session tracking mechanism which user will add the items to the carts.
There are different ways to handle sessions for both web clients and web servers. There are some different methods to track the sessions like cookies,url rewriting, hidden fields, and session API. Cookies are used for session tracking is the key-value pair related information like java hash map whenever the client sent a request to the server also along with the cookies it helps to identify the clients.url rewriting is identified by the session ids in the url using get methods. Finally, the url request itself identified the request using session identifiers, and also it appends the data to the clients.
Html forms is one of the ways to identified the values using https methods like the get and post the request and response datas these also used to store the user session information in the hidden form fields also the concept like dynamically generated the web pages since the whole process of each user session has the unique identifiers. In the session object, the servlet will be used to provide the HTTP session interface for handling the web client request and server response to the user. Regarding jsp, the default session is created automatically, and it does not exist to another, so if we created a jsp page and executed the session is already created for the initial request.
We can also set the session settings in the jsp because it will validate the sessions. So before validation, it checks the existing sessions of the jsp user requests and also its need to use the session attributes method like setAttributes() we can pass the parameters like attribute name and values in session methods we can pass the different types of attributes in the method. We can retrieve the values from the session attribute using session.getAttribute().
Examples of JSP Session
Different examples are mentioned below:
Example #1
Code:
<!DOCTYPE html>
<html>
<head>
<title>Sample</title>
</head>
<body>
<form action="//first.jsp">
<input type="text" placeholder="Name" name="name"><br><br>
<input type="text" placeholder="City" name="city"><br><br>
<input type="submit" value="submit"><br>
</form>
</body>
</html>
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPEhtml>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Sample</title>
</head>
<body>
<%
String name=request.getParameter("name");
String city=request.getParameter("city");
session.setAttribute("name",name);
session.setAttribute("city",city);
out.print("Welcome"+name);
out.print("<br>");
out.print("Session created successfully.");
%>
<br><a href="//WEB-INF//second.jsp">Click</a>
</body>
</html>
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Sample Sessions </title>
</head>
<body>
<%
String name=(String)session.getAttribute("name");
String city=(String)session.getAttribute("city");
out.print("Name :"+name);
out.print("<br>");
out.print("City :"+city);
%>
</body>
</html>
Sample Output:
Example #2
Code:
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.io.*,java.util.*"%>
<%
Date startdate = new Date(session.getCreationTime());
Date lastdate = new Date(session.getLastAccessedTime());
String s = "Welcome Back to my domain";
Integer i = new Integer(0);
String v = new String("lasytvisittimes");
String u = new String("id");
String u1 = new String("welcome");
if (session.isNew() ){
s = "Welcome to my domain";
session.setAttribute(u, u1);
session.setAttribute(v, i);
}
i = (Integer)session.getAttribute(v);
v = v + 1;
u1 = (String)session.getAttribute(u);
session.setAttribute(v, i);
%>
<html>
<head>
<title>Session Tracking</title>
</head>
<body>
<center>
<h1>Session Tracking</h1>
</center>
<table border="10" align="center">
<tr bgcolor="green">
<th>Session informations</th>
<th>Values</th>
</tr>
<tr>
<td>id</td>
<td><% out.print( session.getId()); %></td>
</tr>
<tr>
<td>CreateTime</td>
<td><% out.print(startdate); %></td>
</tr>
<tr>
<td>Lasttime</td>
<td><% out.print(lastdate); %></td>
</tr>
<tr>
<td>ID</td>
<td><% out.print(u1); %></td>
</tr>
<tr>
<td>visit</td>
<td><% out.print(i); %></td>
</tr>
</table>
</body>
</html>
Sample Output:
Example #3
Code:
<!DOCTYPE html>
<html>
<head>
<title>Sample</title>
</head>
<body>
<form action="//samplesss//jsp//first.jsp">
<input type="text" placeholder="Name" name="name"><br><br>
<input type="text" placeholder="City" name="city"><br><br>
<input type="submit" value="submit"><br>
</form>
</body>
</html>
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="javax.servlet.*"%>
<%@page import="javax.servlet.http.*"%>
<%@page import="java.sql.*"%>
<!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<%
Cookie name = new Cookie("name",
request.getParameter("name"));
Cookie city = new Cookie("city",
request.getParameter("city"));
name.setMaxAge(23*20*16);
city.setMaxAge(23*20*16);
response.addCookie(name);
response.addCookie(city);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Sample</title>
</head>
<body>
<b>Name:</b>
<%= request.getParameter("name")%>
<b>City:</b>
<%= request.getParameter("city")%>
</body>
</html>
Sample Output:
Conclusion
JSP has a different set of features while handling the user details in logs as well as browser console techniques like sessions, cookies, etc. Among these sessions is used for retrieves user datas with the help of session ids.
Recommended Articles
This is a guide to JSP Session. Here we discuss the Examples of the JSP Session and Where to use this session, along with the basic syntax of the used session. You may also have a look at the following articles to learn more –