Advertisment

JSP @ shopping site

author-image
PCQ Bureau
New Update

When you visit a website, technically a user session starts, ending only when you leave the site or close your Web browser. Sometimes, as in shopping sites, it is necessary to track this session. We’ll see how this process, called session tracking, takes place. We’ll take a hypothetical website and assemble a PC by selecting components (motherboard, processor, RAM, hard disk, graphics card, sound card) of a computer. We’ll select each component on a different page and use a JSP object, called session, to store the selection made on each page.

Advertisment

How it works

Before getting into coding, let’s get a feel of it. For running JSP, use the /jsp context from our article 



Customizing Tomcat (page 158, PCQuest May 2001). Mount this month’s CD and change to the directory cdrom/sorc_cod/jsp. Copy the files welcome.jsp, session.jsp, result.jsp and process.jsp from this directory to /var/www/html/jsp. Now type in the following URL:

Advertisment

If you have configured the Tomcat module for Apache, then eliminate the port number 8080 from the URL. This will present a “Welcome” Web page and clicking on the link “Start Assembling” will take you through six Web pages where on each page you select a computer component. Then a Web page will show your selected components in a tabular form with a link named “Remove” for each component, clicking on which will remove a component from the list. When you click on the link “Click here to proceed” on this page, you will see your final selections. (To install Tomcat and setup /jsp context on PCQLinux 7.1, see article titled Tomcat on PCQLinux 7.1, page 126.)

Session tracking basics

An object called session is available to all JSP pages as the request object (see article HTML Forms with JSP, page 143, PCQuest June 2001). The different methods (functions) of this object let you store, retrieve, or remove a selection and also invalidate the session. The syntax for the first three is as follows:

Advertisment
session.setAttribute(, );



session.getAttribute();


session.removeAttribute();

Where is a Java String that refers to corresponding , which can be any Java object stored in the session object by the user.

Explore the code

Advertisment

Open the file welcome.jsp in any Linux text editor. You’ll need to keep this code in front of you to understand the explanation given below. The line

<%



session.setAttribute(“PageNumber”,new Integer(0));


%>

stores an Integer Object with value “0” on the session object with unique name . This is used to generate dynamic pages, one for each computer component.

Advertisment

When you click the link “Start Assembling”, it takes you to the JSP file session.jsp. Now open this file in a text editor. 



Within the JSP declaration tags first declare a single dimensional array called ‘items’ and a two dimensional array called ‘options’. These are used to display a component and its corresponding choices using the HTML

Integer IntPageNumber = (Integer)session.getAttribute(“PageNumber”);

retrieves the value corresponding to the unique name “PageNumber”. Since the value is stored as a generic Java Object, it must be cast back to Integer Object (by prefixing “(Integer)”), before it’s assigned to the Integer object IntPageNumber. You can proceed further only when IntPageNumber is not a “null” (or empty) object, which you check using the “if” block. Convert the Integer object to the integer data type using the intValue( ) method of the Integer object and store it in the integer variable pageNumber. When you first access session.jsp from welcome.jsp, the value in PageNumber variable will be 0, so the “if” block will be skipped for the first time. The next line:

Advertisment
Select a <%= items %>

evaluates to

Select a Motherboard
Advertisment

when pageNumber is 0 since items<0> holds the String “Motherboard”. Moving further down, the lines:

<% 



if (pageNumber == 5) 


{


%>




<%


} // closing bracket of if (pageNumber == 5) 


else


{


%>




<%


} // closing bracket of else


%>









Here, check whether PageNumber variable holds 5. If it does, then the user has gone through all 6 pages (as the count (pageNumber) starts from zero) of selecting components, and the HTML Form’s action attribute will point to result.jsp. Otherwise it will keep pointing to

session.jsp.

Next, create a HTML selection list as:


when pageNumber is equal to “0”. We will use this naming scheme to retrieve the selected value as you will see later.

Next using a “for” loop, all choices stored in the options array for the component are displayed using the HTML select list. Finally the

line

session.setAttribute(“PageNumber”,new Integer(pageNumber+1));

increments the pageNumber variable by 1 and stores it on the session object using the unique name PageNumber. This overwrites the previous value for PageNumber, which was “0”. 

Now when you click on the “Add Item” button, the session.jsp page is again loaded. But this time, the pageNumber integer variable holds “1”, so the “if” block:

if (pageNumber != 0)

which was skipped for the first time, gets executed.

The line:

String selectedValue = request.getParameter(items);

retrieves the selected value from the request object (as explained in the article HTML Forms with JSP. For the name of the Form element (the argument for the getParameter( ) method) we have supplied the array element items which evaluates to “Motherboard” when pageNumber is equal to “1” as follows:

items = items<1-1>=items<0>=”Motherboard”

Then, store the selected value with the name “items” in the session object as:

session.setAttribute(items,selectedValue);

When all six components have been selected, the HTML form’s action attribute points to result.jsp, as explained above. Now look at the coding of result.jsp. In this JSP, first declare an Enumeration object called sessionObjectNames and a String object called sessionObjectName. The first can store a number of Java objects, which can be retrieved by calling the method nextElement( ). When result.jsp is first loaded through session.jsp, the request object contains information about the parameter “Sound Card”. So the following “if” block gets executed:

if (item !=null)



session.setAttribute(“Sound Card”,item);

The method getAttributeNames( ) of the session object returns an Enumeration object which contains all the unique names stored in the session object. Hence the line:

sessionObjectNames = session.getAttributeNames( )

stores the returned Enumeration in the Enumeration object sessionObjectNames. Now you can retrieve the unique names by calling the nextElement( ) method. But before calling this method repeatedly to retrieve all the unique names, each time you must check whether the Enumeration object holds more elements. This is accomplished by calling the method

hasMoreElements( ), which returns “true” if the Enumeration object has more elements else returns “false”. Note that you cast the generic Java object returned by the nextElement( ) method to a String object. Since the session object also contains “PageNumber” which you don’t want to display, skip it using the condition - if(!sessionObjectName. equals(“PageNumber”)) and display the component name and the selection made by the component as :



<%= sessionObjectName %> 

<%= session.getAttribute (sessionObjectName) %>

You also display a link named “remove” to remove the component from the list. The link points to:

result.jsp?RemoveItemName=<%=

sessionObjectName.replace(‘ ‘,’+’) %>

This means the link points to the result.jsp and passes a name-value pair. The syntax of the link is:

?

The name value pair is specified as:

RemoveItemName=

The corresponds to sessionObjectName. Uuse the replace( ) method of the String class to replace any spaces with a “+”. In a HTTP URL the “+” stands for a space. If the component name contains a space (like in “Hard Disk”, “Graphics card” and “Sound Card”) the corresponding values will be “Hard”, “Graphics” and “Sound”.

When you click on the remove link, result.jsp is again loaded. This time the request object contains a value corresponding to the name “RemoveItemName”. The call to the getParameter( ) method returns the name of the component. The following if block removes the component from the session object.

if(removeItemName != null)



session.removeAttribute(removeItemName);

When you click on the link “Click here to proceed”, the process.jsp gets loaded. It displays the final selection, that is, all components but those which you removed by clicking on “remove” link. The code of process.jsp is almost the same as result.jsp. You also invalidate or dispose the session in process.jsp using the method invalidate( ) of the session object. You can extend process.jsp by storing the selections in a database using JDBC or mailing the selections using the Java Mail API.

Shekhar Govindarajan

Advertisment