Share on Google+Share on Google+

Page Directive attribute - session

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 -


<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>


sessionPage.jsp -

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

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

sessionAttribute.jsp -

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

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


Posted on: November 30, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.