| Actions in JSP |
| |
| The jsp:useBean Action |
| |
| This action lets us load in a JavaBean to be used in the JSP page. This is a a very useful capability because it lets you exploit the reusability of Java classes without sacrificing the convenience that JSP adds over servlets alone. |
| |
| The simplest syntax for specifying that a bean should be used is: |
| <jsp:useBean id="name" class="package.class" /> |
| |
| This usually means "instantiate an object of the class specified by class, and bind it to a variable with the name specified by id." However, as we'll see shortly, we can specify a scope attribute that makes the bean associated with more than just the current page. In that case, it is useful to obtain references to existing beans, and the jsp:useBean action specifies that a new object is instantiated only if there is no existing one with the same id and scope. |
| |
| Now, once we have a bean, we can modify its properties via jsp:set Property, or by using a scriptlet and calling a method explicitly on the object with the variable name specified earlier via the id attribute. Recall that with beans, when we say "this bean has a property of typeX called form", we really mean "this class has a method called getForm that returns something of type X, and another method called setForm that takes an X as an argument." |
| |
| The jsp:set Property action is discussed in more detail in the next section, but for now note that we can either supply an explicit value, give a param attribute to say that the value is derived from the named request parameter, or just list the property to indicate that the value should be derived from the request parameter with the same name as the property. |
| |
| We read existing properties in a JSP expression or scriptlet by calling the appropriate getXxx method, or more commonly, by using the jsp:getProperty action. |
| |
| Note that the class specified for the bean must be in the server's regular class path, not the part reserved for classes that get automatically reloaded when they change. |
| |
| For example, in the Java Web Server, it and all the classes it uses should go in the classes directory or be in a jar file in the lib directory, not be in the servlets directory. |
| |
| Here is a very simple example that loads a bean and sets/gets a simple String parameter. |
| |
| BeanTest.jsp |
| |
</HTML><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Reusing JavaBeans in JSP</TITLE>
<LINK REL=STYLESHEET
HREF="My-Style-Sheet.css"
TYPE="text/css">
</HEAD>
<BODY>
<CENTER>
<TABLE BORDER=5>
<TR><TH CLASS="TITLE">
Reusing JavaBeans in JSP</TABLE>
</CENTER>
<P>
<jsp:useBean id="test" class="hall.SimpleBean" />
<jsp:setProperty name="test"
property="message"
value="Hello , how is this JSP tutorial from eBIZ" />
<H1>Message: <I>
<jsp:getProperty name="test" property="message" />
</I></H1>
</BODY>
|
| |
| SimpleBean.java |
| Here's the source code for the bean used in the BeanTest JSP page. |
| |
package hall;
public class SimpleBean
{
private String message = "No message specified";
public String getMessage()
{
return(message);
}
public void setMessage(String message)
{
this.message = message;
}
} |
| |
| Here's a typical result: try it out yourself |
| |
| |
|
| |
| |