How To Use JavaBeans in JSP?

JavaBeans are simple classes that are used to develop dynamic WebPages. JavaBeans are required to create dynamic web pages by using separate java classes instead of using java code in a JSP page. It provides getter and setter methods to get and set values of the properties. Using JavaBeans it is easy to share objects between multiple WebPages.

Java Bean Properties:

  1. getPropertyName () : It is used to read the property. This method is called accessor.
  2. setPropertyName (): It is used to write the property. This method is called mutator.

useBean Tag

<jsp:useBean> tag is used to instantiate a JavaBean or to locate existing bean instance and assign it to a variable name. Synatx of <jsp:useBean> action tag :

<jsp:usebean id=”bean name”  scope=”scope name”  class=”packageName.className”  beanName=”packageName.className”  type=” packageName.className”/>


  • id: It represents variable name assigned to id attribute of useBean tag
  • scope: It represents scope in which bean instance has to be located.Scopes may be page,request,session, and application.Default scope is page.
    • page : It indicates that bean can be used within JSP page until page sends response to client or forwards request to another resource.
    • request : It Indicates bean can be used from any JSP page that processing same request until JSP page sends response to the client.
    • session : It indicates that bean can be used from any JSP page invoked in the same session .The Page in which we create bean must have page directive with session=”true”.
    • application : It indicates that bean can be used from any JSP page in the same application.
  • class : It takes class name to create a bean instance if bean instance not present in given scope.It should have no-argument constructor and should not be an abstract class.
  • beanName : It takes class name or expression .
  • type : It takes a class or interface name, which can be used with class or beanName attribute. This attribute can be used with or without class or beanName.

useBean Example

Let us create a simple bean class


public class Person {
	private String firstName = null;
	private String lastName = null;
	private int age = 0;

	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;

	public int getAge() {
		return age;

	public void setAge(int age) {
		this.age = age;

Listing 2: beanExample.jsp

<%@ page language='java' contentType='text/html; charset=UTF-8'
<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' ''>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
<title>Use Bean Example</title>
<jsp:useBean id='date'  class='java.util.Date'/>
<jsp:useBean id='person'  class=''>
   <jsp:setProperty name='person' property='firstName'
   <jsp:setProperty name='person' property='lastName'
   <jsp:setProperty name='person' property='age'

<h2>Simple use of bean calling java.util.Date</h2>
Today is <%=date%>
<h2>Example for Accessing JavaBeans Properties</h2>
<p><b>Person First Name:</b>
   <jsp:getProperty name='person' property='firstName'/>
<p><b>Person Last Name:</b>
   <jsp:getProperty name='person' property='lastName'/>
<p><b>Person Age:</b>
   <jsp:getProperty name='person' property='age'/>

Execute the beanExample.jsp. Right click on beanExample.jsp and select Run > Run As. Following output would be seen:


Previous Tutorial : Session Tracking in JSP || Next Tutorial : Cookies in JSP

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!