Intro to JavaServer Pages: Use JavaBeans

Learn about JavaBeans, and see how to use them.

By John Zukowski

Although you can place large blocks of code within scriptlets, most of the Java code belongs in reusable components called JavaBeans. JavaBeans are like ActiveX controls: they provide known functionality and are designed for off-the-shelf reuse.

A JavaBean's value is made available through a set of properties, which provides access to the JavaBean's settings. Using a person as an analogy, the person would be a JavaBean, and that person's name, social security number, and address would be the properties. With JSP Web sites, you basically play "connect the beans" to get your site to be dynamic.

Assuming the JavaBean was created prior to creating the site, the first thing you need to do is tell the JSP page that it intends to use a bean. This is done with the <jsp:useBean> tag:
<jsp:useBean id="localName" class="com.jguru.Person" scope="application" />.

The <jsp:useBean> tag requires you to identify the bean with the id attribute. Here you provide a name to identify to the bean within the rest of the JSP page. In addition to the id attribute, you also must tell the page where to find the bean, or its Java class name. The class attribute provides this, among a handful of other ways to specify it. The final required element is the scope attribute. With the help of the scope attribute, you can tell the bean to maintain its information for the single page (the default) [scope="page"]; for the request [scope="request"]; for the session [scope="session"]; or for the whole application [scope="application"]. With session scope, you can maintain items such as shopping carts very easily within JSP pages.

Once you have declared a JavaBean, you have access to its properties to customize it. To get the value of a property, use the <jsp:getProperty> tag. With the <jsp:getProperty> tag, you specify the name of the bean to use (from the id field of useBean), as well as the property whose value you want to get. The actual value is then placed within the output:
<jsp:getProperty id="localName" property="name" />.

Changing the property of a JavaBean requires you to use the <jsp:setProperty> tag. For this tag, you again identify the bean and property to modify and additionally provide the new value. If named properly, these can be directly acquired from a submitted form:
<jsp:setProperty id="localName" property="*" />;

acquired from a parameter, in which you have to name the property and parameter directly:
<jsp:setProperty id="localName" property="address" param="parameterName" />;

or just directly set with a name and value:
<jsp:setProperty id="localName" property="serialNumber" value="string" /> or <jsp:setProperty id="localName" property="serialNumber" value= <%= expression %> />.

One last thing about JavaBeans: So that the Web server can find the JavaBeans, you'll need to place their class files in a special place. With the JSWDK, the simplest place is the classes directory under the installation directory, as in \jswdk-1.0.1\classes.

John Zukowski is a software guru and director of education at In addition, John is the author of several Java books and numerous Java-related technical articles.

Editor's Picks

Free Newsletters, In your Inbox