JavaBean is a special Java class, a reusable component written in Java language, and complies with the JavaBeans API specification.
What follows are the unique features of JavaBean compared with other Java classes:
Provide a default no-argument constructor.
Needs to be serialized and implements the Serializable interface.
There may be a range of readable and writable properties.
There may be a series of "getter" or "setter" methods.
The properties of a JavaBean object should be accessible. This attribute 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 | describe |
|---|---|
| getPropertyName () | For example, if the name of the attribute is myName, then the name of the method should be written as getMyName() to read the attribute. This method is also called an accessor. |
| setPropertyName () | For example, if the name of the attribute is myName, then the name of the method should be written as setMyName() to write the attribute. This method is also called a writer. |
A read-only property only provides the getPropertyName() method, and a write-only property only provides the setPropertyName() method.
This is the StudentBean.java file:
package com.tutorialspoint;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(Integer age){ this.age = age; }}Compile the StudentBean.java file, which will be used in the final example of this chapter.
The <jsp:useBean> tag can declare a JavaBean in JSP and then use it. After declaration, JavaBean objects become script variables and can be accessed through script elements or other custom tags. The syntax format of the <jsp:useBean> tag is as follows:
<jsp:useBean id="bean's name" scope="bean's scope" typeSpec/>
Among them, depending on the specific situation, the value of scope can be page, request, session or application. The id value can be arbitrary as long as it is not the same as the id value in other <jsp:useBean> in the same JSP file.
What follows is a simple usage of the <jsp:useBean> tag:
<html><head><title>useBean Example</title></head><body><jsp:useBean id="date" /> <p>The date/time is <%= date %></body ></html>
It will produce the following results:
The date/time is Thu Sep 30 11:18:11 GST 2013
In the body of the <jsp:useBean> tag, use the <jsp:getProperty/> tag to call the getter method, and use the <jsp:setProperty/> tag to call the setter method. The syntax format is as follows:
<jsp:useBean id="id" scope="bean's scope"> <jsp:setProperty name="bean's id" property="property name" value="value"/> <jsp:getProperty name="bean's id" property ="property name"/> ...........</jsp:useBean>
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.
Next, a simple example of attribute access using the above syntax is given:
<html><head><title>get and set properties Example</title></head><body><jsp:useBean id="students" > <jsp:setProperty name="students" property="firstName" value ="Zara"/> <jsp:setProperty name="students" property="lastName" value="Ali"/> <jsp:setProperty name="students" property="age" value="10"/></jsp:useBean><p>Student First Name: <jsp:getProperty name="students" property="firstName"/></p><p>Student Last Name: <jsp: getProperty name="students" property="lastName"/></p><p>Student Age: <jsp:getProperty name="students" property="age"/></p></body></html >
Add StudentBean.class to the CLASSPATH environment variable, and then access the above JSP. The running results are as follows:
Student First Name: Zara Student Last Name: Ali Student Age: 10