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
0
parvez.yu's Avatar, Join Date: Feb 2008
Ambitious contributor
i will try the code
0
kevinthomas12c's Avatar, Join Date: Jul 2008
Newbie Member
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..
0
gkumar's Avatar
Banned
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.
0
alssadi's Avatar
Banned
what if i click on back botton i think it will still be logied in ?
0
rameshb's Avatar, Join Date: Dec 2010
Go4Expert Member
great me definitely going to try this code.. thanks alot
0
seoabhisek's Avatar
Banned
nice buddy this code is used for making the login form whre we need to use sessions to store user's information.
0
virender.ets's Avatar
Banned
Hey buddy,
You are writing code for login but what would happen if click on the back button through browser after login.
0
tiwvinay's Avatar
Banned
thanks for give information in jsp