By nature, HTTP protocol is stateless and connectionless protocol. Stateless means, the server does not remember the client when once response is delivered. All the client data is deleted on server. Sometimes, client would like to do many interactions with the server to get complete information of a database table of a Bank account. Here, his account number is to stored in some place that can be used later in many database queries. Or, client may buy some goods in an online super bazar. The data of the client like soaps, perfumes etc. should be stored in some place until final bill is made. Or, sometimes, user moves from one page to another while searching. It may be required to keep a track of the data of the client in every page. Here comes session object to the rescue. Learn what is Session, Session Tracking, Session Management.
"session" is one of the 14 attributes page directive supports. It is an object of javax.servlet.http.HttpSession interface. All the methods of HttpSession interface can be used straight away by session object. session object is capable to store the data of a single client. The scope of session object data is for a single client. The session object can be used by the client from anywhere of the application (application comprises of many programs). Different clients will have different session objects. One client session object cannot access other client session data. To have data used between different clients, JSP has a different object – application object.
To set the value for a session, setAttribute() is used and later to retrieve the value, getAttribute() is used; these are the two important methods of HttpSession. Other methods are getId(), getCreationTime(), isNew(), invalidate() etc. These methods are used in Servlet Session Tracking with HttpSession – Hit Count.
Syntax of using session attribute
session attribute takes a boolean value of true or false. If set to true, the session is maintained in JSP, else if set to false, session is not maintained. Default is true; that is, session is implicitly maintained with every JSP page.
<%@ page session="true" %>
<%@ page session="false" %>
Let us explore more of set and get methods.
- public abstract void setAttribute(String str, Object obj): This method takes two parameters of String and Object used for key/value pairs. String str is used as a key and obj is used a value (always a value is associated with a key). To retrieve the value, Programmer uses key. The second parameter must be an object, an object of any Java class.
- public abstract Object getAttribute(String str): This is used by the Programmer to get back the value associated with the key str (set with setAttribute() method earlier). The return type Object should be casted in the program. This will be shown in the example given.
A Simple Employee session JSP application to get the concept. It comprises of 3 programs.
1. Client Program, File Name: EmployeeProperties.html: This HTML code reads two properties of an employee like name and salary and send to EmployeeSet.jsp.
2. Server Program, File Name: EmployeeSet.jsp: This file reads the employee properties, send by client , and set with session attribute using setAttribute() method. These values are read from a different program, EmployeeGet.jsp.
3. Server Program, File Name: EmployeeGet.jsp: This reads from session object using getAttribute() method and just prints with small logic.
<%@ page session="true" %> <% // a scriptlet String empName = request.getParameter("nameField"); // reading from client String empSalary = request.getParameter("salaryField"); session.setAttribute("en", empName); // setting with session object session.setAttribute("es", empSalary); // just a message out.println("Employee values are set with session object.
"); out.println("Can be accessed from a different program."); %>
session.setAttribute("en", empName); session.setAttribute("es", empSalary);
Employee name and salary read from HTML file are stored in string variables empName and empSalary. They are set with session object using two strings en and es. en and es are used as keys in the next program to retrieve the associated empName and empSalary.
<% <%@ page session="true" %> // reading from session object Object obj1 = session.getAttribute("en"); // supply the key Object obj2 = session.getAttribute("es"); String str = (String) obj2; int salary = Integer.parseInt(str); if(salary > 10000) out.println(obj1 + " is drawing great salary"); else out.println(obj1 + " is drawing less salary"); %>
Screenshot when HTML code is run and entered some values
Screenshot of EmployeeSet.jsp when HTML file is run
Screenshot when EmployeeGet.jsp is run
Object obj1 = session.getAttribute("en"); Object obj2 = session.getAttribute("es"); String str = (String) obj2; int salary = Integer.parseInt(str);
Because, getAttribute(String) returns an object, it is assigned to obj1 and obj2. obj1 is not casted as it is used for just printing. But obj2 is casted to String str as salary is set as a string in setAttribute("es", empSalary). Later str is parsed to int salary as salary is used in if structure. If only printing purpose, without casting, obj2 can be used in the code.
14 Page directive Attributes and Examples