English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JSP JavaBean

JavaBean is a special Java class written in Java and conforms to the JavaBean API specification.

Next, we give the unique features of JavaBean compared to other Java classes:

  • Provide a default no-argument constructor.

  • Needs to be serialized and implement the Serializable interface.

  • There may be a series of readable and writable properties.

  • There may be a series of getter or setter Method.

JavaBean Properties

The properties of a JavaBean object should be accessible. This property can be any valid Java data type, including custom Java classes.

The properties of a JavaBean object can be readable and writable, or read-only, or write-only. The properties of a JavaBean object are accessed through two methods provided by the JavaBean implementation class:

MethodDescription
getPropertyName() For example, if the property name is myName, then the method name should be written as getMyName() to read this property. This method is also called an accessor.
setPropertyName() For example, if the property name is myName, then the method name should be written as setMyName() to write this property. 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.

JavaBean Program Example

This is the StudentBean.java file:

package com.w3codebox;
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 StudentBean.java file (the last example will be used):

$ javac StudentsBean.java

compile to get StudentBean.class file, copy it to <JSP Project>/WebContent/WEB-INF/classes/com/w3codeboxas shown in the following figure:

Accessing JavaBean

The <jsp:useBean> tag can declare a JavaBean in 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 format of the <jsp:useBean> tag is as follows:

<jsp:useBean id="bean's name" scope="bean's scope" typeSpec/>

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 of other <jsp:useBean> in the same JSP file.

Next, a simple usage of the <jsp:useBean> tag is given:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<title>useBean Example</title>
</head>
<body>
<jsp:useBean id="date" /> 
<p>The date is: <%= date %>
</body>
</html>

It will produce the following result:

The date is: Tue Jun 28 15:22:24 CST 2016

accessing the properties of JavaBean objects

in <jsp:useBean> used in the tag body <jsp:getProperty/> tag to call getter method, using <jsp:setProperty/> tag to call setter The syntax format of the method is as follows:

<jsp:useBean id="id" scope="bean scope">
   <jsp:setProperty name="bean's id" property="propertyName">  
                    value="value"/>
   <jsp:getProperty name="bean's id" property="propertyName">/>
   ...........
</jsp:useBean>

The 'name' property refers to the 'id' property of the Bean. The 'property' property refers to the getter or setter method to be called.

The following is a simple example of using the above syntax to access properties:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<title>get and set properties example</title>
</head>
<body>
<jsp:useBean id="students" 
                   > 
   <jsp:setProperty name="students" property="firstName"
                    value="Xiaoqiang"/>
   <jsp:setProperty name="students" property="lastName" 
                    value="Wang"/>
   <jsp:setProperty name="students" property="age"
                    value="10"/>
</jsp:useBean>
<p>Student Name: 
   <jsp:getProperty name="students" property="firstName"/>
</p>
<p>Student Surname: 
   <jsp:getProperty name="students" property="lastName"/>
</p>
<p>Student Age: 
   <jsp:getProperty name="students" property="age"/>
</p>
</body>
</html>

The running result of the above JSP is as follows:

Student Name:  Xiaoqiang
Student Surname:  Wang
Student Age: 10