Chapter 8. Building JSP Pages Using Standard Actions

Given a design goal, create a code snippet using the following standard actions: jsp:useBean (with attributes: 'id', 'scope', 'type', and 'class'), jsp:getProperty, and jsp:setProperty (with all attribute combinations).

Action jsp:useBean.

A jsp:useBean action associates an instance of a Java programming language object defined within a given scope and available with a given id with a newly declared scripting variable of the same id.

When a jsp:useBean action is used in an scriptless page, or in an scriptless context (as in the body of an action so indicated), there are no Java scripting variables created but instead an EL variable is created.

The jsp:useBean action is quite flexible; its exact semantics depends on the attributes given. The basic semantic tries to find an existing object using id and scope. If the object is not found it will attempt to create the object using the other attributes.

It is also possible to use this action to give a local name to an object defined elsewhere, as in another JSP page or in a servlet. This can be done by using the type attribute and not providing class or beanName attributes.

At least ONE of type and class MUST be present, and it is NOT VALID to provide both class and beanName. If type and class are present, class must be assignable to type (in the Java platform sense). For it not to be assignable is a translation time error.

The attribute beanName specifies the name of a Bean, as specified in the JavaBeans specification. It is used as an argument to the instantiate method in the java.beans.Beans class. It must be of the form a.b.c, which may be either a class, or the name of a resource of the form a/b/c.ser that will be resolved in the current ClassLoader. If this is not true, a request-time exception, as indicated in the semantics of the instantiate method will be raised. The value of this attribute can be a request-time attribute expression.

In the following example, a Bean with name connection of type com.myco.myapp.Connection is available after actions on this element, either because it was already created and found, or because it is newly created.

<jsp:useBean id="connection" class="com.myco.myapp.Connection" />

In the next example, the timeout property is set to 33 if the Bean was instantiated:

<jsp:useBean id="connection" class="com.myco.myapp.Connection">
	<jsp:setProperty name="connection" property="timeout" value="33">

In the following example, the object should have been present in the session. If so, it is given the local name wombat with WombatType. A ClassCastException may be raised if the object is of the wrong class, and an InstantiationException may be raised if the object is not defined:

<jsp:useBean id="wombat" type="my.WombatType" scope="session" />



<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" |


<jsp:useBean id="name" scope="page|request|session|application" typeSpec >

In this case, the [body] will be invoked if the Bean denoted by the action is CREATED. Typically, the body will contain either scriptlets or jsp:setProperty tags that will be used to modify the newly created object, but the contents of the body are not restricted.

Table 8.1. jsp:useBean attributes

id The name used to identify the object instance in the specified scope’s namespace, and also the scripting variable name declared and initialized with that object reference. The name specified is case sensitive and shall conform to the current scripting language variable-naming conventions.
scope The scope within which the reference is available. The DEFAULT value is page. See the description of the scope attribute defined earlier herein. A translation error must occur if scope is not one of page, request, session or application.
class The fully qualified name of the class that defines the implementation of the object. The class name is case sensitive. If the class and beanName attributes are not specified the object must be present in the given scope.
beanName The name of a bean, as expected by the instantiate method of the java.beans.Beans class. This attribute can accept a request-time attribute expression as a value.
type If specified, it defines the type of the scripting variable defined. This allows the type of the scripting variable to be distinct from, but related to, the type of the implementation class specified. The type is required to be either the class itself, a superclass of the class, or an interface implemented by the class specified. The object referenced is required to be of this type, otherwise a java.lang.ClassCastException shall occur at request time when the assignment of the object referenced to the scripting variable is attempted. If unspecified, the value is the same as the value of the class attribute.

Action jsp:getProperty.

The jsp:getProperty action places the value of a bean instance property, converted to a String, into the implicit out object, from which the value can be displayed as output. The bean instance must be defined as indicated in the name attribute before this point in the page (usually via a jsp:useBean action).

The conversion to String is done as in the println methods, i.e. the toString() method of the object is used for Object instances, and the primitive types are converted directly.

If the object is not found, a request-time exception is raised.

The value of the name attribute in jsp:setProperty and jsp:getProperty will refer to an object that is obtained from the pageContext object through its findAttribute method.

The object named by the name must have been "introduced" to the JSP processor using either the jsp:useBean action or a custom action with an associated VariableInfo entry for this name. If the object was not introduced in this manner, the container implementation is recommended (but not required) to raise a translation error, since the page implementation is in violation of the specification.

NOTE, a consequence of the previous paragraph is that objects that are stored in, say, the session by a front component are not automatically visible to jsp:setProperty and jsp:getProperty actions in that page unless a jsp:useBean action, or some other action, makes them visible.

If the JSP processor can ascertain that there is an alternate way guaranteed to access the same object, it can use that information. For example it may use a scripting variable, but it must guarantee that no intervening code has invalidated the copy held by the scripting variable. The truth is always the value held by the pageContext object.


<jsp:getProperty name="user" property="name" />



<jsp:getProperty name="name" property="propertyName" />


Table 8.2. jsp:getProperty attributes

name The name of the object instance from which the property is obtained.
property Names the property to get.

Action jsp:setProperty.

The jsp:setProperty action sets the values of properties in a bean. The name attribute that denotes the bean must be defined before this action appears.

There are two variants of the jsp:setProperty action. Both variants set the values of one or more properties in the bean based on the type of the properties. The usual bean introspection is done to discover what properties are present, and, for each, its name, whether it is simple or indexed, its type, and the setter and getter methods.

Properties in a Bean can be set from one or more parameters in the request object, from a String constant, or from a computed request-time expression. Simple and indexed properties can be set using jsp:setProperty.

When assigning values to indexed properties the value must be an array.

The following two actions set a value from the request parameter values:

<jsp:setProperty name="request" property="*" />
<jsp:setProperty name="user" property="user" param="username" />

The following two elemenst set a property from a value:

<jsp:setProperty name="results" property="col" value="${i mod 4}" />
<jsp:setProperty name="results" property="row" value="<%= i/4 %>" />



<jsp:setProperty name="beanName" prop_expr />

prop_expr ::=
	property="*" |
	property="propertyName" |
	property="propertyName" param="parameterName" |
	property="propertyName" value="propertyValue"
	propertyValue ::= string


Table 8.3. jsp:setProperty attributes

name The name of a bean instance defined by a jsp:useBean action or some other action. The bean instance must contain the property to be set. The defining action must appear before the jsp:setProperty action in the same file.
property The name of the property whose value will be set. If propertyName is set to '*' then the tag will iterate over the current ServletRequest parameters, matching parameter names and value type(s) to property names and setter method type(s), setting each matched property to the value of the matching parameter. If a parameter has a value of "", the corresponding property is not modified.
param The name of the request parameter whose value is given to a bean property. The name of the request parameter usually comes from a web form. If param is omitted, the request parameter name is assumed to be the same as the bean property name. If the param is not set in the Request object, or if it has the value of "", the jsp:setProperty action has no effect. An action MAY NOT have both param and value attributes.
value The value to assign to the given property. This attribute can accept a request-time attribute expression as a value. An action MAY NOT have both param and value attributes.

Professional hosting     Belorussian informational portal         Free SCBCD 1.3 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     Free SCJP 5.0 (Tiger) Study Guide     Free Mock Exam Engine     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide