In this article will look at JSP Standard Actions. We will cover the creation of a JSP Page using the standard actions in conjunction with JavaBeans. This is the third article of five on JavaServer Pages. In the first article, “Tutorial:Review of JavaServer Pages JSP for the Web Component Developer Exam”, we provide a review of JavaServer Pages and the functionality available to the developer to create dynamic content and template text and in the second article “Tutorial:Review of JSP Expression Language for Web Component Developer Exam” we look in depth at the use of unified expression language and implicit variables for JavaServer Pages and JavaServer Faces. This is part of a series of articles to help you prepare for the web component developer exam.

What are JSP Standard Actions

In order for you web application to produce a rich experience for your users, your application will need to produce dynamic content. Examples of dynamic content could be a list of products matching a search criterion on a online retail site, the list of web sites matching the search criterion on a search engine, or the contents of a shopping cart on an e-commerce site, a personalized news page, or your updated agenda from an online personal management system. The data providing the dynamic content can come from a variety of sources including a database, an XML document, an RSS feed, messaging systems, various web services, or data accumulated in memory based on previous requests. All of this dynamic data is then combined with standard HTML elements as well as custom elements, tags and widgets into a page with the providing a layout, navigation bars, branding, etc and once it is ready, it is returned tot he browser. Within the context of using JSP, standard HTML is considered template text. The dynamic data that has been generated on the web server through its interactions with numerous backend resources is then inserted at the relevant places in the template text using JSP standard action elements as well as custom action elements. In this article we will only focus on JSP standard actions. We will discuss custom actions and custom tags in the next article.

A JSP standard action is executed during the request processing phase when a JSP page is requested. JSP standard action elements represent the dynamic actions that will take place at runtime. Note that this differs significantly from JSP directives which are used only during the translation phase when the JSP page is being transformed into Java servlet code. A JSP standard action can be used to add text to the response. We will demonstrate this in an example that will be used throughout this article. This JSP will be able to write to a file on a server, send email as well as retrieve data from a database that is added to the response for other actions. Below we have created a JSP page that includes JSP standard action elements in it:

XML Code: JSP Page with JSP standard action elements
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Math JSP</title>
</head>
<body bgcolor="yellow">

    <h1>We can do calculations easily in a JSP ...</h1>
  
    <%-- Calculate the sum of 12 + 21 + 30 dynamically --%>
    12 + 21 + 30 = <c:out value="${12 + 21 + 30}" />

</body>
</html>
All standard actions are represented by HTML-like elements in the JSP page. If the action element has a body, it starts by an opening tag, sometimes with attribute/value pairs, then with a body, and finally a closing tag. The syntax for a JSP standard action with a body and attributes is shown below:

XML Code: Syntax for a Standard Action Element With Body
<prefix:action_name attr1="value1" attr2="value2">
  action_body
</prefix:action_name>
Note that the syntax for a JSP standard action element is identical to that of a HTML element. Also similarly to an HTML element, the body of an action element can contain text or other action elements. For a standard action element that doesn’t have a body, you can use the following syntax instead:

XML Code: Syntax for a Shortened Standard Action Element
<prefix:action_name attr1="value1" attr2="value2" />
Here, the element ends with a /> rather than with </prefix:action_name> because of the lack of a body in this element. This is just standard XML syntax where the shorthand serves as the equivalent of an opening tag, empty body and closing tag:

XML Code: Syntax for a Standard Action Element with Empty Body
<prefix:action_name attr1="value1" attr2="value2"></prefix:action_name>
JSP Standard Actions are referred to generally as action elements, or tags. They are grouped into tag libraries in order to facilitate developers understanding the functionality available in each group of tags. The element name that is used in the opening and closing tags consists of two parts: a prefix which you are free to choose according to your preferences and the action's name which is defined in the TLD. These two parts are separated by a colon with no space characters between any parts. In this definition we are once again using XML syntax. In this context, the prefix that is used serves as an XML namespace. As mentioned previously, you can define the namespace prefix that you prefer for the library of the taglib directive. This is shown in the previous listing:

XML Code: Example of Definition of Namespace for Tag Library
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    ...
    <c:out value="${12 + 21 + 30}" />
The prefix serves, first to make it possible for actions in different libraries to have the same name without causing collisions in terms of resolving which action to use, and second to make it possible for the container to figure out to which library a specific action belongs. When the container finds an action element, it first locates the taglib directive declaring the library corresponding to the action name prefix. The taglib directive's uri attribute is used as a unique identifier for the tag library. The container uses this to find the information it needs to process the action. Actions are grouped into three different categories. These are:
  • Standard Actions
  • Custom Actions
  • JSP Standard Tag Library


In this article we will focus on standard actions and in the next two articles in the series we will review custom actions and the JSP Standard library. Standard actions are actions that are an integral part of the JSP specification. The default prefix used for JSP standard actions is jsp. This prefix is fixed and cannot be changed. As such, behavior for all standard actions is defined by the specification, you don't need to declare standard actions with a taglib directive.

A standard action element consists of a start tag that can optionally have attributes, a body, and finally an end tag. You can insert template text as well as other JSP elements into the body. Below is an example:

XML Code: Example of Standard Action with JSP Elements
<jsp:forward page="productNext.jsp">
  <jsp:param name="aProduct" value="aProductId" />
</jsp:forward>
Similar to what we explained earlier, action elements that don't have a body, can use a shorthand notation where the start tag ends with "/>" instead of ">". This is shown in the previous listing with the <jsp:param> action. Remember that the action element name as well as it’s attribute names are case-sensitive.

There are some standard action attributes that can accept a request-time attribute value . This will be indicated in the tables outlining the different action elements as to whether they accept dynamic values. In this case, the attribute value can be specified by an EL or Java expression, or by a <jsp:attribute> element. This is shown in the example below:

XML Code: Example of jsp:attribute Element
<% String hdPage = templDir + "/header.jsp"; %>
<%-- Using a Java expression --%>
<jsp:include page="<%= hdPage %>" />

<% pageContext.setAttribute("var", hdPage);

<%-- Using an EL expression --%>
<jsp:include page="${var}" />

<%-- Using a <jsp:attribute> element --%>
<jsp:include>
  <jsp:attribute name="page">
    ${var}
  </jsp:attribute>
</jsp:include>
In the section below we will look in depth at each of the standard actions, their attributes attribute as well as whether it has attributes that accept a request-time attribute.

The Attribute Action Element

The <jsp:attribute> element is used to define an attribute value for another JSP action element. This is an alternative approach to entering the attribute value in the opening tag as in a regular XML attribute value or by defining a fragment input. Often it is used in conjunction with the <jsp:element> action to build up dynamically the template text markup element in a page. The syntax for <jsp:attribute> is provided below:

XML Code: Syntax of jsp:attribute Element
<jsp:attribute name="attributeName " [trim="true | false"]>
  Attribute value, usually created via nested JSP elements 
</jsp:attribute>
An example of the use of <jsp:attribute> is shown below. In this example we are defining an attribute to be used for generated markup:

XML Code: Example of jsp:attribute Element
<% - Example of setting a JSP action attribute value - %>
<jsp:include>
  <jsp:attribute name="page">
    ${theValue}
  </jsp:attribute>
</jsp:include>

<%-- Define an attribute for a generated markup element --%>
<jsp:element name="a">
  <jsp:attribute name="href">
    <c:url value="${theURL}" />
  </jsp:attribute>
</jsp:element>
<jsp:body>
In the table below, we provide the attributes, their types, whether they accept dynamic values and a description:

Tutorial:Review of JSP Actions for the Web Component Developer Exam-a21-jspattribute.jpg
Table: Attributes for the jsp:attribute Action

The Body Action Element

The <jsp:body> action is used to explicitly define the body of an action element. You use it only when the action attributes are defined by using one or more <jsp:attribute> elements. The syntax for <jsp:body> is provided below:

XML Code: Syntax of jsp:body Element
<jsp:body>
  Body content
</jsp:body>
In the example shown below, we demonstrate how to use the <jsp:body> element in conjunction with the <jsp:attribute> elements. This is done in relation to a sports video plugin.

XML Code: Example of jsp:body Element
<jsp:plugin type="applet" code="SportsVideo.class" 
  codebase="applet" jreversion="2.0">
  <jsp:attribute name="width">340</jsp:attribute>
  <jsp:attribute name="height">220</jsp:attribute>
  <jsp:body>
    <jsp:params>
      <jsp:param name="bgcolor" value="6b8fd4" />
    </jsp:params>
    <jsp:fallback>
      This plugin tag OBJECT or EMBED not supported by this version of browser.
    </jsp:fallback>
  </jsp:body>
</jsp:plugin>
Note that the <jsp:body> element doesn’t have any attributes.

The doBody Action

The <jsp:doBody> action is used to evaluate the body of the tag used by the calling page to invoke the particular tag file. It can only be used in a tag file. It evaluates the custom action body and either adds the output to the calling page's output stream or captures it within a variable. The syntax for <jsp:doBody> is provided below:

XML Code: Syntax of jsp:doBody Element
<jsp:doBody [var="var " | varReader="varReader "] 
  [scope="page | request | session | application"] />
In the first example we show how to use <jsp:doBody> in a situation where the output is added to the calling page’s output stream:

XML Code: Example of jsp:body Element without Capture
<jsp:doBody/>
In the second example we show how to use <jsp:doBody> where the output is captured within a variable:

XML Code: Example of jsp:body Element with Capture
<jsp:doBody var="result" />
In the table below, we provide the attributes for <jsp:doBody> as well as the attribute types, whether they accept dynamic values and a description for each:

Tutorial:Review of JSP Actions for the Web Component Developer Exam-a21-dobody.jpg
Table: Attributes for the <jsp:doBody> Action

The Element Action

The <jsp:element> is used to dynamically generate an XML element and add it to the response. This is very useful primarily in JSP Pages in XML syntax that are known as JSP Documents. In JSP Documents, you cannot use other approaches since the document must be well-formed and conforming to the relevant schema, if required. There are two forms of <jsp:element>. There is one with a body and a second shortened version where the <jsp:element> is without a body. The syntax for both forms of <jsp:element> is shown below:

With a body
XML Code: Syntax of jsp:element with Body
<jsp:element name="elementName">
  <jsp:attribute> and/or <jsp:body> actions
</jsp:element>
Without a body
XML Code: Syntax of jsp:element without Body
<jsp:element name="elementName" />
In the example below, we use the <jsp:element> to generate a <a href> html tag for future use:

XML Code: Example of jsp:element
<%-- Create <a href="mypage.jsp">Click Here </a> -- %>
<jsp:element name="a">
  <jsp:attribute name="href">smypage.jsp</jsp:attribute>
  <jsp:body>Click Here</jsp:body>
</jsp:element>
<jsp:fallback>
In the table below, we provide the attributes for <jsp:element> as well as the attribute types, whether they accept dynamic values and a description for each:

Tutorial:Review of JSP Actions for the Web Component Developer Exam-a21-element.jpg
Table: Attributes for <jsp:element> Action

The Fallback Action

The <jsp:fallback> action is used in conjunction with the <jsp:plugin> action as part of the body of the <jsp:plugin> action. You use the <jsp:fallback> actions to provide an message in the form of template text to use for browsers that don't support the HTML <embed> or <object> elements. The syntax for the <jsp:fallback> is shown below:

XML Code: Syntax of jsp:fallback
<jsp:fallback>
  Fallback body text
</jsp:fallback>
In the example provided below, we revisit the example shown for the <jsp:body> action and focus on the part of the related to the <jsp:plugin>. Here we shown the <jsp:fallback> action as part of the body of the <jsp:plugin tag> providing text indicating that the browser doesn’t support any of the valid plugin tags:

XML Code: Example of jsp:fallback
<jsp:plugin type="applet" code="SportsVideo.class" 
  codebase="applet" jreversion="2.0" width=”340” height=”220”>
    <jsp:fallback>
      This plugin tag OBJECT or EMBED not supported by this version of browser.
    </jsp:fallback>
</jsp:plugin>
Note that the <jsp:fallback> element doesn’t have any attributes.

The Forward Action

The <jsp:forward> action is used to forward a request to an HTML file, JSP page, or servlet. It passes the request processing control to another JSP page or servlet in the same web application. At the point of the transfer of request processing control, the execution of the current page is stopped, giving the target resource full control over the request. If there were response content presently buffered when the <jsp:forward> action is executed, the buffer would be cleared. If the response had already been partially sent to the browser, then the execution of the <jsp:forward> action would trigger an IllegalStateException.

There are two forms of <jsp:forward> action. One with a body and the second without a body. In both, the URI path information available in the request object is adjusted to reflect the URI path information for the target resource. As the other parts of the request information is not touched, the target resource will have access to all the original parameters and headers passed with the request. It is possible to pass additional parameters to the target resource through the use of the <jsp:param> elements in the <jsp:forward> element's body. The syntax for the two forms of JSP is shown below:

With nested <jsp:param> actions:
XML Code: Syntax of jsp:forward with Body
<jsp:forward page="pageOrContextURIPath" />
  <jsp:param name=”param1” value=”value1” />
  <jsp:param name=”param2” value=”value2” />
</jsp:forward>
Without parameters
XML Code: Syntax of &lt;jsp:forward&gt; without Body
<jsp:forward page="pageOrContextURIPath" />
An example is shown below of the use of the <jsp:forward> action being used to forward the user to another jsp:

XML Code: Example of jsp:forward
<jsp:forward page="search.jsp" />
In the table below, we provide the attributes for <jsp:forward> as well as the attribute types, whether they accept dynamic values and a description for each:

Tutorial:Review of JSP Actions for the Web Component Developer Exam-a21-forward.jpg
Table: Attributes for <jsp:forward> Action

The GetProperty Action

The <jsp:getProperty> action inserts the value of a bean property into the response. This action will take the value of the specified bean property, convert it to a string, and then add it to the response generated by the page. The syntax for <jsp:getProperty> is shown below:

XML Code: Syntax of jsp:getProperty
<jsp:getProperty name="beanVariableName" property="propertyName" />
An example of the use of <jsp:getProperty> for accessing the property of a bean cargo for the shipping manifest:

XML Code: Example of jsp:getProperty
<jsp:getProperty name="cargo" property="shippingManifest" />
In the table below, we provide the attributes for <jsp:getProperty> as well as the attribute types, whether they accept dynamic values and a description for each:

Tutorial:Review of JSP Actions for the Web Component Developer Exam-a21-getproperty.jpg
Table: Attributes for <jsp:getProperty> Action

The Include Action

The <jsp:include> action is used to include a static resource or the result from a different web component (i.e. JSP page or servlet) in the same web application. The current page will continue to execute after it has included the response generated by the targeted resource in the action. On the <jsp:include> action is executed, any response content that has been buffered, you use the flush attribute to control whether or not to flush the buffer.

The page or context relative URI path information that comes from the request object is based on the URI path information for the source JSP page. This is the case even in the target resource. No other request information is altered so the target resource can access all of the original parameters as well as headers that have been passed in the request. You can add additional parameters to pass to the target resource via <jsp:param> elements in the <jsp:include> element's body. The syntax for the <jsp:include> is shown below:

XML Code: Syntax of jsp:include
<jsp:include page="pageOrContextRelativeURI " [flush="true|false "] />
An simple example of the use of <jsp:include> is shown below:

XML Code: Example of jsp:include
<jsp:include page="header.jsp" />
In the table below, we provide the attributes for <jsp:include> as well as the attribute types, whether they accept dynamic values and a description for each:

Tutorial:Review of JSP Actions for the Web Component Developer Exam-a21-include.jpg
Table: Attributes for <jsp:include> Action

The Invoke Action

The <jsp:invoke> action is used to evaluate a fragment attribute and either adds the output to the calling page’s output stream or captures it in a variable. It must only be used in a tag file. The syntax for the <jsp:invoke> action is shown below:

XML Code: Syntax of jsp:invoke
<jsp:invoke fragment="fragmentName " [var="var " | varReader="varReader "] 
  [scope="page | request | session | application"] />
An example of the use of the <jsp:invoke> action where we show the action being used to add to a response as well as being captured in a variable is shown below:

XML Code: Example of jsp:invoke
<%@ attribute name="fractal" fragment="true" %>

<%-- Add the evaluation result to the response --%>
<jsp:invoke fragment="fractal"/>

<%-- Capture the evaluation result for further processing --%>
<jsp:invoke fragment="fractal" var="result" />
In the table below, we provide the attributes for <jsp:invoke> as well as the attribute types, whether they accept dynamic values and a description for each:

Tutorial:Review of JSP Actions for the Web Component Developer Exam-a21-invoke.jpg
Table:Attributes for <jsp:invoke> Action

The Output Action

The <jsp:output> element is used to specify the XML declaration or the document type declaration in the request output of a JSP document or a tag file in XML syntax. It is used to modify the properties of the generated response. The syntax for the <jsp:output> element is shown below:

XML Code: Syntax of jsp:output
<jsp:output [omit-xml-declaration="true|yes|false|no"] 
  [doctype-root-element="elementName"
    [doctype-public="publicID"] doctype-system="systemID"]/>
An example is shown below where both XML and DOCTYPE declarations are added to the response:

XML Code: Syntax of jsp:output
<!-- Add an XML declaration to the response -->
<jsp:output omit-xml-declaration="true" />

<!-- Add a DOCTYPE declaration to the response -->
  <jsp:output doctype-root-element="html"
    doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
    doctype-system='http://www.w3c.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'/>
<jsp:param>
In the table below, we provide the attributes for <jsp:output> as well as the attribute types, whether they accept dynamic values and a description for each:

Tutorial:Review of JSP Actions for the Web Component Developer Exam-a21-output.jpg
Table: Attributes for <jsp:output> Element

The Param Action

The <jsp:param> action is used in the body of <jsp:forward> or <jsp:include> actions in order to specify any additional request parameters for the targeted resource. It is also used in the body of the <jsp:params> action in order to specify applet parameters. The syntax for <jsp:param> action is shown below:

XML Code: Syntax of jsp:param
<jsp:param name="parameterName" value="parameterValue" />
Below, we show an example of the use of <jsp:param> action within a <jsp:include> actions:

XML Code: Syntax of jsp:param
<jsp:include page="header.jsp">
  <jsp:param name="bgColor" value="<%= clientBGColor %>" />
</jsp:include>
In the table below, we provide the attributes for <jsp:param> as well as the attribute types, whether they accept dynamic values and a description for each:

Tutorial:Review of JSP Actions for the Web Component Developer Exam-a21-param.jpg
Table: Attributes for <jsp:param> Action

The Params Action

The <jsp:params> action is only to be used in the body of a <jsp:plugin> action to provide a set of <jsp:param> actions that specify the applet parameters. The syntax for the <jsp:params> action is shown below:

XML Code: Syntax of jsp:params
<jsp:params>
  <jsp:param name=”param1” value=”value1” />
  <jsp:param name=”param2” value=”value2” />
</jsp:params>
An example is shown below of the use of <jsp:params> action with the <jsp:plugin> action to provide the specifications for the applet:

XML Code: Example of jsp:params
<jsp:plugin type="applet" code="SportsVideo.class" 
  codebase="applet" 
  jreversion="2.0" width="340" height="220" >
  <jsp:params>
    <jsp:param name="bgcolor" value="6b8fd4" />
  </jsp:params>
</jsp:plugin>
There are no attributes for the <jsp:params> action.

The Plugin Action

The <jsp:plugin> action causes the execution of an applet or bean. The applet or bean will execute in the specified plugin. If the plugin is not available, the client will display a dialog to initiate the download of the specified plugin software. The body of a <jsp:plugin> action can contain a <jsp:params> element in order to specify the applet parameters and a <jsp:fallback> element to specify any informative text shown in browsers that don't support the <embed> or <object> HTML elements. The syntax for the <jsp:plugin> is shown below:

XML Code: Syntax of jsp:plugin
<jsp:plugin [align="bottom|middle |top"] [archive="archiveList "]
  code="className " codeBase="relativePath " [height="height "]
  [hspace="horizontalSpace "] [iepluginurl="pluginURL "]
  [jreversion="jreVersion "] [name="appletName "] 
  [nspluginurl="pluginURL "] [title="title "] type="applet|bean"
  [vspace="verticalSpace "] [width="width "] >
  Optionally one <jsp:param> and one <jsp:fallback> action
</jsp:plugin>
Below we show again the example of the plugin this time emphasizing the <jsp:plugin> action. This is shown below:

XML Code: Example of jsp:plugin
<jsp:plugin type="applet" code="SportsVideo.class" 
  codebase="applet" jreversion="2.0">
  <jsp:attribute name="width">340</jsp:attribute>
  <jsp:attribute name="height">220</jsp:attribute>
  <jsp:body>
    <jsp:params>
      <jsp:param name="bgcolor" value="6b8fd4" />
    </jsp:params>
    <jsp:fallback>
      This plugin tag OBJECT or EMBED not supported by this version of browser.
    </jsp:fallback>
  </jsp:body>
</jsp:plugin>
In the table below, we provide the attributes for <jsp:plugin> as well as the attribute types, whether they accept dynamic values and a description for each:

Tutorial:Review of JSP Actions for the Web Component Developer Exam-a21-plugin.jpg
Table: Attributes for <jsp:plugin> Action

The Root Action

The <jsp:root> action element is used to define standard elements and namespace attributes of tag libraries. It can only be used as the root element in a JSP page in XML syntax (i.e. JSP Document). Before JSP 2.0 arrived, the only means of identifying a JSP Document was to use a <jsp:root> element. Since the advent of JSP 2.0 and later, there are other means to achieve this rendering the use of <jsp:root> as optional. The syntax of <jsp:root> is shown below:

XML Code: Syntax of jsp:root
<jsp:root version="jspVersion">
  Well-formed XML content
</jsp:root>
An example of a JSP Document using the <jsp:root> action element is shown below:

XML Code: Example of jsp:root
<jsp:root version="2.0">
  <cargo>
    <id>${param.cargoId}</id>
    <shippingDest>${param.cargoShippingDest}</shippingDest>
    ...
  <cargo>
</jsp:root>
In the table below, we provide the attributes for <jsp:root> as well as the attribute types, whether they accept dynamic values and a description for each:

Tutorial:Review of JSP Actions for the Web Component Developer Exam-a21-root.jpg
Table: Attributes for <jsp:root> Action

The SetProperty

The <jsp:setProperty> action sets a bean property value or values. It is one of two actions available for setting the value of a bean. The other is <c:set>. The syntax for the <jsp:setProperty> is shown below:

XML Code: Syntax of jsp:setProperty
<jsp:setProperty name="beanVariableName" property="propertyName"
  [param="parameterName" | value="value"] />
An example is shown below with <jsp:setProperty> action element. In this case we are going to set the bean property called category for a mixed message bean that has the following two properties:
  • category - this is a writeable String property that can be either thoughts or quotes.
  • message - this is a readable String property that holds the current message of the selected category.


The page for this bean is shown below:

XML Code: Example of jsp:setProperty
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Messages of the Day</title>
</head>
<body bgcolor="yellow">
    <h1>Messages of the Day</h1>
  
    <jsp:useBean id="msg" 
      class="com.acme.mytestproj.domain.MessageBean" />
  
    <h2>Words to Live By!</h2>
  
    <jsp:setProperty name="msg" property="category"
      value="thoughts" />
  
    <i>
      <jsp:getProperty name="msg" property="message" />
    </i>
  
    <h2>Quotes to Remember</h2>
  
    <jsp:setProperty name="msg" property="category"
      value="quotes" />
  
    <i>
      <jsp:getProperty name="msg" property="message" />
    </i>
  
</body>
</html>
In this example we have used the <jsp:setProperty> action element twice in order to reset the category property of the MessageBean. Once this property is set, we can read a new message from the MessageBean. In the table below, we provide the attributes for <jsp:setProperty> as well as the attribute types, whether they accept dynamic values and a description for each:

Tutorial:Review of JSP Actions for the Web Component Developer Exam-a21-setproperty.jpg
Table: Attributes for <jsp:setProperty> Action Element

The <jsp:setProperty> property type attribute can be any valid Java type. This includes the use of primitives, arrays as well as generics. If you have a runtime attribute value specified by the value attribute in which you are using a Java expression, you must ensure that the type of the expression matches the property's type. In many cases where the value is of type String and either explicitly specified by the value attribute or is coming from the request parameter value, this value will be converted to the property’s type. In the table below we outline how this conversion will take place:

Tutorial:Review of JSP Actions for the Web Component Developer Exam-a21-conversionfromstring.jpg
Table: Conversion from String to Property’s Type

You may note that there are some types that are not covered in this table. For example, in the case of a java.util.Date type property, the web container will use the java.beans.PropertyEditor that is registered for the type. It will call the property editor’s setAsText(String) method. It can then convert a string like 2012-01-03 to a Date object that represents this date.

The Text Action

The <jsp:text> action is used to enclose template data primarily in for JSP Documents. Its body must contain only template text and EL expressions. When this action is used in an XML document, you can only have well-formed content in the body. The action's body content is evaluated for the template text and any expressions. The result of the evaluation is added to the response with any whitespaces preserved. The syntax for the <jsp:text> action is shown below:

XML Code: Syntax of jsp:text
<jsp:text>
  Template text and/or EL expressions
</jsp:text>
We provide a simple example using the <jsp:text> action to provide simple text and an EL expression. This is shown below:

XML Code: Example of jsp:text
<jsp:text>
  Some text and ${anELexpression}
</jsp:text>
The UseBean Action

The <jsp:useBean> action is used to instantiate or references a bean with a specific name and scope. It associates a Java bean with a name in one of the JSP scopes and makes it available as a scripting variable. When an attempt is initially made to find a particular bean via it’s name in a specific scope, if it is found, that instance of the bean is used. If it's not found, a new instance of the specified class will be created. There are two forms of the <jsp:useBean> action. They are with and with a body. The syntax for both <jsp:useBean> action elements is shown below:

Using a Concrete Class without a body
XML Code: Syntax of jsp:useBean Using Concrete Class w/o Body
<jsp:useBean id="beanVariableName " class="className "
  [scope="page |request | session | application"] />
Using a Concrete Class with a body
XML Code: Syntax of jsp:useBean Using Concrete Class w Body
<jsp:useBean id="beanVariableName " class="className "
  [scope="page | request | session | application"]>
  Evaluated if a new instance is created
</jsp:useBean>
Using a Type and optionally either a class or a serialized bean without a body
XML Code: Syntax of jsp:useBean Using Type of Class or Bean w/o Body
<jsp:useBean id="beanVariableName " type="className " 
  [class="className " | beanName="className "]
  [scope="page | request | session | application"] />
Using a type and optionally either a class or a serialized bean with a body
XML Code: Syntax of jsp:useBean Using Type of Class or Bean w/o Body
<jsp:useBean id="beanVariableName " type="className " 
  [class="className " | beanName="className "]
  [scope="page | request | session | application"]>
We return to the message.jsp for an example of the <jsp:useBean> action being used to declare a bean for a JSP page:

XML Code: Example of jsp:useBean
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Messages of the Day</title>
</head>
<body bgcolor="yellow">
    <h1>Messages of the Day</h1>
  
    <jsp:useBean id="msg" 
      class="com.acme.mytestproj.domain.MessageBean" />
  
    <h2>Words to Live By!</h2>
  
    <jsp:setProperty name="msg" property="category"
      value="thoughts" />
  
    <i>
      <jsp:getProperty name="msg" property="message" />
    </i>
  
    <h2>Quotes to Remember</h2>
  
    <jsp:setProperty name="msg" property="category"
      value="quotes" />
  
    <i>
      <jsp:getProperty name="msg" property="message" />
    </i>
  
</body>
</html>
In this case, the MessageBean is declared with the <jsp:useBean> action and associated with the name msg. In the table below, we provide the attributes for <jsp:useBean> as well as the attribute types, whether they accept dynamic values and a description for each:

Tutorial:Review of JSP Actions for the Web Component Developer Exam-a21-usebean.jpg
Table: Attributes for the <jsp:useBean> Action

You must specify at least one class or type for the optional attributes. If you specify both a class and a type, the class must be assignable to type. The beanName attribute must always be combined with the type attribute. Note that this is not valid with the class attribute. The <jsp:useBean> action is processed in the following steps:
  1. Attempt is made to locate an object based on the id and scope attribute values.
  2. Definition of a scripting language variable with the given id of the specified type or class.
  3. If the object is found, the variable's value is initialized with a reference to the located object, cast to the type specified by type or class. This completes the processing of the action. If the action element has a nonempty body, it's ignored.
  4. If the object isn't found in the specified scope and neither class nor beanName is specified, an InstantiationException is thrown. This completes the processing of the action.
  5. If the object isn't found in the specified scope, and the class attribute specifies a non-abstract class with a public no-args constructor, a new instance of the class is created and associated with the scripting variable and with the specified name in the specified scope. After this, step 7 is performed.
If the object isn't found, and the specified class doesn't fulfill the requirements, an InstantiationException is thrown. This completes the processing of the action.
  6. If the object isn't found in the specified scope, and the beanName attribute is specified, the instantiate( ) method of the java.beans.Beans class is invoked with the ClassLoader of the JSP implementation class instance and the beanName as arguments. If the method succeeds, the new object reference is associated with the scripting variable and with the specified name in the specified scope. After this, step 7 is performed.
  7. If the action element has a nonempty body, the body is processed. The scripting variable is initialized and available within the scope of the body. The text of the body is treated as elsewhere; if there is template text, it's passed through to the response; scriptlets and action tags are evaluated.
A common use of a nonempty body is to complete initializing the created instance; in that case, the body typically contains <jsp:setProperty> actions and scriptlets.


Summary

That is it for JSP Standard Actions. We have covered in detail all of the JSP actions in order to ensure that you understand all of them for the web component developer exam. In the next article we will cover custom tag libraries.