Page Directive attribute - session


 

Page Directive attribute - session

This tutorial contains description of session attribute of page Directive.

This tutorial contains description of session attribute of page Directive.

Page Directive attribute - session

This tutorial contains description of session attribute of page Directive.

session Attribute :

session attribute is one of attribute of page directives. Its functionality to show whether or not page uses HTTP sessions. The true  value of session attribute indicates that the JSP page has permission to access existing session and false value indicate NO session  will be used automatically. If you try to attempt to access session variable then the servlet will cause RUN-TIME failure. By default value of session attribute is True.

Syntax :

<%@ page session="true/false" %>

Example :

jspPage.jsp -

<html>
<body>

<form method="post" action="sessionPage.jsp" >
<p>Name : <input type="text" name="name" size="20"></p>

<p>Location : <input type="text" name="location" size="20"></p>
<p><input type="submit" value="Submit"></p>
</form>

</body>
</html>

sessionPage.jsp -

<%@page language="java"%>
<%
String name = request.getParameter("name");
session.setAttribute("name", name);
String location = request.getParameter("location");
session.setAttribute("location", location);
%>
<html>

<body>
<a href="sessionAttribute.jsp">Click for session</a>
<a href="jspPage.jsp">Click for input page</a>
<br />
</body>
</html>

sessionAttribute.jsp -

<%@page language="java" session="true" %>
<html>
<body>
<font color="blue" size="5">
Name : <%=session.getAttribute("name") %><br/>
Location: <%=session.getAttribute("location") %>
</font>
</body>
</html>

Output : Here is your output-

First the jspPage.jsp will display -

After filling the text field click on submit button you will get the next page sessionPage.jsp-

If you click on hyperlink "Click for session " you will get the below page. If you click on hyperlink "Click for input page" you will reach on the first page that is jspPage.jsp

Ads