Latest web development tutorials

JSP JavaBean

JavaBean is a special Java class, using the J ava language to write, and to comply with JavaBean API specification.

The next JavaBean is given in terms of unique features compared to other Java class:

  • Provide a default no-argument constructor.
  • It needs to be serialized and implements the Serializable interface.
  • There may be a series of read-write property.
  • There may be a series of "getter" or "setter" methods.

JavaBean properties

A JavaBean property object should be accessible. This property can be any valid Java data types, including custom Java classes.

A JavaBean property object may be read-write or read-only or write-only. JavaBean JavaBean property objects via classes implement two methods provide access to:

method description
get PropertyName () For example, if the name attribute is myName, then the name of this method must be written getMyName () to read this property. This method is also called an access device.
set PropertyName () For example, if the name attribute is myName, then the name of this method must be written setMyName () to write to this property. This method is also known writer.

A read-only attribute getPropertyName () method, a write-only property only setPropertyName () method.


JavaBean Program Example

This is StudentBean.java file:

package com.w3big;

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 (last instance will be used):

$ javac StudentsBean.java

Compile get StudentBean.class file, copy it to the <JSP project> / WebContent / WEB-INF / classes / com / w3big, as shown below:


Access JavaBean

<Jsp: useBean> tag can be declared a JavaBean in JSP, and then used. After the declaration, JavaBean objects became script variables can be accessed through scripting elements or other custom labels. <Jsp: useBean> tag syntax is as follows:

<jsp:useBean id="bean 的名字" scope="bean 的作用域" typeSpec/>

Wherein, depending on the circumstances, the value of scope can be page, request, session or application. id can be any value as long as no other files and the same JSP <jsp: useBean> id value in the same line.

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

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<title>useBean 实例</title>
</head>
<body>

<jsp:useBean id="date" class="java.util.Date" /> 
<p>日期为:<%= date %>

</body>
</html>

It will produce the following results:

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

Access JavaBean properties of an object

In the <jsp: useBean> tag body using <jsp: getProperty /> tag to invoke the getter method, use <jsp: setProperty /> tag to invoke the setter method syntax is as follows:

<jsp:useBean id="id" class="bean 编译的类" scope="bean 作用域">
   <jsp:setProperty name="bean 的 id" property="属性名"  
                    value="value"/>
   <jsp:getProperty name="bean 的 id" property="属性名"/>
   ...........
</jsp:useBean>

name attribute refers to the id attribute of the Bean. property attribute refers to the getter or setter method you want to call.

Next, using the above syntax is given access to the property with a simple example:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<title>get 和 set 属性实例</title>
</head>
<body>

<jsp:useBean id="students" 
                    class="com.w3big.StudentsBean"> 
   <jsp:setProperty name="students" property="firstName"
                    value="小强"/>
   <jsp:setProperty name="students" property="lastName" 
                    value="王"/>
   <jsp:setProperty name="students" property="age"
                    value="10"/>
</jsp:useBean>

<p>学生名字: 
   <jsp:getProperty name="students" property="firstName"/>
</p>
<p>学生姓氏: 
   <jsp:getProperty name="students" property="lastName"/>
</p>
<p>学生年龄: 
   <jsp:getProperty name="students" property="age"/>
</p>

</body>
</html>

Access over JSP, results are as follows:

学生名字: 小强

学生姓氏: 王

学生年龄: 10