JSP JavaBean
JavaBean is a special Java class, written in Java and adhering to the JavaBean API specification.
The following are unique characteristics of JavaBean compared to other Java classes:
- Provides a default no-argument constructor.
- Must be serializable and implement 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. These properties can be any valid Java data type, including custom Java classes.
The properties of a JavaBean object can be readable and writable, read-only, or write-only. The properties of a JavaBean object are accessed through two methods provided in the JavaBean implementation class:
Method | Description |
---|---|
getPropertyName() | For example, if the property name is myName, this method should be named getMyName() to read the property. This method is also known as an accessor. |
setPropertyName() | For example, if the property name is myName, this method should be named setMyName() to write the property. This method is also known as a mutator. |
A read-only property only provides the getPropertyName() method, and a write-only property only provides the setPropertyName() method.
JavaBean Program Example
Here is the StudentBean.java file:
package com.tutorialpro;
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 (it will be used in the final example):
$ javac StudentsBean.java
After compiling, you will get the StudentBean.class file. Copy it to <JSP project>/WebContent/WEB-INF/classes/com/tutorialpro, as shown below:
Accessing JavaBean
The <jsp:useBean>
tag can declare a JavaBean in a JSP and then use it. After declaration, the JavaBean object becomes a script variable that can be accessed through script elements or other custom tags. The syntax for the <jsp:useBean>
tag is as follows:
<jsp:useBean id="bean name" scope="bean scope" typeSpec/>
Where, depending on the situation, the scope can be page, request, session, or application. The id value can be anything as long as it is not the same as the id value in other <jsp:useBean>
tags in the same JSP file.
Here is a simple example of using the <jsp:useBean>
tag:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<title>useBean Example</title>
</head>
<body>
<jsp:useBean id="date" class="java.util.Date" />
<p>The date is: <%= date %>
</body>
</html>
It will produce the following result:
The date is: [current date]
Date: Tue Jun 28 15:22:24 CST 2016