implicit object session JSP Example

implicit object session JSP Example


We know the HTTP Protocol nature (stateless and connectionless) is to break the connection (earlier established between Web client with Web server) when the response is delivered to client. If the same client would like to send another request to the same server, it should establish a new connection and send a fresh request again. The protocol is designed so to distribute the server resources equally to all the clients throughout the world. It becomes a negative point for e-commerce (shopping cart) applications, where on a single connection, the client orders multiple items (say, in an online super bazar) and the data of all these items should be preserved until the final bill is prepared. But, it is not possible with HTTP Protocol. To address this problem, session object was introduced in JSP.

1. What is a session in general?

The interactive time between Web client and Web server on a single connection is known as session. That is, difference of time between connection established time and connection broken time is known as session.

2. What is session tracking?

Preserving the data of the client in a single session (known as conversation state of the client) is known as session tracking. The data on a super bazar may include like Lux soaps – 5, Colgate paste – 2 etc.

3. What is session management?

Adding data (or super bazar items selected) to a session object and retrieving later correctly is known session management. Perfect session management makes an e-commerce application robust.

4. Now what is session JSP object?

"session" is one of the 9 implicit objects, JSP supports. session object can take care of session tracking mechanism (can be achieved with cookies also). session represents an object of javax.servlet.http.HttpSession interface. The session object works with HTTP protocol only.

Few methods of HttpSession which session JSP object can use are:

getAttribute(String name), setAttribute(String name, Object value), getId(), isNew(), setMaxInactiveInterval(int time), invalidate(), removeAttribute(String name).

These methods are used very often in session programming.

A simple session program illustrates many of the above methods.

File Name: UsingSession.jsp

session JSP

Let us explain the code.

<%@ page session="true" %>

In JSP, by default session is set to false. If session tracking is required, we must set the session to true. In Servlets, default session is true. The above statement is called page directive and right now do not bother much about this part. Concentrate on the relevant topic now.

In the Scriptlet, session object is set with some data. The method used is setAttribute(). The syntax is setAttribute(String str, Object obj). This method takes two parameters in key/value pairs. The first parameter is a string always and the second parameter is an object of Object class. That is, the second parameter can be an object of any Java class (ofcourse, a subclass of Object class). For this reason, in the above statements, key and value are both string objects. It is accepted. Infact, the second parameter can be an object of Integer or Double or even a Vector.

out.println(session.getAttribute("Lux"));

with getAttribute() method of session object, the value associated with the key can be retrieved. Simply, pass the key to the method, the method returns the value of the key.

At the same time, we can get the values of all the keys at a time with getAttributeNames() method of session object. This method returns an object of Enumeration. Here, e object of Enumeration contains all the keys (but not values). Each key one-by-one is obtained with iterations. The iterations are done by hasMoreElements() method of Enumeration. The nextElement() method returns one key with each iteration. Infact, the nextElement() returns an object of Object class and is casted to string as in setAttribute() method, the values are stored as strings (the second parameter). getAttribute(key1) returns the key associated with key1. key1 changes with each iteration.

If the user does not use the session object for a stipulated time period, the session gets expired automatically. The time can be set with setMaxInactiveInterval(300) and can be known with getMaxInactiveInterval(). The default period of time is 1800 seconds. That is, if the user does not use the e-commerce Web site for 1800 seconds (or 30 minutes), it gets implicitly expired and connection is broken with a message "time out" and sometimes "session expired" etc. With setMaxInactiveInterval(300) method, the default time is changed to 300 seconds.

If the user opens the same Web site from another browser on the same system, it does create a new session, instead adds to the old existing session. This is checked with isNew() method.

Also it is possible to print only the keys with getValueNames() method. This method returns a string array with all the keys added with setAttribute() methods earlier. To print the keys, enhanced for loop introduced withJDK 1.5 is used.

Leave a Reply

Your email address will not be published. Required fields are marked *