|
Beginner’s Guide to Web Development – The Session Object
In prior sections of the tutorial, we have talked about sending and receiving information between JSP pages by passing the information
from one page to the next. Now we are going to talk about storing and retrieving data without explicitly passing the data. This can be
accomplished by using the Session object, which is a built in object of JSP.
For example, let’s say that you have a web site that has a login page. At this page you request the user’s name.
Let’s also say that you want to display this user’s name on each page of your web site. One way to do this would be to pass this name to
each page of the site. This could be done by using an HTML form as we covered earlier. However, this method quickly gets tedious.
A simpler way to display the name would be to store it somewhere that every page has access to. Then, each page could simply request
the name from the storage place. The storage place is the Session object. Here’s an example:
Type the following code into your text editor.
<html>
<head>
<title>Session Example</title>
</head>
<body>
<%
session.setAttribute ("name", "John Doe");
out.print ("Storing John Doe in the session");
%>
<br>
<br>
<a href="session2.jsp">Next Page </a>
</body>
</html>
Save this code as session.jsp in the following directory:
c:\tomcat\webapps\examples\jsp\work\
Also, type this code into your text editor:
<html>
<head>
<title>Session Example 2 </title>
</head>
<body>
<%
String name = (String) session.getAttribute ("name");
out.print ( name );
%>
</body>
</html>
Save this file as session2.jsp in the following directory:
c:\tomcat\webapps\examples\jsp\work\
If your Tomcat server is not running, start it now.
Navigate to the following URL:
http://localhost:8080/examples/jsp/work/session.jsp
That’s how easy it is to store your data in the Session object.
Let’s talk a little about the code.
The only syntax we haven’t been exposed to yet are the operations on the Session object. To store the data in the session, we used the
following line of code:
session.setAttribute ("name", "John Doe");
We can refer to the Session object simply by typing session. This is because the Session object is built in to the JSP page. Every JSP page has
access to this object. The method "setAttribute(String name, Object value) " is the method used to store data in the session.
The String "name" is the descriptor we have chosen for this example. Thus, if we need to retrieve the data we have stored, we will
do it by passing in the value "name". The second piece of information the "setAttribute" method takes is the piece of
data you want to store. In this case, we want to store "John Doe". Notice that the second argument in the setAttribute method is an Object.
This means that you can pass any Java object into this method. Since Strings are objects, and "John Doe" is a String, we meet the
criteria for this argument.
The "getAttribute (String name)" method simply returns the piece of data associated with the "name" String.
Since we named our data "name", we simply call
session.getAttribute ("name");
This call retrieves the value of "name", which is "John Doe". Take note of the following line of
code used in the above example:
String name = (String) session.getAttribute ("name");
Notice the (String) right before the session.getAttribute method call. This is referred to as
a cast. The reason this cast is needed is because the getAttribute method
of the Session object returns a data type of the form "Object". However, our name variable is of the type "String". Thus,
we need to put the cast in our code so we can force the "getAttribute" return type into a String. We will see more examples
of casting later on in the sample application.
Storing data in the Session object is a good way to store data that needs to be access across multiple pages and won’t be changed frequently.
However, there are some things you need to be aware of when using the Session object. There is a timeout associated with each Session.
That means that if the user is inactive for a specified period of time, usually around 30 minutes, the information you have stored
in the session could be lost. Also, storing too much information in the session could have performance effects on your server depending
on how your server implements the Session object.
This concludes our discussion of the Session object. Let's move on to the next part of the tutorial, which covers JSP syntax.
|