Back to: JSP Tutorials for Beginners and Professionals
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.
- What is JavaBeans in JSP?
- Why do we need to use JavaBeans in JSP?
- Working of JavaBeans in JSP
- Advantages and Disadvantages of JavaBeans
- How to access JavaBeans in JSP Application?
- 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:
- The class should be public and must not be final, abstract.
- Recommend to implement serializable.
- Must have direct/indirect param constructor.
- Member variables should be non-static and private.
- 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?
- It provides a default, no-argument constructor.
- It should be serializable and implement the Serializable interface.
- It may have a number of properties/variables which can be read or written.
- 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.
Advantages of Java Beans
- It is easy to reuse the software components.
- The properties and methods of Java Bean can be exposed to another application.
Disadvantages of Java Beans
- JavaBeans are mutable objects so it cannot take advantage of immutable objects.
- JavaBeans will be in an inconsistent state because of creating the setter and getter method for each property separately.
Java Bean Properties
- getPropertyName(): This method is used to read the property which is also called accessor.
- 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:
- Id: It will take a variable to manage generated Bean object reference.
- Class: This attribute will take the fully qualified name of the Bean class.
- 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.
- 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:
- Name: It will take a variable that is the same as the id attribute value in <jsp:useBean> tag.
- 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:
- Name: It will take a variable that is the same as the id attribute value in <jsp:useBean> tag.
- Property: It will take a property name in order to access the respective setter method.
- 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
Note:
- <jsp:useBean> element is used to create and use a JavaBeans Component.
- <jsp:setProperty> element is the standard way to set JavaBeans component properties in a JSP page.
- 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.