Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JSP JavaBean


May 12, 2021 JSP


Table of contents


JSP JavaBean

JavaBean is a special Java class, a re-usable component written in the Java language, and adheres to the Java Beans API specification.

Here's what's unique about JavaBean compared to other Java classes:

  • Provides a default non-parameter constructor.
  • The Serializable interface needs to be serialized and implemented.
  • There may be a series of readable and writeable properties.
  • There may be a series of "getter" or "setter" methods.

JavaBeans properties

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

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

Method Describe
get PropertyName () For example, if the name of a property is myName, the name of the method is written as getMyName() to read the property. This method is also known as an accessor.
set PropertyName () For example, if the name of a property is myName, the name of the method is written as setMyName() to write the property. This method is also known as a writer.

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


Example of a JavaBeans program

This is a document from .java Bean:

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 .java file, which will be used in the last example of this chapter.


Visit Java Beans

The useBean tag can declare a JavaBean in JSP and then use it. A fter the declaration, the JavaBean object becomes a script variable that can be accessed through script elements or other custom labels. T he syntax format of the label is as follows:

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

Where, depending on the situation, the value of scope can be page, request, session, or application. The id value can be as long as it is not the same as the other id values in the same JSP file.

Here's a simple use of the hashtag:

<html>
<head>
<title>useBean Example</title>
</head>
<body>

<jsp:useBean id="date" class="java.util.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

Access the properties of the Java Beans object

The getter method is called by using the hashtag in the body of the label, and the setter method is called by using the tag, and the setter method is called using the slt;jsp:setProperty/sgt; tag, in the syntax format as follows:

<jsp:useBean id="id" class="bean's class" 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 property refers to the id property of the bean. The property property property refers to the getter or setter method that you want to call.

Here's a simple example of using the above syntax for property access:

<html>
<head>
<title>get and set properties Example</title>
</head>
<body>

<jsp:useBean id="students"                      class="com.tutorialspoint.StudentsBean"> 
   <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 the SentBean .class classPATH environment variable, and then access the JSP above, with the following results:

Student First Name: Zara 

Student Last Name: Ali 

Student Age: 10