SCWCD : 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">
</jsp:useBean>
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" />
Syntax:
<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"
or
<jsp:useBean id="name" scope="page|request|session|application" typeSpec >
[body]
</jsp:useBean>
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 | Attribute | Description |
|---|
| 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.
Examples:
<jsp:getProperty name="user" property="name" />
Syntax:
<jsp:getProperty name="name" property="propertyName" />
Table 8.2. jsp:getProperty attributes | Attribute | Description |
|---|
| 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="0" />
<jsp:setProperty name="results" property="row" value="<%= i/4 %>" />
Syntax:
<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 | Attribute | Description |
|---|
| 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.
|
Given a design goal, create a code snippet using the following standard actions:
jsp:include, jsp:forward, and jsp:param.
Action jsp:include.
A jsp:include action provides for the inclusion of static and dynamic
resources in the same context as the current page.
Inclusion is into the current value of out. The resource is
specified using a relativeURLspec that is interpreted in the
context of the web application (i.e. it is mapped).
The page attribute of both the jsp:include and the
jsp:forward actions are interpreted relative to the current JSP
PAGE, while the file attribute in an include
directive is interpreted relative to the current JSP FILE.
An included page cannot change the response status code or set headers. This
precludes invoking methods like setCookie. Attempts to invoke
these methods will be IGNORED. The constraint is equivalent to the one imposed on
the include method of the RequestDispatcher class.
A jsp:include action may have jsp:param subelements
that can provide values for some parameters in the request to be used for the inclusion.
Request processing resumes in the calling JSP page, once the inclusion is completed.
The flush attribute controls flushing. If true,
then, if the page output is buffered and the flush attribute is given
a true value, then the buffer is flushed prior to the inclusion,
otherwise the buffer is not flushed. The default value for the flush attribute is
false.
Examples:
<jsp:include page="/templates/copyright.html" />
The above example is a simple inclusion of an object. The path is interpreted
in the context of the Web Application. It is likely a static object, but it could be
mapped into, for instance, a servlet via web.xml.
Syntax:
<jsp:include page="urlSpec" flush="true|false" />
and
<jsp:include page="urlSpec" flush="true|false">
{ <jsp:param .... /> }*
</jsp:include>
The first syntax just does a request-time inclusion. In the second case, the
values in the param subelements are used to augment the request for the purposes
of the inclusion.
Table 8.4. jsp:include attributes | Attribute | Description |
|---|
| page |
The URL is a relative urlSpec. Relative paths are interpreted
relative to the current JSP page. Accepts a request-time attribute value
(which must evaluate to a String that is a relative URL
specification).
| | flush |
Optional boolean attribute. If the value is true, the buffer
is flushed now. The default value is false.
|
Action jsp:forward.
A jsp:forward action allows the runtime dispatch of the current
request to a static resource, a JSP page or a Java servlet class in the same context as
the current page. A jsp:forward effectively terminates the execution
of the current page.
The request object will be adjusted according to the value of the
page attribute.
A jsp:forward action may have jsp:param
subelements that can provide values for some parameters in the request to be
used for the forwarding.
If the page output is buffered, the buffer is CLEARED prior to forwarding.
If the page output is buffered and the buffer was flushed, an attempt to
forward the request will result in an IllegalStateException.
If the page output was unbuffered and anything has been written to it, an
attempt to forward the request will result in an IllegalStateException.
The following action might be used to forward to a static page based on some
dynamic condition:
<% String whereTo = "/templates/" + someValue; %>
<jsp:forward page="<%= whereTo %>" />
Syntax:
<jsp:forward page="relativeURLspec" />
and
<jsp:forward page="urlSpec">
{ <jsp:param .... /> }*
</jsp:forward>
Table 8.5. jsp:forward attributes | Attribute | Description |
|---|
| page |
The URL is a relative urlSpec. Relative paths are interpreted
relative to the current JSP page. Accepts a request-time attribute value
(which must evaluate to a String that is a relative URL
specification).
|
Action jsp:param.
The jsp:param element is used to provide key/value information.
This element is used in the jsp:include, jsp:forward,
and jsp:params elements. A translation error shall occur if the
element is used elsewhere.
When doing jsp:include or jsp:forward, the
included page or forwarded page will see the original request object, with the original
parameters augmented with the new parameters, with NEW VALUES TAKING PRECEDENCE over
existing values when applicable. The scope of the new parameters is the
jsp:include or jsp:forward call; i.e. in the
case of an jsp:include the new parameters (and values) will not apply
after the include. This is the same behavior as in the ServletRequest
include and forward methods.
For example, if the request has a parameter A=foo and a parameter
A=bar is specified for forward, the forwarded request shall
have A=bar,foo. Note that the NEW PARAM HAS PRECEDENCE.
Syntax:
<jsp:param name="name" value="value" />
This action has two mandatory attributes: name and
value. name indicates the name of the parameter,
and value, which may be a request-time expression, indicates its value.
|