JSP Action Elements
Unlike JSP directive elements, JSP action elements take effect during the request processing phase. JSP action elements are written in XML syntax.
Using JSP actions, you can dynamically insert files, reuse JavaBean components, redirect users to other pages, and generate HTML code for Java plugins.
Action elements have only one syntax, which conforms to the XML standard:
<jsp:action_name attribute="value" />
Action elements are essentially predefined functions. The JSP specification defines a series of standard actions, which use "jsp" as a prefix. 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 properties of a JavaBean. |
jsp:forward | Forwards the request to a new page. |
jsp:plugin | Generates OBJECT or EMBED tags based on the browser type for Java plugins. |
jsp:element | Defines dynamic XML elements. |
jsp:attribute | Sets attributes for dynamically defined XML elements. |
jsp:body | Sets the content for dynamically defined XML elements. |
jsp:text | Uses text templates in JSP pages and documents. |
Common Attributes
All action elements have two attributes: id and scope.
- id attribute:
The id attribute is a unique identifier for the action element and can be referenced in the JSP page. The id value created by the action element can be invoked via PageContext.
- scope attribute:
This attribute is used to identify the lifecycle of the action element. The id and scope attributes are directly related, with the scope attribute defining the lifespan of the associated id object. The scope attribute can have four possible values: (a) page, (b) request, (c) session, and (d) application.
<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:
<jsp:include page="relative URL address" flush="true" />
Previously, the include directive was introduced, which includes files when the JSP file is converted to a Servlet. The jsp:include action, however, inserts the file at the time 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 be included in the page. |
flush | Boolean attribute, defines whether to flush the cache before including the resource. |
Example
Below, we define two files, date.jsp and main.jsp, with the following code:
date.jsp file code:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<p>
Today's date is: <%= (new java.util.Date()).toLocaleString()%>
</p>
main.jsp file code:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>tutorialpro.org(tutorialpro.org)</title>
</head>
<body>
<h2>include action example</h2>
<jsp:include page="date.jsp" flush="true" />
</body>
</html>
Now, place these two files in the root directory of the server 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
<jsp:useBean> Action Element
The jsp:useBean action is used to load a JavaBean that will be used in the JSP page.
This function is very useful because it allows us to take advantage of Java component reusability.
The simplest syntax for the jsp:useBean action is:
<jsp:useBean id="name" class="package.class" />
After the class is loaded, we can modify and retrieve the properties of the bean using the jsp:setProperty
and jsp:getProperty
actions.
Here 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 the object. |
beanName | Specifies the name of the Bean using the instantiate() method of java.beans.Beans . |
Before providing a specific example, let's take a look at the jsp:setProperty
and jsp:getProperty
action elements:
<jsp:setProperty> Action Element
jsp:setProperty
is used to set the properties of an 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:
<jsp:useBean id="myName" ... />
...
<jsp:setProperty name="myName" property="someProperty" .../>
In this case, jsp:setProperty
will execute whether jsp:useBean
finds an existing Bean or creates a new Bean instance. The second usage is to place jsp:setProperty
inside the jsp:useBean
element, as shown below:
<jsp:useBean id="myName" ... >
...
<jsp:setProperty name="myName" property="someProperty" .../>
</jsp:useBean>
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:
Attribute | Description |
---|---|
name | The name attribute is required. It indicates which Bean's properties are to be set. |
property | The property attribute is required. It indicates which property to set. A special usage: if the property value is "*", all request parameters that match the Bean property names will be passed to the corresponding property set methods. |
value | The value attribute is optional. It specifies the value of the Bean property. String data will be automatically converted to numeric, boolean, Boolean, byte, Byte, char, Character types in the target class using standard valueOf methods. 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 simultaneously, but either one can be used. | |
param | The param attribute is optional. It specifies which request parameter to use as the Bean property value. If the current request has no parameters, nothing will be done, and the system will not pass null to the Bean property's set method. Therefore, you can let the Bean provide default property values, and only modify the default values when the request parameter explicitly specifies a new value. |
<jsp:getProperty> Action Element
The jsp:getProperty
action extracts the value of a specified Bean property, converts it to a string, and then outputs it. The syntax format is as follows:
<jsp:useBean id="myName" ... />
...
<jsp:getProperty name="myName" property="someProperty" .../>
Here are 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
The following example uses a Bean:
package com.tutorialpro.main;
public class TestBean {
private String message = "tutorialpro.org";
public String getMessage() {
return(message);
}
public void setMessage(String message) {
this.message = message;
}
}
Compile the above example file TestBean.java:
$ javac TestBean.java
After compilation, a TestBean.class file will be generated in the current directory. Copy the file to the WebContent/WEB-INF/classes/com/tutorialpro/main directory of the current JSP project (the package path com/tutorialpro/main, if it doesn't exist, needs to be created manually).
Below is a directory structure diagram in Eclipse: Unable to initialize Java Plugin </jsp:fallback>
</jsp:plugin>
If you are interested, you can try using the applet to test the jsp:plugin action element. The <fallback> element is a new element that sends error messages to users when the component fails.
<jsp:element>, <jsp:attribute>, <jsp:body> Action Elements
The <jsp:element>, <jsp:attribute>, <jsp:body> action elements dynamically define XML elements. Dynamic is crucial, meaning that XML elements are generated dynamically at compile time rather than statically.
The following example dynamically defines an XML element:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>tutorialpro.org(tutorialpro.org)</title>
</head>
<body>
<jsp:element name="xmlElement">
<jsp:attribute name="xmlElementAttr">
Attribute Value
</jsp:attribute>
<jsp:body>
XML Element Body
</jsp:body>
</jsp:element>
</body>
</html>
When accessing the page in a browser, the output will be as follows:
<jsp:text> Action Element
The <jsp:text> action element allows writing text templates in JSP pages and documents. The syntax is as follows:
<jsp:text>Template Data</jsp:text>
The above text template cannot contain repeated elements, only text and EL expressions (note: EL expressions will be introduced in subsequent chapters). Note that in XML files, you cannot use expressions like ${whatever > 0} because the > symbol is illegal. You can use the ${whatever gt 0} expression or embed the value in a CDATA section.
<jsp:text><![CDATA[<br>]]></jsp:text>
If you need to declare a DOCTYPE in XHTML, you must use the <jsp:text> action element. Here is an example:
<jsp:text><![CDATA[<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"DTD/xhtml1-strict.dtd">]]>
</jsp:text>
<head><title>jsp:text action</title></head>
<body>
<books><book><jsp:text>
Welcome to JSP Programming
</jsp:text></book></books>
</body>
</html>
You can try the difference in execution results by using and not using the <jsp:text> action element.