Java Reference
In-Depth Information
Using JavaBeans with Java Server Pages
One of the most useful features of Java Server Pages is that JSP directly supports the use of
JavaBeans through the <jsp:useBean> tag. Java Beans, as you probably know, are Java classes that
can be loaded by name and otherwise conform to a specific set of rules. These rules include the
following:
 
Being a public class: public class JavaBean
 
Having a public, no argument constructor: public JavaBean ( )
 
Using private data fields only: private String message
 
Providing public, no-argument access methods for its private data fields:
 
public getMessage()
 
public setMessage(String message)
 
Supporting introspection — the ability of an external class to query a bean for its behavior.
A big advantage of using JavaBeans in JSP applications is that they allow you to implement the logic of
the JSP page as a separate Java class and "plug it in." This approach offers these significant
advantages:
 
Separation of content from logic
 
Reusable plug-in components for common tasks
When they are used with Java Server Pages, JavaBeans have two primary functions. They can be used
as logic blocks, where their primary purpose is to separate logic from display, and as storage classes,
where their primary purpose is data storage.
Loading by Name: the <jsp:useBean> tag
The ability to load and execute a JavaBean by name is the real key to using JavaBeans as pluggable
components. Since the JavaBean is linked into the JSP at runtime rather than at compile time, the JSP
can be edited and updated separately from the business logic in the JavaBean.
To call a Java Bean from a JSP, simply write this:
<jsp:useBean id="TestBean" class="java_database_bible.TestBean"/>
The <jsp:useBean> element has a number of attributes. Of these, the most commonly used are the
following:
 
id="beanInstanceName". The id attribute assigns a local name to the JavaBean for
references within the JSP page. The id is case sensitive and must be used consistently throughout
the scope of the bean.
 
s cope="page | request | session | application". The scope attribute defines the
scope in which the bean exists and the in which variable named in id is available. The default
value is page.
 
class="package.class". The class attribute defines the JavaBean class to load if the
JavaBean with the specified id has not already been loaded in the defined scope. The new bean is
instantiated using the new keyword and the class constructor.
Note
The <jsp:useBean> tag first looks for the bean instance with the specified name and
instantiates a new one only if it cannot find the bean instance within the specified scope.
If the bean has already been created by another <jsp:useBean> element, the value of
id must match the value of id used in the original <jsp:useBean> element.
Scope
Once a JavaBean has been loaded, it can be accessed from various parts of your application,
depending on its scope . In other words, the scope of a JavaBean defines that part of your application
that can access the bean. The default value is page scope . The meanings of the different scopes are
as follows:
Search WWH ::




Custom Search