Sun Java Solaris Communities My SDN Account Join SDN
 
Tutorials & Code Camps

jGuru: Understanding JSP Object Scope

 


[Help | API Docs | Short Course| Exercises]

This exercise implements a simple JSP page (Counter.jsp), which instantiates two instances of a bean which maintains a counter (CounterBean.java), but with differing scope. One bean is attributed with session scope, and the other with application scope. Each time the JSP page in invoked, the count of each of the beans is incremented by one. You can observe the difference between session and application scope when you access the counter page from different browsers. Each browser maintains a distinct count for their session, but share the counter with application scope, since it is treated as a global variable.

Prerequisites

Skeleton Code

Tasks

1. Develop a simple counter bean, CounterBean.java.

2. Compile the counter bean.

3. Deploy the bean within Tomcat.

4. Develop a JSP page, Counter.jsp, which creates two instances of the counter bean, one with session scope, and the other with application scope.

5. Deploy the JSP file for the example within Tomcat.

6. Run the example.

Where help exists, the task numbers above are linked to the step-by-step help page.

Solution Source

The following files contain a complete implementation of the example demonstrating JSP variable scope:

Demonstration

From your browser (say, Netscape Navigator), access the URL http://localhost:8080/examples/jdc/counter/Counter.jsp

Reload the page a few times. You should see the counters incremented as shown below:

[Demonstrating beans with varying scope]

From using a different browser (say, MSIE), access the same URL. Observe the difference in the counts:

[Demonstrating beans with varying scope]

Next Exercise

Exercises

Short Course

Copyright 1996-2000 jGuru.com. All Rights Reserved.