Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JSP JavaBean

JSP JavaBean

A JavaBean is a special Java class, written in the Java language and conforming to the JavaBean API specification.

The following are the unique characteristics of JavaBeans compared with other Java classes:


JavaBean Properties

The properties of a JavaBean object should be accessible. Such a property 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 is named myName, the method should be named getMyName() to read the property. This method is also called an accessor.
setPropertyName() For example, if the property is named myName, the method should be named setMyName() to write the property. This method is also called a mutator.

A read-only property provides only the getPropertyName() method, and a write-only property provides only the setPropertyName() method.


JavaBean Program Example

This is the StudentBean.java file:



package com.runoob;



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 (used in the last example):



$ javac StudentsBean.java

After compiling, you will obtain the StudentBean.class file, copy it to the /WebContent/WEB-INF/classes/com/runoobas shown in the following figure:


Accessing JavaBeans

jsp:useBean The tag can declare a JavaBean in a JSP and then use it. Once declared, the JavaBean object becomes a scripting variable that can be accessed through scripting elements or other custom tags.jsp:useBean The syntax of the tag is as follows:





Among these, depending on the situation, the value of 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.

The following is a jsp:useBean simple example of using the tag:



<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>





useBean 实例







 

日期为:<%= date %>

It will produce the following result:



日期为:Tue Jun 28 15:22:24 CST 2016


Accessing JavaBean Properties

in jsp:useBean Inside the tag body, use the jsp:getProperty/ tag to call the getter method, and use the jsp:setProperty/ tag to call the setter method, with the following syntax:





   

   

   ...........



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.

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



<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>





get 和 set 属性实例







 

   

   

   





学生名字:

学生姓氏:

学生年龄:

Access the above JSP, and the result is as follows:



学生名字: 小强



学生姓氏: 王



学生年龄: 10