Using Bean in JSP Page(jsp:useBean tag)

What is a Java Bean ?

Java Beans are java objects that have properties,which can be read via a get method or changed via a set method.A java bean should not have any public variables. All the variables should be accessed using the getter/setter methods.


Let us create a class Employee which will be used in JSP page .Employee class is an example of a basic bean.

What is a Java Bean ?

Java Beans are java objects that have properties,which can be read via a get method or changed via a set method.A java bean should not have any public variables. All the variables should be accessed using the getter/setter methods.


Let us create a class Employee which will be used in JSP page .Employee class is an example of a basic bean.



Employee.java
package com.javawebtutor.jsp;

public class Employee {
	private String name;
	private String department;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

}


How to use java beans in a JSP file ?

In JSP we can use java beans using <jsp:useBean> Tag.The jsp:useBean element instantiates an object of the class specified by class and binds it to a variable with the name specified by ID.

Syntax of jsp:useBean action tag :

Following is syntax of <jsp:useBean> .

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

Where

  • id :Holds the unique name that is assigned to the bean
  • type:Is an optional attribute,which specifies the type of the class.
  • class:is the class name of the bean.
  • beanName: Is the name of bean as supplied to the instantiate() method in java.beans.Beans class.
  • scope:Indicates the context in which the bean should be made available.There are four types of scope available.
  • page:This is default scope which indicates that the bean is only available on the current page.The bean can be used within the JSP page with the jsp:useBean tag or any other static include files until the page sends a response back or forwards request to another resource.
    request:The value of this object indicates that the bean is available for the current client request.The bean can be used within the JSP page processing the same request until a jsp page sends a response back or forwards request to another resource.
    session:The value of this object indicates that the bean is available to all pages during that share the same ServletContext.In other words the bean can be used from any jsp page in the same application.
    application:The value of this object indicates that the bean is only available for the current client request.The bean can be used within the JSP page processing the same request until a jsp page sends a response back or forwards request to another resource.

Following JSP standard actions is required to use Java bean in a JSP file.

  • <jsp:useBean>
  • <jsp:getProperty>
  • <jsp:setProperty>

Load Java bean inside a JSP :

To start working with java beans inside a jsp page ,the bean should be available into the page. Once the bean is available in jsp,the variable or properties of the bean can be accessed.

We need to use <jsp:useBean> tag to load a bean into JSP.The basic syntax is given below:

<jsp:useBean id="employee" class="com.javawebtutor.jsp.Employee" />

In above syntax Employee class is instantiated and binding it to a variable name specified in the "id " attribute.

Writing above syntax in a JSP page creates an object referencing to the class Employee and the name of the object is employee .

Getting the Properties of the Bean :

After the bean gets loaded into the page, the properties can be accessed using the <jsp:getProperty> standard actions.

The basic syntax of the <jsp:getProperty> is as follows:

<jsp:getProperty name="employee" property="name"/>

The above syntax tells the compiler to get the value of the variable " name " of the object " employee ". The attribute name in the above syntax represents the object created using the action. The value of the name attribute of the <jsp:getProperty> and the id attribute of the <jsp:useBean> property should be same.

For getting all the properties of the bean inside the jsp page the syntax of the <jsp:getProperty> should be

<jsp:getProperty name="employee" property="*"/>


Example of <jsp:getProperty> :


Step 1: Create Java Bean :

Create a java class inside com.javawebtutor.jsp package in eclipse and assign the name of the class as Employee .Declare and initialize two string variables " name " and " department ".

Employee.java
package com.javawebtutor.jsp;

public class Employee {
	private String name = "Mukesh";
	private String department = "Development";

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

}


Step 2 : Create a jsp page

Create a jsp page inside WebContet directory of the project and name it as index.jsp . Get the value of java Bean variable using <jsp:getProperty> tag.

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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Getting the Value of Bean</title>
</head>
<body bgcolor="#99CCFF">
	<jsp:useBean id="employee" class="com.javawebtutor.jsp.Employee" />
	<div>
		<h2> Employee details is mentioned below</h2>
	</div>
	<jsp:getProperty name="employee" property="name" /><br>
	<jsp:getProperty name="employee" property="department" />
</body>
</html>

Browser Output :

JSP useBean Action

Example of <jsp:setProperty> :

To modify (or) assign value to any variable of the bean object <jsp:setProperty> standard action is used . The basic syntax is as below

<jsp:setProperty name="employee" property="name" value="Mukesh"/>

Step 1: Create Java Bean :

Create a java class inside com.javawebtutor.jsp package in eclipse and assign the name of the class as Employee .Declare and initialize two string variables " name " and " department ".

Employee.java
package com.javawebtutor.jsp;

public class Employee {
	private String name;
	private String department;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

}

Step 2 : Create JSP Page:

Create a jsp page index.jsp and Load the bean using the <jsp:useBean> action and name the bean as " employee " using the " id " attribute.

Set the properties of the bean using the <jsp:SetProperty> and Print the variables again using the <jsp:getProperty> to check the new values of the bean.Code of index.jsp is given below.

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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Java Beans</title>
</head>
<body bgcolor="#008B8B">
	<jsp:useBean id="employee" class="com.javawebtutor.jsp.Employee" />
	<jsp:setProperty name="employee" property="name" value="Mukesh" />
	<jsp:setProperty name="employee" property="department" value="Java" />
	<div>
		<h3>Employee Details</h3>
	</div>
	<jsp:getProperty name="employee" property="name" /><br>
	<jsp:getProperty name="employee" property="department" />
</body>
</html>


Browser Output :

JSP useBean Action





comments powered by Disqus