You are on page 1of 11

Managing Sessions and Handling Errors

Session Management

• Session management refers to tracking the state of an end user across Web
pages.
• Session management enables programmers to create applications where the
state of an end user is required to be maintained across multiple Web pages.
Managing Sessions and Handling Errors

Session Management (Contd.)

• The techniques for managing the state of an end user are:

• Hidden form field


• URL rewriting
• Cookies
• Servlet session API
Managing Sessions and Handling Errors

Session Management (Contd.)

• Hidden Form Field is:

• Simplest technique to maintain the state of an end user.


• Embedded in an HTML form.
• Not visible when you view an HTML file in a browser window.
• Not able to maintain the state of an end user when it encounters a static
document.
Managing Sessions and Handling Errors

Session Management (Contd.)

• URL Rewriting:

• Maintains the state of end user by modifying the URL.


• Is used when the information to be transferred is not critical.
• Cannot be used for maintaining the state of an end user when a static
document is encountered.
Managing Sessions and Handling Errors

Session Management (Contd.)

• Cookies:

• Are chunks of information created by the server and are stored by the
browser on the client machine.
• Supported by the Web browser and the size of each cookie is maximum of
4 bytes.
• Are used by the server to find out the computer name, IP address, or any
other details of the client computer.
Managing Sessions and Handling Errors

Session Management (Contd.)


• The following table describes the methods defined in the Cookie class:

Method Description

public String getName() Returns the name of the cookie.

public void setMaxAge(int expiry) Sets the maximum time for which the
client browser retains the cookie value.

public int getMaxAge() Returns the maximum age of the cookie


in seconds.

public void setValue(String value) Sets a new value to the cookie.

public String getValue() Returns the value of the cookie.


Managing Sessions and Handling Errors

Session Management (Contd.)

• The Servlet Session API provides various interfaces and classes, which can be
used for managing end user sessions. The interfaces defined in the Servlet
Session API are:

• javax.servlet.http.HttpSession
• javax.servlet.http.HttpSessionListener
• javax.servlet.http.HttpSessionBindingListener
Managing Sessions and Handling Errors

Session Management (Contd.)


• The following table describes the various methods defined in the HttpSession
interface:
Method Description

public void setAttribute(String Binds an attribute to a session object with


name, Object value) a unique name and stores the name/value
pair in the current session. If an object is
already bound with the same attribute,
then the new object replaces the existing
object.
public Object getAttribute(String Retrieves the object bound with the
name) attribute name specified in the method,
from the session object. If no object is
found for the specified attribute, then the
getAttribute() method returns null.
Managing Sessions and Handling Errors

Session Management (Contd.)


• Methods of HttpSession interface (Contd.):

Method Description

public Enumeration Returns the name of all the objects


getAttributeNames() that are bound to the session object.

public void removeAttribute(String Unbinds the session object from the


name) attribute, name specified in the
method.

public void Sets the maximum time for which


setMaxInactiveInterval(int interval) the session will remain active. The
time is specified in seconds.
Managing Sessions and Handling Errors

Session Management (Contd.)


• Methods of HttpSession interface (Contd.):

Method Description

public int getMaxInactiveInterval() Returns the maximum time in


seconds for which the server will not
invalidate the session even if there is
no client request.

public String getId() Returns a string that contains the


unique identifier associated with the
session.

public void invalidate() Invalidates a session. All the objects


bound to the session are
automatically unbound.
Managing Sessions and Handling Errors

Demonstration-Implementing Session
Management using Session API
• Problem Statement

• Larry Williams is in charge of the garments section of Countryside


Markets. He receives information stating that the total in the bill is
wrong. Larry visits the Web site of his company and does an online
shopping. He finds that the bill includes information of only those
shirts that he selected on the last Web page he visited. The
information of shirts that he selected in the earlier Web pages was
lost. Larry asks John, the developer of the company’s Web site, to
modify the Web site such that the shirts selected by a user should
be tracked and the bill should get updated accordingly. John
develops a test application for the customer, Mike to test the
functionality.

You might also like