UseBean In JSP

UseBean In JSP section describes you how to locate or instantiate the Bean class in JSP.

UseBean In JSP

UseBean In JSP

In this section we will learn about the jsp:useBean action tag in JSP.

<jsp:useBean> is an action tag in JSP that locates or instantiates the Bean class. The created bean object is defined by its scope i.e. if the bean object of a Bean class is created for a specific scope (for example, request, session, page, application) with the specified name then it doesn't create the bean again. But, if it doesn't find the specified bean object then it instantiates the bean.

JavaBeans is a class that is written using Java code and follows the JavaBeans API specifications. A JavaBeans should implement the Serializable interface, it should have the properties that can be read and/or written, it should declare the setter/getter method for the properties.

<jsp:useBean> tag is used to access the JavaBeans in a JSP page. This tag declares the JavaBean in a JSP page. Using the jsp:useBean you can access the property/properties of a JavaBeans. You can use the jsp:setProperty tag to set the property/properties value and jsp:getProperty tag to get the property/properties value. For this you have to define the setter and/or getter methods of the properties in JavaBeans.

Syntax of <jsp:useBean>

<jsp:useBean id= "nameOfInstance" scope= "page | request | session | application" class= "package.class" type= "package.class  beanName="package.className | <%= expression>" ></jsp:useBean>

Following is the description of Attributes that are used in jsp:useBean tag :

  • id : This is a required attribute which specifies the identification of the bean in the given scope.
     
  • scope :
  • This is not a required attribute. This attribute specifies the availability/useability of the specified bean in the specified scope. Default value of this attribute is page.
     
    • page : This is a default value of the scope attribute which specifies that the given bean can be used within the page.
       
    • request : request value of the scope attribute of <jsp:useBean> tag specifies that the given bean can be used from any page but, within the same request process.
       
    • session : session value of the scope attribute of <jsp:useBean> tag specifies that the given bean can be used within the same session from any page. It doesn't matter request process is same or not.
       
    • application : application value of the scope attribute of <jsp:useBean> tag specifies that the given bean can be used within the same application.
       
  • class : class attribute of the <jsp:useBean> tag specifies the bean class which object is to be created or instantiated. But, this class doesn't have the argument or constructor and it mustn't be the abstract class.
     
  • type : type attribute of the <jsp:useBean> tag can be used to provide the data type to a bean that is already existed. Type attribute is an optional attribute which should be used with the class and/or beanName attribute because, the bean will be not instantiated if these attributes are not used.
     
  • beanName : This attribute is used to instantiates the bean. To instantiate a bean it uses java.beans.Beans.instantiate() method.

Example

Here I am giving simple examples which will demonstrate you about how to use the <jsp:useBean> tag in JSP. We will try to show you that how you can use the jsp:useBean tag in JSP, access the properties, etc. Here we will create different JSP pages where we will use the <jsp:useBean> tag and then we will do the specified tasks, said above. At first I have created two packages one net.roseindia and the other net.roseindia.bean. I have created a class in the net.roseindia package named DateExample inside which I have create a method named todayDate() which will return a String as date. Then I have created a JSP page named dateExampl.jsp file inside which I have used the <jsp:useBean> tag for instantiating a bean. Then I have used the useBean id for accessing the method of Bean class. In the another package I have created a bean class called NameBean which contains some properties and their setter getter method. Then I have create a JSP file named beanExample.jsp inside which I have created a HTML form for input the field values. Then I have used the <jsp:useBean> tag and instantiated the bean with the name pageBean then I have used this id to access the property at the JSP page.

Directory Structure

DateExample.java

package net.roseindia;

import java.util.Calendar;
public class DateExample {
	
	public String todayDate(Calendar cal)
	{
		int day = cal.get(Calendar.DAY_OF_MONTH);
		int month = cal.get(Calendar.MONTH)+1; //month is count from 0 to 11
		int year = cal.get(Calendar.YEAR);	
		
		return ""+day+"/"+month+"/"+year;
	}
}

dateExample.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.Calendar" %>
<%@ page import="net.roseindia.DateExample" %>
<jsp:useBean id="dt" class="net.roseindia.DateExample"/>
<!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>Date Example</title>
</head>
<body>
<%
Calendar cal = Calendar.getInstance();
String date = dt.todayDate(cal);
%>
Today is : <%=date %>
</body>
</html>

Output :

When you will compile and deploy the dateExample.jsp page then the output will be as follows :

NameBean.java

package net.roseindia.bean;

public class NameBean {
	
	String firstName, lastName;

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
}

beanExample.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="net.roseindia.bean.NameBean" %>
<jsp:useBean id="pageBean" class="net.roseindia.bean.NameBean" scope="page" />
<!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>Bean Example</title>
</head>
<body>
<%
String firstName = request.getParameter("firstName").toUpperCase(); 
String lastName = request.getParameter("lastName").toUpperCase();
if(firstName != null) 
{
%>
<jsp:setProperty name="pageBean" property="firstName" value="<%=firstName %>"/>
<jsp:setProperty name="pageBean" property="lastName" value="<%=lastName %>"/>
<H3> Welcome to the UseBean JSP </H3>
<P><B>Page bean: </B></P>
<% if (pageBean.getFirstName().equals("")) { %>
I don't know you. 
<% } else { %>
Hello <%= pageBean.getFirstName() %> <%=pageBean.getLastName() %> !
<% } %>
<%}//if closed %>

<P>Fill The Following Fields :</P>
<form method="get">
<table>
<tr>
<td>Enter Your First Name </td>
<td><input type="text" name=firstName size = 20></td>
</tr>
<tr>
<td>Enter Your Last Name</td>
<td><input type="text" name="lastName"/></td>
</tr>
<tr>
<td></td><td><input type ="submit" value="Submit"></td>
</tr>
</table>
</FORM>
</body>
</html>

Output :

When you will compile and deploy the beanExample.jsp then the output will be as follows :

The first page of the output will be as follows :

When you will entered the fields value and click on the submit button then the output will be as follows :

Download Source Code