Session Tracking

Sang Shin, sang.shin@sun.com, Sun Microsystems, www.javapassion.com/j2ee



The goal of this lab is to exercise session tracking feature of the Web application.  The first exericse lets you see the cookies that are being exchanged between a browser and the backend server.  In the second exercise, you are going to see the session tracking feature that is being used to maintain the items that are being added to the shopping cart.

Expected duration: 60 minutes (excluding homework)


Software Needed

Before you begin, you need to install the following software on your computer. 


OS platforms you can use


Change Log



Lab Exercises



Exercise 1: Build and run "Cookies" part of the "Servlet Examples" sample application

In this exercise, you are going to observe the cookies that are being exchanged between the browser and the backend server.

  1. Build and run "Servlet Examples" sample application
  2. Run "Cookies" part of the application

(1.1) Build and run "Servlet Examples" sample application


0. Start NetBeans IDE
1. Create a new NetBeans project from the sample app that comes with NetBeans (Or you can use the opened application)


2. Accept the default project name and other values and click Finish.



3. Right click ServletExamples project node and select Run.
4. Observe that the browser gets displayed

(1.2) Run "Cookies" part of the application


1. Click Execute to execute the "Cookies" servlet code.
2. Enter some values in the Name and Value fields.
3. Click Submit Query button. (Figure-1.10 below)


Figure-1.10: Create a new Cookie

4. Using HTTP Monitor of NetBeans, observe the Incoming and Outgoing cookies. (Figure-1.11 below)


Figure-1.11: Observe the Incoming and Outgoing cookies

Summary

In this exercise,  you have built and run "Cookies" part of the Servlet Examples sample application and observed the cookies being exchanged.

                                                                                                                        return to the top




Exercise 2: Build and run "JSP Examples" sample application

In this exercise, you are going to build and run "JSP Examples" sample application that comes with NetBeans.


(2.1) Build and run "JSP Examples" sample application


1. Create a new NetBeans project from the sample app that comes with NetBeans

Figure-2.10: Create a new NetBeans project

2. Under New Sample Web Application dialog box, accept the default project name and other values and click Finish.
3. Right click JSPExamples project node and select Run.
4. Observe that the browser gets displayed.  Scroll down to the section with JSP 1.2 Examples.

(2.2) Run "Carts" part of the application that uses session management


1. Click Execute to execute the "Carts" example.
2. Select an item from the drop down list and click add button.  Repeat it a couple of times. 
3. Notice that the items that have been added in previous interactions are displayed. (Figure-2.40 below)  This is the expected behavior.


Figure-2.40: Running Carts

(2.3) Make a breakpoint and observe the shopping items


0. Make sure JSPExamples project is the Main project.  If the project node is in bold font, it is the Main project.
1. Double click DummyCart.java under JSPExamples->Source Packages->sessions to open it in the source editor.
2. Make two breakpoints as shown in Figure-2.30 below.


Figure-2.30: Make breakpoints

3. Select Run from the top-level menu and select Debug Main Project.


Figure-2.31: Run the project in Debug mode

4. Observe that the browser gets displayed.  Scroll down to the section with JSP 1.2 Examples.
5. Click Execute to execute the "Carts" example.
6. Select an item from the drop down list and click add button.  Breakpoint should be hit.
7. Press CTRL+F5 (Continue) twice.
8. Select another item from the drop down list and click add button.  Breakpoint should be hit.
9. Press CTRL+F5 (Continue) once. 
10. Observe in the Local Variable section of the debugger window, the Vector contains the previously added item. (Figure-2.32 below)


Figure-2.32: Observe the  shopping items

(2.4) Modify "Carts" part of the application to use request scope


In this exercise, you are going to modify the "Carts" part of the application to use different scopes - request scope and application scope - other than session scope.  When you use the request scope to maintain the items in the cart, the application will not maintain the items that have been added previously.

1. Modify carts.jsp under under JSPExamples->Web Pages->sessions to use request scope as shown in Code-2.41 below. The code fragments that need to be changed are highlighted in bold and blue colored font.

<jsp:useBean id="cart" scope="request" class="sessions.DummyCart" />

<jsp:setProperty name="cart" property="*" />
<%
    cart.processRequest(request);
%>


<FONT size = 5 COLOR="#CC0000">
<br> You have the following items in your cart:
<ol>
<%
    String[] items = cart.getItems();
    for (int i=0; i<items.length; i++) {
%>
<li> <% out.print(util.HTMLFilter.filter(items[i])); %>
<%
    }
%>
</ol>

</FONT>

<hr>
<%@ include file ="/sessions/carts.html" %>
</html>
Code-2.41: Use request scope

2. Right click JSPExamples project node and select Run.
3. Observe that the browser gets displayed.  Scroll down to the section with JSP 1.2 Examples.
4. Click Execute to execute the "Carts" example.
5. Select an item from the drop down list and click add button.  Repeat it a couple of times. 
6. Notice that the itemts that have been added previously are not displayed.  In other words, you will see always only one item in the list. (Figure-2.42 below)


Figure-2.42: Items are not maintained

7. Make breakpoints and see how the Vector maintains the shopping items.

(2.5) Modify "Carts" part of the application to use application scope



1. Modify carts.jsp under under JSPExamples->Web Pages->sessions to use application scope. The code fragments that need to be changed are highlighted in bold and blue colored font.

<jsp:useBean id="cart" scope="application" class="sessions.DummyCart" />

<jsp:setProperty name="cart" property="*" />
<%
    cart.processRequest(request);
%>


<FONT size = 5 COLOR="#CC0000">
<br> You have the following items in your cart:
<ol>
<%
    String[] items = cart.getItems();
    for (int i=0; i<items.length; i++) {
%>
<li> <% out.print(util.HTMLFilter.filter(items[i])); %>
<%
    }
%>
</ol>

</FONT>

<hr>
<%@ include file ="/sessions/carts.html" %>
</html>
Code-2.50: Use application scope

2. Right click JSPExamples project node and select Run.
3. Observe that the browser gets displayed.  Scroll down to the section with JSP 1.2 Examples.
4. Click Execute to execute the "Carts" example.
5. Select an item from the drop down list and click add button.  Repeat it a couple of times. 
6. Notice that the itemts that have been added previously are now displayed again. 
7. Change back the carts.jsp to use session scope again.


Summary

In this exercise, you have built and run "Sessions" part of the  "JSPExampless" sample application.
   
                                                                                                                                   Return to the top


Homework Exercise (for people who are taking Sang Shin's "Java EE Programming online course")


1. The homework is to modify the HelloWeb project as described below.  The HelloWeb project is included as part of the hands-on lab zip file as <LAB_UNZIPPED_DIRECTORY>/sessiontracking/samples/HelloWeb. (You might want to create a new MyHelloWeb project by copying HelloWeb project. You can name the homework project in any way you want but here I am going to call it MyHelloWeb.) 
2. Send the following files to j2eehomeworks@sun.com with Subject as J2EEHomework-sessiontracking.

                                                                                                                     return to the top