Bean Basics

I l @ ve RuBoard

A Bean is an encapsulated unit of data with accessor methods to set and get properties, and action methods to do things with the data. As a simple example, a user Bean might have properties such as lastName , firstName , city , state , and zipCode . It might also have action methods such as validateZipCode or saveToDatabase .

The only real requirement for a Bean is that it implements a get and set method of each property that it contains. The methods must start with get or set . That's the basic definition of a Bean.

Let's look at a simple Bean that implements user data in Listing 2.1.

Listing 2.1 User.java
 package com.cartapp.user; public class User {     protected String lastName;     protected String firstName;     public String getLastName() {      return lastName;     }     public void setLastName(String lname) {      lastName = lname;     }     public String getFirstName() {      return firstName;     }     public void setFirstName(String fname) {      firstName = fname;     } } 

Obviously, a real user object would store a lot more information, as you'll see later in the chapter. However, this example lets you see how to access data in a Bean. You should always make the actual class variables holding the data protected because good encapsulation methodology indicates that all outside access should be through the methods.

I l @ ve RuBoard


MySQL and JSP Web Applications. Data-Driven Programming Using Tomcat and MySQL
MySQL and JSP Web Applications: Data-Driven Programming Using Tomcat and MySQL
ISBN: 0672323095
EAN: 2147483647
Year: 2002
Pages: 203
Authors: James Turner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net