Wednesday, April 13, 2011

Chapter 40: Using jsp:useBean

In the previous chapter we saw an example as to how use JavaBeans in a JSP page. We used the jsp:useBean tag to accomplish that. It was just a straight-forward example. In this chapter we are going to cover the useBean tag in detail.

So, lets get started!!!

The jsp:useBean Tag

Whenever we want to include business logic and data in a JSP page, JavaBeans are the best way to do it. And to let us accomplish this we use the jsp:useBean tag.

When you use jsp:useBean, the container performs several actions. Basically, it instantiates the object in memory and provides a variable name for you to use within the scope you set in the tag. If the bean class hasn't been loaded, the container will try to locate it and load it. The container creates the bean and stores it as an attribute of the scope object. The value of the id attribute determines the name of the bean within the object scope. Using this name, you can access this bean inside your JSP.

The typical syntax of useBean usage is:
< jsp : useBean id="name" scope="page|request|session|application" typeSpec / >
typeSpec ::= class="className" |
class="className" type="typeName" |
type="typeName" class="className" |
beanName="beanName" type="typeName" |
type="typeName" beanName="beanName" |
type="typeName"

The id and scope are easy. The confusion always comes up about the usage of the typeSpec. The container will always look for the Bean. However, if the typeSpec is used one way, the container will throw an exception if it can't find that Bean (not already instantiated). If it is used another way, it will create a new instance of the Bean if one hasn't been created already.

The typical use of the jsp:useBean element is this:

< jsp : useBean ...>
Body
< / jsp : useBean >

If the bean has been instantiated already, the body is not executed! The Body portion of the jsp:useBean element is executed only when the bean is first instantiated. If the bean instance has already been loaded into memory from a previous jsp:useBean the body of the jsp:useBean element will not be executed. Since beans can be shared, jsp:useBean doesn't always newly instantiate a bean. The body is executed only once for a given bean, regardless of how many jsp:useBean elements refer to it. Also, you'll get an exception if more than one jsp:useBean refers to the same ID.

The body is executed upon instantiation but not after the bean is loaded, so we need to be careful about this. So, there are two behaviors of useBean: to instantiate an object or to retrieve an object as a Webapp attribute.

Let us quickly review the useBean attributes:

Attribute Name Attribute Definition
id The case sensitive name used to identify the object instance in the specified scope. This name doubles as the scripting variable name declared and initialized with that object reference.
scope The scope within which the reference is available. The default value is page. The options are page, request, session, and application.
class The fully qualified name of the class. If the class and beanName attributes are not specified the object must be present in the given scope. The class must not be abstract (or an interface) and must have a public, no-argument constructor, but the implicit no args constructor will suffice.
beanName The name of a Bean, as expected by the instantiate() method of the java.beans.Beans class. This name can be set dynamically by an expression as a value.
type If specified, it defines the type of the variable defined. The type is required to be the class itself, a superclass of the class, or an interface implemented by the class specified. If unspecified, the value is the same as the value of the class attribute.
When you use jsp:useBean, the container performs several actions. Basically, it instantiates the object in memory and provides a variable name for you to use within the scope you specified in the tag. If this JavaBean doesn't actually exist, the container will try to create it.

Below is what happens:

• An attempt is made to locate an object based on the attribute values id and scope.
• Container declares a variable for the class with the same name as the id. It has the same scope as declared in the tag.
• If the object is found, the variable's value is initialized with a reference to the located object, cast to the specified type. If the cast fails, a java.lang.ClassCastException shall occur. This ends the processing of the current jsp:useBean action.
• If the jsp:useBean element had a non-empty body, it is ignored. This ends the processing of this jsp:useBean action.
• If the object is not found in the specified scope and neither class nor beanName are given, a java.lang.InstantiationException shall occur. This ends the processing of this jsp:useBean action.
• If the object is not found in the specified scope, and the class specified names a non-abstract class that defines a public no-args constructor, the class is instantiated. The new object reference is associated with the scripting variable using the scope specified. After this, the last step listed below is performed.
• If the object is not found, and the class is abstract, an interface, or no public no-args constructor is defined, then a java.lang.InstantiationException shall occur. This ends the processing of the current jsp:useBean action.
• If the object is not found in the specified scope and beanName is given, then the container attempts to create the bean. If it succeeds, the new object reference is associated with the scripting variable in the specified scope.
• If the jsp:useBean element has a non-empty body, the body is processed. The variable is initialized and available within the scope of the body. The text of the body is treated as elsewhere. Any template text will be passed through to the out stream. Scriptlets and action tags will be evaluated. Usually, you use jsp:setProperty in here.

As you can see, the container does a lot of work for you so that you only need a few tags to perform helpful work.

Exam Trivia:
You write a JavaBean like you would any normal class. The class must meet a few criteria in order to be a JavaBean, including the following: It must be a public class, it must have a public constructor with no arguments, and it must have get and set methods for any properties you want accessed or mutated. The container handles the communication between the JSP page and the JavaBean. All you have to do is use the useBean, getProperty, and setProperty actions to get and set the attributes of the bean. Also, you can refer to the bean like any other class in scriptlets by the name specified in the ID attribute of the useBean element.

Previous Chapter: Chapter 39 - Creating JavaBeans

Next Chapter: Chapter 41 - Modifying Bean Properties

5 comments:

  1. i think something is missing here:
    "The typical use of the jsp:useBean element is this:


    Body"

    ReplyDelete
    Replies
    1. @ Michee - Thanks again for pointing these out. A simple problem with html code being ignored by the blogger editor. Fixed now.

      Anand

      Delete
  2. no problem! i like this blog very much!:)

    ReplyDelete
  3. Nice! Thank you. You answered my question regarding casting in JSP useBean...

    ReplyDelete

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers