View RSS Feed

My Java Tips

Using Java Beans in JSP

Rate this Entry
by , 05-24-2011 at 02:42 PM (3653 Views)
JavaBeans are classes written in the Java programming language conforming to a particular convention.

Consider the scenario: you are writing a JSP and you wish to write pure Java code in it. You may use scriptlets within JSP.

Java Code:
// Java code goes here
Using scriptlets means that your JSP will contains business logic as well which is not a good design approach. Ideally you would like to keep business logic separately in Java classes.

Here you can use reusable components called JavaBeans. JavaBeans in reality are simple Java classes very much like ActiveX controls: they provide known functionality and are
designed for off-the-shelf reuse.

You may access JavaBean's value through a set of properties, which provides access to the JavaBean's settings. For instance: Student is a JavaBean and student id, name, age, majors are properties.

Now lets get into details. JavaBean are written before JSP. You put the business logic in JavaBeans and then write JSP to use the beans where required. The first step is to tell the JSP page that it intends to use a bean.

<jsp:useBean> tag is used for this purpose. For example:

Java Code:
<jsp:useBean id="localName" class="com.test.Student" scope="application" />.
The <jsp:useBean> tag's id attribute is used to identify the bean. Bean will not be referred with this id through out the page. The class attribute is used to tell the page where to find the bean. You also must define the scope of bean using scope attribute. Using scope attribute, we tell the bean to maintain its information for the single page (the default) [scope="page"]; for the request [scope="request"]; for the session [scope="session"]; or for the whole application [scope="application"].

JavaBean's session scope is used to maintain items such as shopping carts very easily within JSP pages.

Now JavaBean is declared, we need to access its properties.

Java Code:
<jsp:getProperty> tag is used to get the value of a property.
With the <jsp:getProperty> tag, we need to specify the following:

  • name of the bean to use (from the id field of useBean)
  • the property whose value you want to get

For instance:

Java Code:
<jsp:getProperty id="localName" property="name" />.
To put value to JavaBean, we use <jsp:setProperty> tag. The requirements for this are as follwos:

  • identify the bean
  • identify the property to modify
  • provide the new value

If named properly, these can bedirectly acquired from a submitted form:

Java Code:
<jsp:setProperty id="localName" property="*" />;
acquired from a parameter, in which you have to name the property and parameter directly:

Java Code:
<jsp:setProperty id="localName" property="address" param="parameterName" />;
or just directly set with a name and value:

Java Code:
<jsp:setProperty id="localName" property="serialNumber" value="string" /> 
// or
<jsp:setProperty id="localName" property="serialNumber" value= <%= expression %> />.
How can web server find you JavaBeans. You need to place their class files in a special place. Normally for Tomcat and JBoss, you need to place then under WEB-INF/classes folder.

Submit "Using Java Beans in JSP" to Facebook Submit "Using Java Beans in JSP" to Digg Submit "Using Java Beans in JSP" to Submit "Using Java Beans in JSP" to StumbleUpon Submit "Using Java Beans in JSP" to Google