A JavaBean is a special Java class, written in the Java language, that follows the JavaBean API specification.
Here are the unique characteristics of a JavaBean compared to other Java classes:
* Provides a default no-argument constructor.
* Needs to be serializable and implements the Serializable interface.
* May have a series of readable and writable properties.
* May have a series of getter or **setter** methods.
* * *
## JavaBean Properties
The properties of a JavaBean object should be accessible. This property can be any legal Java data type, including custom Java classes.
The properties of a JavaBean object can be read-write, read-only, or write-only. The properties of a JavaBean object are accessed through two methods provided in the JavaBean implementation class:
| **Method** | **Description** |
| --- | --- |
| get**PropertyName**() | For example, if the property name is myName, then this method should be named getMyName() to read this property. This method is also called an accessor. |
| set**PropertyName**() | For example, if the property name is myName, then this method should be named setMyName() to write to this property. This method is also called a mutator. |
A read-only property only provides a getPropertyName() method, and a write-only property only provides a setPropertyName() method.
* * *
## JavaBean Program Example
This is the StudentBean.java file:
package com.tutorial;public class StudentsBean implements java.io.Serializable{ private String firstName = null; private String lastName = null; private int age = 0; public StudentsBean() { } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public void setFirstName(String firstName){ this.firstName = firstName; } public void setLastName(String lastName){ this.lastName = lastName; } public void setAge(int age) { this.age = age;}}
Compile the StudentBean.java file (the last example will use it):
$ javac StudentsBean.java
After compilation, you get the **StudentBean.class** file. Copy it to **/WebContent/WEB-INF/classes/com/tutorial**, as shown in the following image:
!(#)
* * *
## Accessing JavaBean
The tag can declare a JavaBean in a JSP and then use it. After declaration, the JavaBean object becomes a script variable and can be accessed through script elements or other custom tags. The syntax format of the tag is as follows:
Where, depending on the situation, the value of scope can be page, request, session, or application. The id value can be any value as long as it is not the same as the id value in other tags in the same JSP file.
Here is a simple usage example of the tag:
useBean Example The date is:
It will produce the following result:
The date is: Tue Jun 28 15:22:24 CST 2016
* * *
## Accessing Properties of a JavaBean Object
Use the **** tag to call the **getter** method and the **** tag to call the **setter** method within the body of the **** tag. The syntax format is as follows:
...........
The name attribute refers to the id attribute of the Bean. The property attribute refers to the getter or setter method you want to call.
Here is a simple example of property access using the above syntax:
Get and Set Property Example Student First Name:
Student Last Name:
Student Age:
Accessing the above JSP, the result is as follows:
Student First Name: Xiaoqiang
Student Last Name: Wang
Student Age: 10