The session Attribute of page Directive In JSP

This section provides you the best illustration of the session
attribute of the page directive in JSP. This is the boolean attribute of
the directive. It sets a boolean value
either true or false. If the value of session attribute is true
then the session object refers to the current or a new session because the
client must be in the HTTP session for running the JSP page on the server. If
you set the value of session object false then you can not use the
session object or <jsp:useBean> element with scope="session" in
JSP page. And then if a type of error occurs i.e. called the translation-time
error. The default value of session attribute is true.
There are four pages have been provided for
understanding the session attribute of the page directive of JSP. These
are:
- sessionForm.jsp
- session.jsp
- sessionresult.jsp
- ShowFalseSession.jsp
First you have to run the sessionForm.jsp page on the
server and get the value of username field and the password field if any and set
the session objects (username and password) with the retrieved value of those
and show the session.jsp page with Welcome message with the username. This page
retrieves the value of username and the password field by using the request.getParameter()
method and set these values to the session object that is retrieved later
in the sessionresult.jsp page for showing the welcome message with username by
getting the session value. The sessionresult.jsp page contains the page
directive which attributed session has been set the true value for
the session operation. But when you click on the another link ("Next Page
with session false.") made on the session.jsp page then the
ShowFalseSession.jsp page will be seen with the message "The value of
session object is false!" because when the value of the session attribute
of the page directive is false then the page does not support any type of
session operations in the page.
Here is the code of the sessionForm.jsp page:
<html>
<head><title>Disable Session Environment.</title></head>
<body>
<form action="session.jsp" method="post">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>User Name: </td>
<td><input type="text" size="20" name="txtUserName" />
</tr>
<tr>
<td>Password: </td>
<td><input type="password" size="20" name="txtPassword" />
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Submit" name="B1" /></td>
</tr>
</table>
</form>
</body>
</html>
|
Output for the sessionForm.jsp page:

Here is the code of the session.jsp page:
<%@page language="java" %>
<%
String userName = request.getParameter("txtUserName");
String password = request.getParameter("txtPassword");
if(userName == null)
userName = "";
if(password == null)
password = "";
if(!userName.equals("") && !password.equals("")){
session.setAttribute("SessionUser", userName);
session.setAttribute("SessionPassword", password);
out.println("Welcome " + userName + "!");
out.println("<br/><a href=sessionresult.jsp>
Next Page with session true.</a>");
out.println("<br/><a href=ShowFalseSession.jsp>
Next Page with session false.</a>");
}
else if(userName.equals("")){
out.println("<font color=red><b>User name required.</b></font>");
out.println("<br/><a href=sessionForm.jsp>Go back!</a>");
}
else if(password.equals("")){
out.println("<font color=red><b>Password required.</b></font>");
out.println("<br/><a href=sessionForm.jsp>Go back!</a>");
}
%>
|
Output for the session.jsp page:

Above image shows the output when you submit the form
in the sessionForm.jsp page without entering the username and if you do not
enter the password and submit the form then the output will be seen as:

If you click on the link ("Go back") then the
sessionForm.jsp will be loaded. If you enter the username and password then the
session.jsp page will be seen as:

In the above output if you click on the first link
("Next Page with session true.") then the sessionresult.jsp page will
be opened otherwise the ShowFalseSession.jsp page will be seen if you click on
the another link ("Next Page with session false.").
Here is the code of the sessionresult.jsp page:
<%@page language="java" session="true" %>
<%
String username = (String)session.getAttribute("SessionUser");
String password = (String)session.getAttribute("SessionPassword");
out.println("<b>Welcome " + username + "!</b>");
%>
|
Output for the sessionresult.jsp page:

Here is the code of the ShowFalseSession.jsp page:
<%@page language="java" session="false" %>
<%
out.println("The value of session attribute is false!");
%>
|
Output for the ShowFalseSession.jsp page:

Download sessionForm.jsp
file.
Download session.jsp
file.
Download sessionresult.jsp file.
Download ShowFalseSession.jsp file.

|
Current Comments
4 comments so far (post your own) View All Comments Latest 10 Comments:good and very clear example..its very easy to understand.. thanks rose india..
Posted by hidash on Tuesday, 04.17.07 @ 10:40am | #14408
Very nice roseIndia always have good examples to help us, thanks
Posted by mopont on Thursday, 03.15.07 @ 02:10am | #11735
excellent
Posted by Bhanu on Thursday, 03.8.07 @ 11:57am | #11106
I want to update the values in the page which comes third place.By using session can i do so
Posted by Lukesh Bundele on Tuesday, 02.20.07 @ 19:00pm | #8518