
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.

Current Comments
17 comments so far (post your own) View All Comments Latest 10 Comments: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
how to send a list of random nos stored in an array through a session??plz help me..
Posted by souvik on Thursday, 11.15.07 @ 23:22pm | #37521
sir how can we manage session in JSP and how can we verify that the user is in the current session or not??????????????
Posted by Bushra Amin on Wednesday, 11.7.07 @ 16:02pm | #36296
Hi
I work with sessions and want to know how i can code te logout out of a session.I have to put it behind a button!
Can anyone help me with this?
Wim
Posted by Wim on Wednesday, 10.17.07 @ 19:21pm | #34286