13.2. Example: StringBean

Listing 13.1 presents a simple class called StringBean that is in the coreservlets package. Because the class has no public instance variables (fields) and has a zero-argument constructor since it doesn't declare any explicit constructors, it satisfies the basic criteria for being a bean. Since StringBean has a method called getMessage that returns a String and another method called setMessage that takes a String as an argument, in beans terminology the class is said to have a String parameter called message.

Listing 13.2 shows a JSP file that uses the StringBean class. First, an instance of StringBean is created with the jsp:useBean action as follows:

 <jsp:useBean id="stringBean" class="coreservlets.StringBean" /> ...

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.