Body
Body
htm#i1006026
The JSP specification includes the following standard action tags, which are introduced and briefly
discussed immediately below:
• jsp:usebean
• jsp:setProperty
• jsp:getProperty
• jsp:param
• jsp:include
• jsp:forward
• jsp:plugin
jsp:useBean tag
The jsp:useBean tag accesses or creates an instance of a Java type, typically a JavaBean class,
and associates the instance with a specified name, or ID. The instance is then available by that ID as
1 of 5
a scripting variable of specified scope. Scripting variables are introduced in "Custom Tag
Libraries". Scopes are discussed in "JSP Objects and Scopes".
The key attributes are class, type, id, and scope. (There is also a less frequently used
beanName attribute, discussed below.)
Use the id attribute to specify the instance name. The JSP container will first search for an object
by the specified ID, of the specified type, in the specified scope. If it does not exist, the container
will attempt to create it.
Intended use of the class attribute is to specify a class that can be instantiated, if necessary, by the
JSP container. The class cannot be abstract and must have a no-argument constructor. Intended use
of the type attribute is to specify a type that cannot be instantiated by the JSP container—either an
interface, an abstract class, or a class without a no-argument constructor. You would use type in a
situation where the instance will already exist, or where an instance of an instantiable class will be
assigned to the type. There are three typical scenarios:
• Use type and id to specify an instance that already exists in the target scope.
• Use class and id to specify the name of an instance of the class—either an instance that
already exists in the target scope or an instance to be newly created by the JSP container.
• Use class, type, and id to specify a class to instantiate and a type to assign the instance
to. In this case, the class must be legally assignable to the type.
Use the scope attribute to specify the scope of the instance—either page for the instance to be
associated with the page context object, request for it to be associated with the HTTP request
object, session for it to be associated with the HTTP session object, or application for it to
be associated with the servlet context.
As an alternative to using the class attribute, you can use the beanName attribute. In this case,
you have the option of specifying a serializable resource instead of a class name. When you use the
beanName attribute, the JSP container creates the instance by using the instantiate()
method of the java.beans.Beans class.
The following example uses a request-scope instance reqobj of type MyIntfc. Because
MyIntfc is an interface and cannot be instantiated directly, reqobj would have to already exist.
<jsp:useBean id="reqobj" type="mypkg.MyIntfc" scope="request" />
This next example uses a page-scope instance pageobj of class PageBean, first creating it if
necessary:
<jsp:useBean id="pageobj" class="mybeans.PageBean" scope="page" />
The following example creates an instance of class SessionBean and assigns the instance to the
variable sessobj of type MyIntfc:
<jsp:useBean id="sessobj" class="mybeans.SessionBean"
2 of 5
type="mypkg.MyIntfc scope="session" />
jsp:setProperty tag
The jsp:setProperty tag sets one or more bean properties. The bean must have been
previously specified in a jsp:useBean tag. You can directly specify a value for a specified
property, or take the value for a specified property from an associated HTTP request parameter, or
iterate through a series of properties and values from the HTTP request parameters.
The following example sets the user property of the pageBean instance (defined in the
preceding jsp:useBean example) to a value of "Smith":
<jsp:setProperty name="pageBean" property="user" value="Smith" />
The following example sets the user property of the pageBean instance according to the value
set for a parameter called username in the HTTP request:
<jsp:setProperty name="pageBean" property="user" param="username" />
If the bean property and request parameter have the same name (user), you can simply set the
property as follows:
<jsp:setProperty name="pageBean" property="user" />
The following example results in iteration over the HTTP request parameters, matching bean
property names with request parameter names and setting bean property values according to the
corresponding request parameter values:
<jsp:setProperty name="pageBean" property="*" />
When you use the jsp:setProperty tag, string input can be used to specify the value of a non-
string property through conversions that happen behind the scenes. See "Bean Property Conversions
from String Values".
jsp:getProperty tag
The jsp:getProperty tag reads a bean property value, converts it to a Java string, and places
the string value into the implicit out object so that it can be displayed as output. The bean must
have been previously specified in a jsp:useBean tag. For the string conversion, primitive types
are converted directly and object types are converted using the toString() method specified in
the java.lang.Object class.
The following example puts the value of the user property of the pageBean bean into the out
object:
<jsp:getProperty name="pageBean" property="user" />
3 of 5
jsp:param tag
You can use jsp:param tags in conjunction with jsp:include, jsp:forward, and
jsp:plugin tags (described below).
Used with jsp:forward and jsp:include tags, a jsp:param tag optionally provides name/
value pairs for parameter values in the HTTP request object. New parameters and values
specified with this action are added to the request object, with new values taking precedence
over old.
The following example sets the request object parameter username to a value of Smith:
<jsp:param name="username" value="Smith" />
jsp:include tag
The jsp:include tag inserts additional static or dynamic resources into the page at request-time
as the page is displayed. Specify the resource with a relative URL (either page-relative or
application-relative). For example:
<jsp:include page="/templates/userinfopage.jsp" flush="true" />
A "true" setting of the flush attribute results in the buffer being flushed to the browser when a
jsp:include action is executed. The JSP specification and the OC4J JSP container support
either a "true" or "false" setting, with "false" being the default. (The JSP 1.1 specification
supported only a "true" setting, with flush being a required attribute.)
You can also have an action body with jsp:param tags, as shown in the following example:
<jsp:include page="/templates/userinfopage.jsp" flush="true" >
<jsp:param name="username" value="Smith" />
<jsp:param name="userempno" value="9876" />
</jsp:include>
Note that the following syntax would work as an alternative to the preceding example:
<jsp:include page="/templates/userinfopage.jsp?username=Smith&userempno=9876"
flush="true" />
jsp:forward tag
The jsp:forward tag effectively terminates execution of the current page, discards its output,
and dispatches a new page—either an HTML page, a JSP page, or a servlet.
The JSP page must be buffered to use a jsp:forward tag; you cannot set buffer="none" in a
page directive. The action will clear the buffer and not output contents to the browser.
As with jsp:include, you can also have an action body with jsp:param tags, as shown in the
second of the following examples:
4 of 5
<jsp:forward page="/templates/userinfopage.jsp" />
or:
<jsp:forward page="/templates/userinfopage.jsp" >
<jsp:param name="username" value="Smith" />
<jsp:param name="userempno" value="9876" />
</jsp:forward>
5 of 5