Unlike JSP directive elements, JSP action elements take effect during the request processing phase. JSP action elements are written using XML syntax.
Using JSP actions, you can dynamically insert files, reuse JavaBean components, redirect users to another page, and generate HTML code for Java plugins.
Action elements have only one syntax, which conforms to the XML standard:
Action elements are essentially predefined functions. The JSP specification defines a series of standard actions, prefixed with "jsp". The available standard action elements are as follows:
| Syntax | Description |
|---|---|
| jsp:include | Includes a file when the page is requested. |
| jsp:useBean | Finds or instantiates a JavaBean. |
| jsp:setProperty | Sets the properties of a JavaBean. |
| jsp:getProperty | Outputs the property of a JavaBean. |
| jsp:forward | Forwards the request to a new page. |
| jsp:plugin | Generates OBJECT or EMBED tags for the Java plugin based on the browser type. |
| jsp:element | Defines a dynamic XML element. |
| jsp:attribute | Sets the attribute of a dynamically defined XML element. |
| jsp:body | Sets the content of a dynamically defined XML element. |
| jsp:text | Uses a template for writing text in JSP pages and documents. |
Common Attributes
All action elements have two attributes: the id attribute and the scope attribute.
- id attribute:
Theidattribute is the unique identifier of the action element and can be referenced in the JSP page. Theidvalue created by the action element can be invoked via thePageContext. - scope attribute:
This attribute is used to identify the lifecycle of the action element. Theidandscopeattributes are directly related; thescopeattribute defines the lifespan of the associatedidobject. Thescopeattribute has four possible values: (a)page, (b)request, (c)session, and (d)application.
The <jsp:include> Action Element
The <jsp:include> action element is used to include both static and dynamic files. This action inserts the specified file into the page being generated. The syntax is as follows:
The include directive was introduced earlier; it includes a file when the JSP file is converted into a Servlet. The jsp:include action is different; the file is inserted when the page is requested.
Below is a list of attributes related to the include action.
| Attribute | Description |
|---|---|
| page | The relative URL address of the file to include in the page. |
| flush | A boolean attribute that defines whether to flush the buffer before including the resource. |
Example
We define two files, date.jsp and main.jsp, with the following code:
date.jsp file code:
Today's date is:
main.jsp file code:
include Action Example
Now, place the above two files in the server's root directory and access the main.jsp file. The display result is as follows:
include Action Example
Today's date is: 2016-6-25 14:08:17
The <jsp:useBean> Action Element
The jsp:useBean action is used to load a JavaBean that will be used in the JSP page.
This feature is very useful because it allows us to leverage the advantages of Java component reuse.
The simplest syntax for the jsp:useBean action is:
After the class is loaded, we can modify and retrieve the bean's properties using the jsp:setProperty and jsp:getProperty actions.
Below is a list of attributes related to the useBean action.
| Attribute | Description |
|---|---|
| class | Specifies the full package name of the Bean. |
| type | Specifies the type of the variable that will reference this object. |
| beanName | Specifies the name of the Bean via the instantiate() method of java.beans.Beans. |
Before giving specific examples, let's first look at the jsp:setProperty and jsp:getProperty action elements:
The <jsp:setProperty> Action Element
jsp:setProperty is used to set the properties of an already instantiated Bean object. There are two ways to use it. First, you can use jsp:setProperty outside (after) the jsp:useBean element, as shown below:
In this case, jsp:setProperty will execute regardless of whether jsp:useBean found an existing Bean or created a new Bean instance. The second way is to place jsp:setProperty inside the jsp:useBean element, as shown below:
In this case, jsp:setProperty will only execute when a new Bean instance is created. If an existing instance is used, jsp:setProperty will not execute.
The jsp:setProperty action has the following four attributes, as shown in the table below:
| Attribute | Description |
|---|---|
| name | The name attribute is required. It indicates which Bean to set the property for. |
| property | The property attribute is required. It indicates which property to set. There is a special usage: if the value of property is "*", it means that all request parameters whose names match the Bean property names will be passed to the corresponding property's set method. |
| value | The value attribute is optional. This attribute is used to specify the value of the Bean property. String data will be automatically converted to numbers, booleans, Booleans, bytes, Bytes, characters, or Characters in the target class using the standard valueOf method. For example, boolean and Boolean type property values (like "true") are converted via Boolean.valueOf, and int and Integer type property values (like "42") are converted via Integer.valueOf. value and param cannot be used at the same time, but either one can be used. |
| param | param is optional. It specifies which request parameter to use as the value of the Bean property. If the current request has no parameters, nothing happens; the system will not pass null to the Bean property's set method. Therefore, you can let the Bean provide its own default property values and only modify the default values when the request parameter explicitly specifies a new value. |
The <jsp:getProperty> Action Element
The jsp:getProperty action extracts the value of the specified Bean property, converts it to a string, and then outputs it. The syntax is as follows:
The table below lists the attributes associated with getProperty:
| Attribute | Description |
|---|---|
| name | The name of the Bean property to retrieve. The Bean must be defined. |
| property | Indicates the value of the Bean property to extract. |
Example
In the following example, we use a Bean:
package com.tutorial.main;public class TestBean { private String message = ""; public String getMessage() { return(message); } public void setMessage(String message) { this.message = message; }}Compile the above example file TestBean.java:
After compilation, a TestBean.class file will be generated in the current directory. Copy this file to the WebContent/WEB-INF/classes/com/tutorial/main directory of the current JSP project (the com/tutorial/main package path; create it manually if it doesn't exist).
Below is a directory structure diagram in Eclipse:
Here is a very simple example. Its function is to load a Bean and then set/read its message property.
Now let's call this Bean in the main.jsp file:
JSP Using JavaBean Example
Output message....
Access the above file in a browser. The output is as follows:
The <jsp:forward> Action Element
The jsp:forward action forwards the request to another page. The jsp:forward tag has only one attribute, page. The syntax is as follows:
Below are the attributes associated with forward:
| Attribute | Description |
|---|---|
| page | The page attribute contains a relative URL. The value of page can be given directly or calculated dynamically at request time. It can be a JSP page or a Java Servlet. |
Example
In the following example, we use two files: date.jsp and main.jsp.
The date.jsp file code is as follows:
Today's date is:
The main.jsp file code is as follows:
forward Action Example
Now, place the above two files in the server's root directory and access the main.jsp file. The display result is as follows:
Today's date is: 2016-6-25 14:37:25
The <jsp:plugin> Action Element
The jsp:plugin action is used to insert the OBJECT or EMBED elements necessary to run Java Applets via the Java plugin, based on the browser type.
If the required plugin does not exist, it will download the plugin and then execute the Java component. The Java component can be an applet or a JavaBean.
The plugin action has multiple attributes corresponding to HTML elements for formatting the Java component. The param element can be used to pass parameters to the Applet or Bean.
Below is a typical example using the plugin action element:
If you are interested, you can try using an applet to test the jsp:plugin action element. The <fallback> element is a new element that sends an error message to the user if the component fails.
The <jsp:element>, <jsp:attribute>, and <jsp:body> Action Elements
The <jsp:element>, <jsp:attribute>, and <jsp:body> action elements dynamically define XML elements. "Dynamic" is very important; it means that the XML elements are generated dynamically at compile time, not statically.
The following example dynamically defines an XML element:
Attribute Value XML Element BodyAccess the following page in a browser. The output result is as follows:
The <jsp:text> Action Element
The <jsp:text> action element allows the use of a template for writing text in JSP pages and documents. The syntax is as follows:
The above text template cannot contain duplicate elements; it can only contain text and EL expressions (Note: EL expressions will be introduced in subsequent chapters). Please note that in XML files, you cannot use expressions like ${whatever > 0} because the > symbol is illegal. You can use the expression ${whatever gt 0} or embed the value in a CDATA section.
]]>
If you need to declare a DOCTYPE in XHTML, you must use the <jsp:text> action element. The example is as follows:
You can try the above example with and without the <jsp:text> action element to see the difference in the results.
YouTip