Intoduction
In this article we discuss Java Beans and the jsp:useBean action tag used in JSP action elements.
Java Bean Class
This class is a Java class that should have the following conventions:
- It should be Serializable.
- It should have a no-arg constructor.
- It should provide methods to set and get the values of the properties, known as getter and setter methods.
Why we use
It is a reusable software component. It contains many objects in one single object, so we can access this object from multiple places. Moreover, it provides easy maintenance.
Example
In this example we have created two Java files (Student.java and Check.java) using the Eclipse IDE. In Student.java we have created two methods for getting the student roll_number and their names. The second Java class (Check.java) is used to provide an access; so, the Java Bean class can be accessed by the use of setter and getter methods.
For creating Java classes in Eclipse we need to right-click on the project name (for example javabean) then we must select the package and define the package name for our program. Then right-click on the package that we have created (for example mypack) then select the class then provide the class name there and start our work as in the following.
Student.java created using Eclipse IDE.
package mypack;
public class Student implements java.io.Serializable
{
private int roll_no;
private String sname;
public Student(){}
public void setSId(int roll_no)
{
this.roll_no=roll_no;
}
public int getSId()
{
return roll_no;
}
public void setSName(String sname)
{
this.sname=sname;
}
public String getSName()
{
return sname;
}
}
How to provide access to the Java Bean class?
For accessing the Java Bean class, we need to use getter and setter methods.
Check.java created using Eclipse IDE
package mypack;
public class Check
{
public static void main(String args[])
{
Student st=new Student();
st.setSName("Sandeep");
System.out.println(st.getSName());
st.setSId(25);
System.out.println(st.getSId());
}
}
Output
For running the Java file we need to determine in which file the main function exists. In our program main exists in the Check.java class. So we run the Check.java file.
To run Check.java we need to right-click on Check.java then select "Run As" then select "run as Java Application". The Eclipse console window shows the following output:
jsp:useBean action tag
This tag is used to instantiate or locate a Bean class. If the object of the Bean class is already created, then they don't create the Bean again. But if the object of the Bean is not created then it instantiates the Bean.
Attribute and Usage of this tag
id
used to identify the Bean in the specified scope.
scope
represents the scope of the Bean. It may be a page, request, session or application. The default scope is page.
Page
specifies that we can use the Bean within a JSP page. The default scope is page.
request
specifies that we can use this Bean from any JSP page that processes the same request. It has wider scope than page.
session
specifies that we can use the Bean from any JSP page in the same session whether it processes the same request or not. It hs a wider scope than request.
application
specifies that we can use this Bean from any JSP page in the same application. It has wider scope than session.
class
instantiated the specified Bean class (in other words creates an object of the Bean class) but it must have no-arg or no constructor and must not be abstract.
type
provides the Bean a data type if the Bean already exists in the scope. It is mainly used with class or beanName attribute. If we use it without class or beanName then no Bean is instantiated.
beanName
instantiates the Bean using java.beans.Beans.instantiate() method.
Example
In this example; we are simply invoking the method of the Bean class. First create a Bean class in Java (in other words SquareEx.java) then invoke it in JSP file (in other words SquareEx.jsp).
SquareEx.java
package mypack;
public class SquareEx
{
public int square(int n)
{
return n*n;
}
}
SquareEx.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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Print the square</title>
</head>
<body>
<jsp:useBean id="obj" class="mypack.SquareEx"/>
<%
int m=obj.square(46);
out.print("square of 46 is"+m);
%>
</body>
</html>
Output
Run the jsp file using "run as server" in Eclipse; the result is shown below: