Sessions
- HttpSession interface provides the JSP with the ability to store
and
share information between jsp files for an ongoing interaction with a
user.
- Every distinct user who starts and interaction with any JSP of an
application
is associated with a unique sesssion object.
- The session id is stored as a cookie on the user's machine. This
cookie
allows the server to associate a user with an active session.
- Sessions have lifetimes and will expire after a period of
inactivity.
- The lifetime can be set and accessed by methods of the session
object
- setMaxInactiveInterval(int) - sets the maximum time a session
can
be inactive before becoming invalid (in seconds)
- getMaxInactiveInterval() - retrieves this maximum inactiver
interval
(in seconds)
- Sessions can be used to store attribues (objects) that should
survive
for the lifetime of the session
- void setAttribute(String name, Object obj) - stores an objects
as
a session attribute
- Object getAttribute(String name) - retrieves the attribute.
- these objects are usually instances of java beans.
- If you are coding in a JSP, then the session object is already
available, in a Servlet, you need to get the session from the request
object
HttpSession session = request.getSession();
- if the session associated with the web user exists, it is
returned, if not, a new one is created.
- HttpSession is a "container" type class, it is used to
store objects (Java beans) across requests within the same session.
- you'll have to import javax.servlet.http.HttpSession to use it..
- to store an object in a HttpSession, use the setAttribute method,
e.g.
session.setAttribute("name","Fred Flintstone");
// sets a string object to the attribute called "name"
- to get the attribute back in subsequent requests, use
getAttribute method, this method returns null if the attribute is not
associated with the session, e.g.
String name =
(String)session.getAttribute("name");
- you should always set the inactive timeout interval (in seconds)
for your session, e.g.
session.setMaxInactiveInterval(600); //
timeout and destroy the session and its attributes after 10 minutes
- Note, the session will automatically expire if the user exits
their browser if you are using the Apache tomcat server (as it uses a
memory only cookie to track the sessions).
- Other useful methods:
- session.getLastAccessedTime(); // returns the time the
client last sent a request associatied with the session (long)
- session.invalidate(); // has the effect of removing
all attributes objects from the session
- session.removeAttribute("name"); // removes the attribute
object with the specified name
- session.isNew(); // returns true if the session was
just created
- Note, the use-bean directive can be used to create
javabeans and register them with a session, e.g.
<jsp:useBean id="course"
class="registrarbeans.Course" scope ="session"
/>
replaces the following seqence.
registrarbeans.Course course =
(registrarbeans.Course)session.getAttribute("course");
if(course == null) // new sesssion, just create a course
{
course = new registrarbeans.Course();
session.setAttribute("course", course);
}
Java classes and sessions
- Java objects can be created with the "new" operator just like you
did for desktop applications.
- In Netbeans, your web
project will have a "src" directory, within that is a "java"
directory. To create a java class, first create a package in the
Source Packages under your project, then create the class within the
package.
- You will need to import the package, or fully qualify the class
name of
the bean
<%@ page
import="registrarbeans.*"%>
- For example, if you had java class named "ShoppingCart" are in a
package called "registrarbeans", then you could create a new
ShoppingCart object using
registrarbeans.ShoppingCart cart =
new registrarbeans.ShoppingCart();
- To make an object exist throughout the lifetime of the
session, you need to add it to the session during the request
that it was created, then access it other requests., eg.
onlinecompany.ShoppingCart cart =
(onlinecompany.ShoppingCart)session.getAttribute("Cart");
// retrieve object
if(cart == null) // See if object exists, if not, then create one
{
cart = new ShoppingCart();
session.setAttribute("Cart", cart);
}
- if you are coding in JSP, then you could just use the use bean
directive:
<jsp:useBean id="cart" class="registrarbeans.ShoppingCart" scope
="session" />
- Note, it is possible for a user to have multiple browser windows
open at the same time, effectively creating multiple threads on the
server that are accessing the session at the same time. This can
cause problems, so to prevent that, place all code that adds items to
the session in a synchronized block. e.g.
synchronized(session) // lock the session object
{
} // release the lock