Home / Articles / Using JavaBeans with JSP

Using JavaBeans with JSP

  • By .
  • Sample Chapter is provided courtesy of Que.
  • Date: Feb 22, 2002.


  1. Using <jsp:useBean>
  2. Using &lt;jsp:getProperty&gt;
  3. Using &lt;jsp:setProperty&gt;

Chapter Description

The JavaBeans specification allows for the creation of reusable Java components, thus providing a good deal of modularity to your code. This article introduces you to JavaBeans, and shows you how to use JavaBeans components in your JSP pages.

Using <jsp:getProperty>

One of the primary reasons for using JavaBeans is to return dynamic information. The <jsp:getProperty> action returns information from a bean as a String object. There are only two attributes in this action:

  • name—Specifies the name of the bean instance from which you want to obtain the data. This must be the name of a valid bean that was instantiated by using the <jsp:useBean> action.

  • property—Specifies the property within the specified bean that you want to output.

So, using the instantiated today bean, you can return the current month and date by using the following JSP code:

<jsp:getProperty name="today" property="month" />
<jsp:getProperty name="today" property="date" />

If today is the 17th of February, this example outputs the information 2/17.

3. Using &lt;jsp:setProperty&gt; | Next Section Previous Section