Java Tutorials

Core Java
JSP
Servlet
JDBC
Hibernate
Struts 1
Struts 2
JSF
Spring
J2EE
J2ME
Web Services
Ajax
Dojo
MySQL
Latest Comments
user understand pr
Struts iterator/se
Java Sms
I will Appreciate
web services
  All Comments...
 

 

 
Struts Tutorials
*Stuts TOC
*Apache Struts Introduction
* Struts Controller
* Struts Action Class
* Struts ActionFrom Class
* Using Struts HTML Tags
*Struts Validator Framework    
*Client Side Address Validation    
*Struts Tiles
*tiles-defs.xml
*Struts DynaActionForm
*Struts File Upload
*Struts DataSource
*AGGREGATING ACTIONS
*Internationalization
Struts Resources
*Struts Books
*Struts Articles
*Struts Frameworks
*Struts IDE
*Struts Alternative
*Struts Links
*Struts Presentations
*Struts Projects
*Struts Software
*Struts Reference
*Struts Resources
*Other Struts Tutorial
Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Have tutorials?
Add your tutorial to our Java Resource and get tons of hits.

We offer free hosting for your tutorials. and exposure for thousands of readers. drop a mail
roseindia_net@yahoo.com
 
   

 
Join For Newsletter

Powered by groups.yahoo.com
Visit Group! Post Questions!

Setting and Getting Data in jsp through the Session

                          

In this example you will learn how to make Form with the help of html in jsp. By this example you can easily learn how we can get data from one page to another page. You can retrieve data from one page to another page only when the session is true . If you disable the session in your code,  some thing like this: <%@ page language="java" session="false"%> when the Session value is false, no one can retrieve data from any Form. Please remember that the default value of the Session is true.

Program Description: 

In this program first page that appears to the user displays name and email which user have to fill. There are two buttons labeled "next" and "reset" to go in the next page and clear the current form entries respectively. In the next page there are three address lines to fill and the "next" button to go in the next page. In the third page a text area is shown, labeled "description" where the user can write whatever he/she wants to describe and one more button, labeled "finish". On clicking the finish button a new page comes where the user is thanked to complete the previous pages. In this page there is a link "click here" to see all the information filled by the user in previous pages.


Code Description:
In the code of the pages we have used getParameter() method to get the parameters from the previous page and setAttribute() of the session object to set the attribute to the session so that we can access these attributes later in the forthcoming pages. These attributes can be used as global which can be used anywhere in the application. To set the attribute we pass the name of attribute as a string by which this attribute will be known in the session and the name of attribute that has been specified in the previous page. To get these attributes from session we use getAttribute() method of the session object and pass the name of attribute which we have specified during setting this attribute in setAttribute(). It returns the attribute in the form of Object so we need to typecast it in the String object. Finally you will get all attributes in the "information.jsp" where you can show them in the required format.


Code for form1.jsp :

<html>
     <body bgcolor="#FFEAF4">
        <center>
        <form action="form2.jsp" method="post">
	   <table border="1" cellpadding="0" cellspacing
="0" height="30%" width="50%">
	       <tr bgcolor="#FBFBFB">
      		   <td><B>Name:</B><br><input type="text"
 name="name" value="" size="20" /><td>
		</tr>
		<tr bgcolor="#FBFBFB">
		   <td><B>Email:</B><br><input type="text"
 name="mail" value="" size="20" /></td>
		</tr>
		<tr bgcolor="#FBFBFB">
		   <td><input type="submit" value="NEXT" 
/><input type="reset" value="Reset" /></td>
		</tr>
	   </table>
	</form>
	</center>
    </body>
</html>

form1.jsp page :


form2.jsp :

<%
String name = request.getParameter("name");
session.setAttribute("theName", name);
String email = request.getParameter("mail");
session.setAttribute("theemail", email);
%>
<html>
  <body bgcolor="#FFEAF4">
    <center>
      <form action="description.jsp" method="post">
    <table border="1" cellpadding="0" cellspacing="0" 
               decolor=""#E2FEFD height="30%" width="50%">
	<tr bgcolor="#FBFBFB">
	<td>
        <B>Address1:</B>
        <input type="text" name="Address1" value="" size="30" />
        </td>
	</tr>

	<tr bgcolor="#FBFBFB">
	<td><B>Address2:</B><input type="text" 
        name="Address2" value="" size="30" /></td>
	</tr>

	<tr bgcolor="#FBFBFB">
	<td><B>Address3:</B><input type="tex
t" name="Address3" value="" size="30" /></td>
	</tr>

	<tr bgcolor="#FBFBFB">
	<td><input type="submit" value="NEXT" /></td>
	</tr>
	
    </table>
     </form>
   </center>
  </body>
</html>
 

form2.jsp page :


description.jsp :

<%
        String address1 = request.getParameter("Address1");
                        session.setAttribute("address1", address1);

       String address2 = request.getParameter("Address2");
                        session.setAttribute("address2", address2);

        String address3 = request.getParameter("Address3");
                        session.setAttribute("address3", address3);

%>

<html>
        <body bgcolor="#FFFFFF">
                   <form action="result.jsp" method="post">
                         <table border="0" cellpadding="0" cellspacing="0" height="30%" width="50%">
                               <tr bgcolor="#FBFBFB">
                                     <td><B>Description:</B><br><textarea name="description" rows="12"                                           cols="50"colspan="2"></textarea>
                                        </td>
                                 </tr>
                               <tr bgcolor="#FBFBFB">
                                   <td colspan="2"><input type="submit" value="Finished" /></td>
                               </tr>
                        </table>
              </form>
       </body>
</html>

description.jsp :


result.jsp :

<%
        String description = request.getParameter("description");
                session.setAttribute("description", description);

                         out.println("Thanks for completing the wizard.");

<%
       String description = request.getParameter("description");
                 session.setAttribute("description", description);

                        out.println("Thanks for completing the wizard.");
%>

                                   Plz, <a href="information.jsp">click here </a>for showing details.


%>

                 Plz, <a href="information.jsp">click here </a>for showing details.






information.jsp :

<%
	out.println("Name : " + (String)session.getAttribute("theName") + "<br/>");
	out.println("Email ID: " + (String)session.getAttribute("theemail") + "<br/>");
	out.println("Address1: " + (String)session.getAttribute("address1") + "<br/>");
	out.println("Address2: " + (String)session.getAttribute("address2") + "<br/>");
	out.println("Address3: " + (String)session.getAttribute("address3") + "<br/>");
	out.println("description: " + (String)session.
getAttribute("description") + "<br/>");
%>

information.jsp page :

                           ]

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

Current Comments

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

Really excellent..hats of to rose ondia

Posted by Prasad on Saturday, 12.1.07 @ 16:32pm | #41027

this finds me very usefull as i was not aware of how to use it.

Posted by vishal on Wednesday, 11.28.07 @ 16:42pm | #40829

Very good tutorials

Posted by firoz on Monday, 08.20.07 @ 12:48pm | #23764

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.

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

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

Copyright © 2008. All rights reserved.