Home Jsp Working with sessions

Related Tutorials


 
 

Share on Google+Share on Google+

Working with sessions

Advertisement
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

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.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  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.

Posted on: March 13, 2008

Related Tutorials

Discuss: Working with sessions   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
Sagar
July 11, 2011
Help! Not Working

Dear Sir, A Nice Tuorial But Need Some Assistance! My System takes the Username From The Database. Once the Username and Password is Correct, it takes the Page to a SessionSet.jsp which informs the User that the Session is Active. Then there is a link which takes the User to a MainMenu.jsp where i want it to display the Username. I tried the Same Way like you did in showsessionvalue.jsp but not working! Please Help!
Shoubhik
November 15, 2011
JAVA

BEST SITE!
Siva Krishna
August 28, 2012
Jsp Session

Working with jsp i filled jsp form page and i submitted. take the next tab on same browser same form page without filling that form i cam submitted. here i have to display sorry u cant not do this u have already submitted. cam any one tell me how to do this
DMCA.com