In JSP, session attribute of page directive specifies if the current JSP page takes part in the current HTTP session or not.
Attribute session may either take the boolean value true or false.
Syntax of session attribute
<%@ page session = "true|false" %>
session = "true"
When session attribute of page directive is set to true, the webpage participates in the current existing
HTTP session by using the built-in session object of type HTTPSession. But if there is no existing session then a new session is created.
<%@ page session= "true" %>
Note : By default, session attribute is set to true.
Session.jsp
<html>
<head>
<title>JSP session attribute</title>
</head>
<body>
<%@page import="java.util.*" session ="true" %>
<h2>Creating a session</h2> <br/>
Session ID : <%= session.getId() %> <br/>
Session creation time : <%= new Date(session.getCreationTime()) %>
</body>
</html>
Executing Session.jsp created a new session and its information will be displayed to the user.
Advertisement
session = "false"
When session attribute is set to false, the current page does not participates in the current existing
HTTP session and accessing the session object of HTTPSession class gives an error. Let's see how.
<%@ page session= "false" %>
Session2.jsp
<html>
<head>
<title>JSP session attribute</title>
</head>
<body>
<%@page import="java.util.*" session ="false" %>
<h2>Accessing a disabled session</h2> <br/>
Session ID : <%= session.getId() %> <br/>
Session creation time : <%= new Date(session.getCreationTime()) %>
</body>
</html>
Executing Session2.jsp gives us an JSP error because we are trying to gather the information of the current session using the built-in session object of HTTPSession class(whose value is not set)
because the current JSP hasn't participated in any session, as its session attribute was set to false.