Chapter 6. Using JavaBeans Components in JSP Pages

The JavaBeans specification defines a set of programming conventions for Java classes that should be used as pluggable components. In layman’s terms, tools that have no inside information about a class can use it if it’s developed according to these conventions. For instance, a GUI builder tool can support widgets developed as JavaBeans components. A JavaBeans component, or just a bean for short, is often used in JSP as the container for the dynamic content to be displayed by a web page. It typically represents something specific, such as a person, a product, or a shopping order. When JSP is combined with servlets, the bean can be created and initialized with data by the servlet and passed to a JSP page that simply adds the bean’s data to the response. But even in a pure JSP application, a bean is a useful tool, for instance for capturing and validating user input.

A programmer must develop the bean, but someone who doesn’t have any programming experience can then use it in a JSP page. JSP defines a number of standard actions for working with beans, and the JSTL Expression Language accepts beans as variables in expressions. In this chapter, we take a closer look at what a bean is and how it can produce dynamic content in a page. We’ll return to beans in Chapter 8 to see how they can be used for input validation.

What Is a Bean?

As I said earlier, a bean is simply a Java class that follows certain coding conventions, so it can be ...

Get JavaServer Pages, Second 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.