JSP session implicit object

JSP, session is an implicit object of type HttpSession.

The Developer can use this object to set,get or remove attribute.

Using session object you set and get Information about session.

Let’s understand it with Example.

Project Structure

Createsession.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
<%@ page language="java"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
 
<h1>Hi This is Bhavesh Lakhani</h1>
<br>
<%
session.setAttribute("username","blakhani2012");
%>
 
<a href="getsession.jsp">Click here to go to Welcome page</a>
 
</body>
</html>

getsession.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
<%@ page language="java"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
 
String u_name=(String)session.getAttribute("username");
 
out.println("Hello "+u_name);
 
%>
 
</body>
</html>

output:


When you click on hyperlink request is goes to another JSP page where you can get the session which you have created in createsession.jsp

Leave a Reply

Your email address will not be published. Required fields are marked *