Overview of JSP Syntax Element
 
Standard Actions: JSP Tags
 
JSP action elements result in some sort of action occurring while the JSP page is being executed, such as instantiating a Java object and making it available to the page.
 
Such actions may include the following:
1. creating a JavaBean instance and accessing its properties
2. forwarding execution to another HTML page, JSP page,
3. including an external resource in the JSP page
For standard actions, there is a set of tags defined in the JSP specification. Although directives and scripting elements described earlier in this chapter are sufficient to code a JSP page, the standard tags described here provide additional functionality and convenience
 
Here is the general tag syntax for JSP standard actions:
<jsp:tag attr1="value1" attr2="value2" ... attrN="valueN">
...body...
</jsp:tag>
or, where there is no body:
<jsp:tag attr1="value1", ..., attrN="valueN" />
 
The JSP specification includes the following standard action tags, which are introduced and briefly discussed here.
 
. jsp:useBean
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 key attributes are class, type, id, and scope
# 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.
 
# 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.
 
# 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.
 
There are three typical scenarios:
1. Use type and id to specify an instance that already exists in the target scope.
2. 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.
3. 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.
 
Consider the following examples:
 
<jsp:useBean id="reqobj" type="mypkg.MyIntfc" scope="request" />
The preceding example uses a request-scope instance reqobj of type MyIntfc.
 
<jsp:useBean id="pageobj" class="mybeans.PageBean" scope="page" />
The preceding example uses a page-scope instance pageobj of class PageBean, first creating it if necessary.
 
<jsp:useBean id="sessobj" class="mybeans.SessionBean"
type="mypkg.MyIntfc scope="session" />
The preceding example creates an instance of class SessionBean, and assigns the instance to the variable sessobj of type MyIntfc.
 
. jsp:setProperty
The following example sets the user property of the pageBean instance 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), we can simply set the property as follows:
<jsp:setProperty name="pageBean" property="user" />
 
The below 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="*" />
 
 
. jsp:getProperty
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" />
 
. jsp:param
We 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
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 support either a "true" or "false" setting, with "false" being the default.
 
We 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>
 
. jsp:forward
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.
 
Examples:
<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>
 
. jsp:plugin
The jsp:plugin tag results in the execution of a specified applet or JavaBean in the client browser, preceded by a download of Java plugin software if necessary. Specify configuration information, such as the applet to run and the code base, using jsp:plugin attributes.
 
The following example shows the use of an applet plugin:
<jsp:plugin type=applet code="Molecule.class" codebase="/html" >
<jsp:params>
<jsp:param name="molecule" value="molecules/benzene.mol" />
</jsp:params>
<jsp:fallback>
<p> Unable to start the plugin. </p>
</jsp:fallback>
</jsp:plugin>