Using Java Beans in JSP - JEE
Home » JEE, Jsp, Tech Notes

Using Java Beans in JSP

16 April 2009 One Comment

Java bean is a Plain Old Java Object (POJO) having few attributes and getter and setter methods in it. These POJOs can be used in JSPs to send data to the server. It reduces the amount of Java code that exists in a JSP. Also the JSP will contain the presentation details only. Data handling is moved to the Java bean in this approach. This can be achieved using following simple steps.

  • Create a Java bean
  • Create a JSP using Java bean

Let us take an example, to display customer name on customer jsp using Customer Java bean. This is how the Java bean will be.

com.myorg.jsptutorial.Customer.java

package com.myorg.jsptutorial;

public class Customer {

	private String name = "Java Bean Example";

	public String getName() {
		return name;
	}

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

}

The jsp page using this Java bean is -

<html>

<head>

<title>Customer Java Bean Example</title>

</head>

<jsp:useBean id="customer" scope="page" class="com.myorg.jsptutorial.Customer"></jsp:useBean>

<body>

  <h1>Name is: <jsp:getProperty name="customer" property="name"/></h1>

</body>

</html>

In this Jsp, we are using <jsp:useBean> tag.  Here, getProperty tag will call getName method on bean and retrieve the default value. This value will be displayed in the JSP. Finally the JSP will be presented like this.

Using Java Bean

<<Previous   Home   Next>>

 

More Related Posts in JEE, Jsp, Tech Notes

One Comment »

  • Vamsi Kuchi said:

    Given Information is clear and helped to refresh my skills

    Thanks for the Team

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.