Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
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.

 

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>&nbsp;</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.

                         

» View all related tutorials
Related Tags: c io sed page tag if for example to exam ci e use in m ad age xa xamp s

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

5 comments so far (
post your own) View All Comments Latest 10 Comments:

I tried these samples, but the values of sessions variables are always null.

By the way, me too i remembered these notions of theory, but seems not work...

Posted by Luca on Friday, 08.1.08 @ 17:56pm | #70323

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

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.