Java Reference
In-Depth Information
The
Counter
JavaBean
As discussed in Chapter 16, “JSP Standard Actions,” the
<jsp:useBean>
standard action pro-
vides four different options for the
scope
attribute:
page
,
request
,
session
, and
application
.
We will discuss each of these and give an example of how to use them in the following sec-
tions.
For our examples, we will be using the
Counter
bean from Chapter 16, which acts as a simple
counter. It has a single
int
property,
count
, which holds the current number of times the
bean's property has been accessed. It also contains the appropriate methods for getting and set-
ting this property. Listing 17.1 contains the source for this bean.
L
ISTING
17.1
Counter.java
import java.io.Serializable;
public class Counter implements Serializable{
// Initialize the bean on creation
int count = 0;
// Parameterless Constructor
public Counter() {
}
// Property Getter
public int getCount() {
// Increment the count property, with every request
count++;
return this.count;
}
// Property Setter
public void setCount(int count) {
this.count = count;
}
}
To use this bean you will need to compile it and move the class file to your
<
SERVER_ROOT
>/djs/WEB-INF/classes/
directory, if you have not already done so.