A.13. Using JavaBeans with JSP

Basic Requirements for Class to be a Bean

  1. Have a zero-argument (empty) constructor.

  2. Have no public instance variables (fields).

  3. Access persistent values through methods called getXxx (or isXxx) and setXxx.

Basic Bean Use

  • <jsp:useBean id="name" class="package.Class" />

  • <jsp:getProperty name="name" property="property" />

  • <jsp:setProperty name="name" property="property"
                     value="value" />
    

The value attribute can take a JSP expression.

Associating Properties with Request Parameters

  • Individual properties:

    <jsp:setProperty
        name="entry"
        property="numItems"
        param="numItems" />
    
  • Automatic type conversion: for primitive types, performed according to valueOf method of wrapper class.

  • All properties:

     <jsp:setPropert y name="entry" ...

Get Core Servlets and JavaServer Pages™ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.