Working With Sessions in JSP

Sanskruti's Avatar author of Working With Sessions in JSP
This is an article on Working With Sessions in JSP in JSP.
Rated 3.00 By 1 users
This article 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 article 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:
HTML Code:
<%@ 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>
</html>
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:
HTML Code:
<%@ 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>
</html>
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:
HTML Code:
<%@ 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>
</html>
The function session.getAttribute("username") is used to retrieve the user name saved in the session
alssadi like this
Ambitious contributor
6Mar2008,17:03   #2
parvez.yu's Avatar
i will try the code
Newbie Member
14Jul2008,17:45   #3
kevinthomas12c's Avatar
i need help with sessions in JSP.. i implemented sessions. and it worked correctely..but i got a problem. every time i logout it successfully logouts but if i press the back button on the menu bar it automatically gets redirected back to the page of the user who just logged out... i would like somebody to help..
pls..
Banned
24Jun2009,16:39   #4
gkumar's Avatar
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:

Code:
<%@ 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:

Code:
<%@ 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:

Code:
<%@ 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.
Banned
19Dec2010,21:18   #5
alssadi's Avatar
what if i click on back botton i think it will still be logied in ?
Go4Expert Member
20Dec2010,14:26   #6
rameshb's Avatar
great me definitely going to try this code.. thanks alot
Banned
4Feb2011,11:06   #7
seoabhisek's Avatar
nice buddy this code is used for making the login form whre we need to use sessions to store user's information.
Banned
7Feb2011,16:20   #8
virender.ets's Avatar
Hey buddy,
You are writing code for login but what would happen if click on the back button through browser after login.
Banned
20Jul2011,17:35   #9
tiwvinay's Avatar
thanks for give information in jsp