Capturing Parameter Values Using a Bean

As you may remember from Chapter 6, a bean is often used as a container for data, created by some server process and used in a JSP page to display the data. But a bean can also be used to capture user input. The captured data can then be processed by the bean itself or used as input to some other server component (e.g., a component that stores the data in a database or picks an appropriate banner ad to display).

To capture the user input from the example form, I have implemented a bean named com.ora.jsp.beans.userinfo.UserInfoBean , with the properties described in Table 8-4.

Table 8-4. Properties for com.ora.jsp.beans.userinfo.UserInfoBean

Property name

Java type

Access

Description

userName

String

Read-write

The user’s full name

birthDate

String

Read-write

The user’s birth date in the format yyyy-mm-dd (e.g., 2002-01-23)

emailAddr

String

Read-write

The user’s email address in the format name@company.com

gender

String

Read-write

The user’s gender (m or f)

luckyNumber

String

Read-write

The user’s lucky number (between 1 and 100)

food

String[]

Read/write

The user’s favorite food (any combination of z, p, and c)

As shown in the “Access” column in Table 8-4, all properties are read/write, meaning that, in addition to using the bean’s properties to generate output, the property values can be set based on user input.

Example 8-4 shows the last part of a JSP page that uses the bean to capture the user input ...

Get JavaServer Pages, 3rd Edition 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.