Go4Expert

Go4Expert (http://www.go4expert.com/)
-   JSP (http://www.go4expert.com/articles/jsp-tutorials/)
-   -   Working With Sessions in JSP (http://www.go4expert.com/articles/sessions-jsp-t3897/)

Sanskruti 16Apr2007 19:31

Working With Sessions in JSP
 
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

parvez.yu 6Mar2008 17:03

Re: Working With Sessions in JSP
 
i will try the code

kevinthomas12c 14Jul2008 17:45

Re: Working With Sessions in JSP
 
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..

gkumar 24Jun2009 16:39

Re: Working With Sessions in JSP
 
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.

alssadi 19Dec2010 21:18

Re: Working With Sessions in JSP
 
what if i click on back botton i think it will still be logied in ?

rameshb 20Dec2010 14:26

Re: Working With Sessions in JSP
 
great me definitely going to try this code.. thanks alot

seoabhisek 4Feb2011 11:06

Re: Working With Sessions in JSP
 
nice buddy this code is used for making the login form whre we need to use sessions to store user's information.

virender.ets 7Feb2011 16:20

Re: Working With Sessions in JSP
 
Hey buddy,
You are writing code for login but what would happen if click on the back button through browser after login.

tiwvinay 20Jul2011 17:35

Re: Working With Sessions in JSP
 
thanks for give information in jsp


All times are GMT +5.5. The time now is 06:29.