Session Tracking
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.
- Java Standard Development Kit (JDK™) version
6.0 (download)
- If you already have installed JDK 5.0 or JDK 6.0, you
can skip this.
- NetBeans IDE 6.1 (download)
- When you install NetBeans IDE, it will ask you which JDK
you want to use.
- Download either "Web &
Java EE" or "All"
bundles.
OS platforms you can use
- Windows
- Solaris x86, Solaris Sparc
- Linux
- Mac OS X
Change Log
- Nov. 13th, 2006: Homework is added
- Nov. 22nd, 2006: HelloWeb project is included as a sample project
for the homework
- Nov. 28th, 2006: Homework has a bit more clarification
- July 22nd, 2008: NetBeans 6.1 and GlassFish v2 are used, homework
is clarified a bit
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.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)
- Select File->New Project (Ctrl+Shift+N).
- Observe that the New Project
dialog box appears.
- Expand Samples.
- Select Web under Categories and Servlet Examples under Projects.
- Click Next.
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
- Select File->New Project (Ctrl+Shift+N).
(Figure-1.10 below) The New Project
dialog box appears.
- Expand Samples.
- Select Web under Categories and JSP Examples under Projects. Click Next.
(Figure-2.10 below)

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.)
- It appends the newly typed name in the second request to the name
entered in the first request through a session as following.
- The first time you run the application, you get prompted to
enter your name. You enter your name, for example, Shin, and click OK. This is
the first request, which will be responded with the response.jsp displaying Hello, Shin.
- Modify the respond.jsp
so that it has a button or link a user has to click to go to index.jsp again, which prompts a
user to enter another name.
When the user enters a new name, for example, Sang, and press OK, this
will be sent to the server as the second request.
- The response.jsp
should then display ShinSang
at this point.
- The name is maintained as an attribute in the Session scope
object.
- Hint: Note that the scope of the mybean is currently set to request as shown below in response.jsp. This is the
scope you want to change to session.
- <jsp:useBean id="mybean" scope="request"
class="org.me.hello.NameHandler" />
- Hint: You might want to change the setName() method as following
- public void setName(String name) {
if (this.name == null) {
this.name
= name;
}
else {
this.name
= this.name + name;
}
}
2
. Send the following files to
j2eehomeworks@sun.com
with Subject
as J2EEHomework-sessiontracking.
- Zip file of the the MyHelloWeb
NetBeans project. (Someone else
should be able to open and run it as a NetBeans project.) You can
use your favorite zip utility or you can use "jar" utility that comes
with JDK as following.
- cd <parent directory that contains MyMyHelloWeb directory>
(assuming you named your project as MyHelloWeb)
- jar cvf MyHelloWeb.zip MyHelloWeb (MyHelloWeb should contain nbproject directory)
- Captured output screen -
name it as J2EEHomework-sessiontracking.gif
orJ2EEHomework-sessiontracking.jpg (or J2EEHomework-sessiontracking.<whatver
graphics format>)
- Any screen capture that shows that your program is working is
good enough.
- If you decide to use
different IDE other than NetBeans, the zip
file should contain all the files that are needed for rebuilding the
project - war file with necessary source files is OK.
return to the top