The idea of beans is to have some Java programs (beans)
available to use for any other applications. For example,
a program TheSimpleBean.java, once compiled, its
corresponding TheSimpleBean.class file will be used
by any other application. In java, to set this little
related program (bean), this bean must be serializable,
has an empty constructor, and has getter and
setter methods (the properties); like this:
public class TheSimpleBean implements java.io.Serializable {
package NestDirectory;
private Type PropertyName;
public TheSimpleBean () {}
public Type getPropertyName() {
return PropertyName;
}
public void setThePropertyName(Type ThisPropertyName) {
PropertyName = ThisPropertyName;
}
}
The class implements the interface Serializable class. This class has no methods;
It allows just the object to be stored and then retrieved some later time. In order to
locate the bean, we mention at first its nest directory as a package. This directory
is a subdirectory of /WEB-INF/classes/, rekated to Tomcat. That is the file
TheSimpleBean.java is located at : /WEB-INF/classes/NestDirectory/.
If there are other subclasses X1, X2, ...Xn between /WEB-INF/classes/, for example:
/WEB-INF/classes/X1/X2/X3/NestDirectory/, we mention the package as follows:
package X1.X2.X3.NestDirectory;
Now, we will use this JavaBean in JSP program. More precisely, JSP will call
TheSimpleBean.class by the following tags properties:
<jsp:useBean
ID = "Object Name"
class="Package/NameOfClass"
/>
<jsp:setProperty
name ="ID Of JavaBean, ie Object Name"
property = "PropertyName"
value="New value of this Property"
/>
<jsp:getProperty
name = "ID Of JavaBean, ie Object Name"
property="PropertyName"
/>
We built TheSimpleBean.jsp like this:
<html>
<head>
<title> TheSimpleBean JSP Page</title>
</head>
<body>
<jsp:useBean id = "ObjectName" class = "X.NestDirectory.TheSimpleBean">
<jsp:setProperty name = "ObjectName" property="PropertyName"
value="The value of the PropertyName" />
</jsp:useBean>
<jsp:getProperty name="ObjectName" property = "PropertyName"/>
</body>
</html>
And place it in the in /WEB-APP folder.
|