Java Reference
In-Depth Information
Introspection
Introspection refers to the ability to look inside a JavaBean and identify the methods available to the
user. For example, introspection allows the JSP engine to look at the JavaBean properties that can be
set from the JSP page. This means that the JSP engine can handle most of the details of setting
properties automatically, as shown here:
<jsp:setProperty name=" TestBean" property="*"/>
Using the "*" wildcard as the value of the property attribute in a <jsp:setProperty> tag tells the JSP
engine to use introspection to identify the JavaBean's properties and match them by name with the
parameters the form passes. This approach is illustrated in Listing 12-6 .
Listing 12-6: Using a JSP with the <jsp:useBean/> tag
<html>
<head>
<title>ParameterTestBean</title>
</head>
<body>
<%@ page language="java"%>
<jsp:useBean id="ParameterTestBean"
class="JavaDatabaseBible.ch12.ParameterTestBean"/>
<jsp:setProperty name="ParameterTestBean" property="*"/>
User Name:
<jsp:getProperty name="ParameterTestBean" property="username"/><p/>
Password:
<jsp:getProperty name="ParameterTestBean" property="password"/>.
</body>
</html>
Much nicer, isn't it? Of course, if you don't have a one-to-one match between the parameter names and
the bean properties, you can always resort to mapping them by hand. Incidentally, this technique also
works for saving values from radio buttons, where the JavaBean property is set to the selected radio-
button value.
You can test the use of JSP and JavaBeans using the simple JavaBean of Listing 12-7 . This is the
JavaBean originally used with the JSP code of Listing 12-6 .
Listing 12-7: Simple JavaBean illustrating getter and setter methods
package JavaDatabaseBible.ch12;
public class ParameterTestBean extends java.lang.Object{
protected String username;
protected String password;
public ParameterTestBean(){
}
public void setUsername(String username){
Search WWH ::




Custom Search