JavaBeans in JSP

JavaBeans in JSP Application with Examples

In this article, I am going to discuss JavaBeans in JSP Application with Examples. Please read our previous article where we discussed How to convert XML to Server-Side Object in JSP Application. As part of this article, we are going to discuss the following pointers.

  1. What is JavaBeans in JSP?
  2. Why do we need to use JavaBeans in JSP?
  3. Working of JavaBeans in JSP
  4. Advantages and Disadvantages of JavaBeans
  5. How to access JavaBeans in JSP Application?
  6. JSP JavaBeans Example
What is JavaBeans in JSP?

Bean means component. Components mean reusable objects. JavaBean is a reusable component. Java Bean is a normal java class that may declare properties, setter, and getter methods in order to represent a particular user form on the server-side. JavaBean is a java class that is developed with a set of conventions. The conventions are:

  1. The class should be public and must not be final, abstract.
  2. Recommend to implement serializable.
  3. Must have direct/indirect param constructor.
  4. Member variables should be non-static and private.
  5. Every bean property should have one setter method and one getter method with the public modifier.

Note: setter method is useful to set data to bean property whereas the getter method is useful to read data from bean property.

Java Bean is useful to combine multiple simple values to an object and to send that object from one resource to another resource or one layer to another layer. For example, to keep form data into a single object we can use java bean. Java Bean is a specially constructed Java class written in Java and coded according to the Java Beans API specification.

Why do we need to use JavaBeans in JSP?
  1. It provides a default, no-argument constructor.
  2. It should be serializable and implement the Serializable interface.
  3. It may have a number of properties/variables which can be read or written.
  4. It may have a number of “getter” and “setter” methods for the properties.
Working of JavaBeans in JSP

First, the browser sends the request for the JSP page. Then the JSP page accesses Java Bean and invokes the business logic. After invoking the business logic Java Bean connects to the database and gets/saves the data. At last, the response is sent to the browser which is generated by the JSP.

Why do we need to use JavaBeans in JSP?

Advantages of Java Beans
  1. It is easy to reuse the software components.
  2. The properties and methods of Java Bean can be exposed to another application.
Disadvantages of Java Beans
  1. JavaBeans are mutable objects so it cannot take advantage of immutable objects.
  2. JavaBeans will be in an inconsistent state because of creating the setter and getter method for each property separately.
Java Bean Properties
  1. getPropertyName(): This method is used to read the property which is also called accessor.
  2. setPropertyName(): This method is used to write the property which is also called mutator.

How to access JavaBeans in JSP Application?

<jsp:useBean>

The jsp:useBean tag is utilized to start up an object of JavaBean or it can re-utilize the existing java bean object. The primary motivation behind jsp:useBean tag is to interface with bean objects from a specific JSP page. In this tag, it is consistently suggestible to give either application or meeting degree to the extension characteristic worth. At the point when the compartment experiences this label then the holder will get class characteristic worth for example completely qualified name of Bean class then the holder will perceive Bean .class record and perform Bean class stacking and launch. Subsequent to making the Bean object compartment will allot Bean object reference to the variable indicated as an incentive to id property. In the wake of getting Bean object reference holder will store Bean object in an extension indicated as an incentive to scope trait.

Syntax: <jsp:useBean id=”—” class”—” type”—” scope=”—”/>

Attributes:
  1. Id: It will take a variable to manage generated Bean object reference.
  2. Class: This attribute will take the fully qualified name of the Bean class.
  3. Type: It will take the fully qualified name of the Bean class to define the type of variable in order to manage the Bean object reference.
  4. Scope: It will take either of the JSP scopes to the Bean object.
<jsp:getProperty>

The jsp:getProperty tag is utilized to get indicated property from the JavaBean object. The principle reason for <jsp:getProperty> tag is to execute a getter strategy to get an incentive from the Bean object.

Syntax: <jsp:getProperty name=”—” property=”—”/>

Attributes:
  1. Name: It will take a variable that is the same as the id attribute value in <jsp:useBean> tag.
  2. Property: It will take a particular property to execute the respective getter method.
<jsp:setProperty>

The jsp:setProperty tag is utilized to set a property in the JavaBean object. The principle motivation behind jsp:setProperty tag is to execute a specific setter technique to set an incentive to a specific Bean property.

Syntax: <jsp:setProperty name=”—” property=”—” value=”—”/>

Attributes:
  1. Name: It will take a variable that is the same as the id attribute value in <jsp:useBean> tag.
  2. Property: It will take a property name in order to access the respective setter method.
  3. Value: It will take a value to pass as a parameter to the respective setter method.
JSP JavaBeans Example

In this example, we have created a Simple Bean class Test.java and index.jsp page which loads the bean and sets/gets a simple String Parameter.

Test.java
package Action;
public class Test {
 private String message = "No message specified";
 public String getMessage() {
  return (message);
 }

 public void setMessage(String message) {
  this.message = message;
 }
}
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Using JavaBeans in JSP</title>
</head>

<body>
 <h2>Using JavaBeans in JSP</h2>
 <jsp:useBean id="test" class="Action.Test" />
 <jsp:setProperty name="test" property="message" value="Hello JSP..." />

 <p>Got message....</p>
 <jsp:getProperty name="test" property="message" />
</body>
</html>
Output

How to access JavaBeans in JSP Application?

Note:
  1. <jsp:useBean> element is used to create and use a JavaBeans Component.
  2. <jsp:setProperty> element is the standard way to set JavaBeans component properties in a JSP page.
  3. To retrieve JavaBeans component properties we need to use unified EL expressions or by using the <jsp:getProperty> element.

In the next article, I am going to discuss TagLibrary in JSP. Here, in this article, I try to explain JavaBeans in JSP Applications and I hope you enjoy JavaBeans in JSP with Examples article.

Leave a Reply

Your email address will not be published.