Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Working with sessions
This JSP Tutorial shows you how to track the session between different JSP pages. In any web application user moves from one page to another and it becomes necessary to track the user data and objects throughout the application. JSP provide an implicit ob
 
Search Tutorials:
 
Software Solutions and Services
 

 
Google Custom Search:
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation

Working with sessions

                          

This JSP Tutorial shows you how to track the session between different JSP pages. In any web application user moves from one page to another and it becomes necessary to track the user data and objects throughout the application. JSP provide an implicit object "session", which can be use to save the data specific the particular to the user.

In this tutorial we will create an application that takes the user name from the user and then saves into the user session. We will display the saved data to the user in another page.

Here is the code of the JSP file (savenameform.jsp) that takes the input from user:

<%@ page language="java" %>
<html>
<head>
<title>Name Input Form</title>
</head>
<body>
<form method="post" action="savenametosession.jsp">
<p><b>Enter Your Name: </b><input type="text" name="username"><br>
<input type="submit" value="Submit">

</form>

</body>

 The above page prompts the user to enter his/her name. Once the user clicks on the submit button, savenametosession.jsp is called. The JSP savenametosession.jsp retrieves the user name from request attributes and saves into the user session using the function session.setAttribute("username",username);. Here is the code of savenametosession.jsp:

<%@ page language="java" %>
<%
String username=request.getParameter("username");
if(username==null) username="";

session.setAttribute("username",username);
%>

<html>
<head>
<title>Name Saved</title>
</head>
<body>
<p><a href="showsessionvalue.jsp">Next Page to view the session value</a><p>

</body>

 The above JSP saves the user name into the session object and displays a link to next pages (showsessionvalue.jsp). When user clicks on the "Next Page to view session value" link, the JSP page showsessionvalue.jsp displays the user name to the user. Here is the code of showsessionvalue.jsp:

<%@ page language="java" %>
<%
String username=(String) session.getAttribute("username");
if(username==null) username="";
%>
<html>
<head>
<title>Show Saved Name</title>
</head>
<body>
<p>Welcome: <%=username%><p>

</body>

The function session.getAttribute("username") is used to retrieve the user name saved in the session.

                          

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

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

Hi, this is vishal....
I like your site because every example is given in very simple language...
Thank you for creating this site....

Posted by Vishal on Friday, 04.10.09 @ 12:47pm | #86704

Try to illustrate some effective and more no of examples.

Posted by karthick on Wednesday, 12.10.08 @ 01:16am | #82585

It is good...

Posted by sadik on Monday, 12.1.08 @ 04:25am | #82227

I want a code for logout from the session
i am using jsp..

Posted by Dhiraj Kumar on Saturday, 07.19.08 @ 12:29pm | #68049

how to run program

Posted by kalyan on Wednesday, 06.18.08 @ 16:23pm | #63730

how can i implement session in jsp

Posted by RAJ on Sunday, 06.1.08 @ 13:07pm | #61688

if any one know about how to access the lotus mail inbox from jsp and using jsp how to store the password to access database with encrypted format.

Posted by manikandan on Saturday, 02.9.08 @ 16:58pm | #47696

Hi,
I am using session to get the username throught out the pages .But I want to check the same when each page loads .I tried using if condition but its not working.This is important since there are two kind of users in my application and one shouldn't get another seesion

Posted by Thomas on Wednesday, 01.9.08 @ 15:09pm | #44844

jsp session meaning

Posted by mahe on Thursday, 01.3.08 @ 15:52pm | #44431

hello all of you

Posted by mukesh on Wednesday, 01.2.08 @ 23:49pm | #44388

Training Courses
Tell A Friend
Your Friend Name
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.