Maintaining Persistence Using Session Variables

 

Introduction.  It shouldn't surprise you that many websites require many more than a single or a few JSP pages to do their work.  For example, think of some of the general functionality available in most e-commerce stores.
  • browsing products
  • developing a shopping basket
    • adding items
    • deleting items
    • updating items
    • emptying the basket
  • maintaining a shopper profile
    • accessing the profile
    • updating the profile
    • integrating the profile into the current shopping experience
  • checking out
    • obtaining shipping information
    • obtaining billing information
    • obtaining payment information
    • verifying inputs

Obviously this list can go on and on.  But so far we have had a variety of variables, for example

  • variables local to an entire page
  • variables local to a segment of a page
  • values requested from a posting form page
  • instance variables available to all users running a page

Obviously, there will need to be other sorts of variables/information that can be used in something like an e-commerce store.

Now we need to be able to develop variables that are local to each user, but available to all of the pages during a particular session.  These are called Session variables in JSP, ASP and PHP.

The session object in Java is an instance of HTTPSession.  The three methods that are used most often are displayed in the following table.

 

Method Exception
public void setAttribute(String name, Object value) throws IllegalStateException
public Object getAttribute(String name) throws IllegalStateException
public void removeAttribute(String name, Object value) throws IllegalStateException

 

For example, in order to assign a session variable you would use something like the following.

session.setAttribute("identifier_key", "some sort of value to be assigned")

In order to retrieve this session variable later in the code you would use something like the following.

String retrieved_value = (String) session.getAttribute("identifier_key")

Now we will develop an series of pages that show how local variables exist only within restrictive scriptlets or pages, posted information exists only for the subsequent pages and how session variables persist for a session.  The series of pages will obtain some information, such as a username, that will persist for the duration of the session and be reported back to the user on each page.  Other types of information can't be accessed on every page.

You should call this first JSP page login_knower.html.

 

<html>
<head>
<title>A Form to Get User Information</title>
</head>

<body>
<h2>Please enter your information:</h2>
<form method="post" action="login_knower.jsp">
<table>
<tr>
<td>Username:
</td>
<td><input type="text" name="txt_username" size=15>
</td>
</tr>
<tr>
<td>Password:
</td>
<td><input type="password" name="txt_password" size=15>
</td>
</tr>
<tr>
<td colspan=2><input type="submit" value="Submit!">
</td>
</tr>
</table>
</form>
</body>
</html>

 

After copying this code and uploading it and accessing it on your web you should see something like the following.

 

 

You will need to upload the following page before pressing the submit button.

You should call this page login_knower.jsp.

 

<html>
<head>
<title>A Login Page that Displays the User's Name</title>
</head>
<body>
<%
// obtaining the login information
String userName = request.getParameter("txt_username");
String password = request.getParameter("txt_password");
// setting a session variable
session.setAttribute("username", userName);
%>
<h2>Welcome <%=userName%>!</h2>
<form method="post" action="login_knower_next.jsp">
<input type="submit" value="On To Next">
</form>
</body>
</html>

 

After copying this code and uploading it and accessing it on your web by pressing on the submit button on login_knower.html, you should see something like the following.

 

 

Before pressing this page's submit button you need to copy and upload the following page which you should call login_knower_next.jsp.

 

<html>
<head>
<title>A Login Page that Displays the User's Name</title>
</head>
<body>
<%
// setting a session variable
String sessionUserName = (String) session.getAttribute("username");
%>
<P>
<font size=4>Trying the session variable from the last page.</font>
<h2>Welcome <%=sessionUserName%>!</h2>
</P>
</body>
</html>

 

This just displays the session variable for the username on a subsequent page.