How To Use JavaBeans in JSP?

SHARE & COMMENT :

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”/>

Where:

  • 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 Person.java:
Listing 1:Person.java

package javabeat.net.jsp.beans;

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'
    pageEncoding='UTF-8'%>
<!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=UTF-8'>
<title>Use Bean Example</title>
</head>
<body>
<jsp:useBean id='date'  class='java.util.Date'/>
<jsp:useBean id='person'  class='javabeat.net.jsp.beans.Person'>
   <jsp:setProperty name='person' property='firstName'
                    value='joe'/>
   <jsp:setProperty name='person' property='lastName'
                    value='smith'/>
   <jsp:setProperty name='person' property='age'
                    value='10'/>
</jsp:useBean>

<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>
<p><b>Person Last Name:</b>
   <jsp:getProperty name='person' property='lastName'/>
</p>
<p><b>Person Age:</b>
   <jsp:getProperty name='person' property='age'/>
</p>
</body>
</html>

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

jsp_javabeans

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

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better