Unlike JSP directive elements, JSP action elements work during the request processing phase. JSP action elements are written in XML syntax.
Use JSP actions to dynamically insert files, reuse JavaBean components, redirect users to other pages, and generate HTML code for Java plug-ins.
There is only one syntax for action elements, and it conforms to the XML standard:
<jsp:action_name attribute="value" />
Action elements are basically 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:
grammar | describe |
---|---|
jsp:include | Import a file when the page is requested. |
jsp:useBean | Find or instantiate a JavaBean. |
jsp:setProperty | Set JavaBean properties. |
jsp:getProperty | Output the properties of a JavaBean. |
jsp:forward | Send the request to a new page. |
jsp:plugin | Generates OBJECT or EMBED tags for Java plug-ins depending on the browser type. |
jsp:element | Define dynamic XML elements |
jsp:attribute | Set dynamically defined XML element attributes. |
jsp:body | Sets the content of a dynamically defined XML element. |
jsp:text | Using templates that write text in JSP pages and documents |
All action elements have two attributes: id attribute and scope attribute.
id attribute:
The id attribute is the unique identifier of the action element and can be referenced in the JSP page. The id value created by the action element can be called through PageContext.
scope attribute:
This attribute is used to identify the life cycle of the action element. The id attribute is directly related to the scope attribute, and the scope attribute defines the lifespan of the associated id object. The scope attribute has four possible values: (a) page, (b) request, (c) session, and (d) application.
The <jsp:include> action element is used to include static and dynamic files. This action inserts the specified file into the page being generated. The syntax format is as follows:
<jsp:include page="relative URL" flush="true" />
The include directive has been introduced before. It introduces files when the JSP file is converted into a Servlet. The jsp:include action here is different. The time when the file is inserted is when the page is requested.
The following is a list of properties related to the include action.
property | describe |
---|---|
page | The relative URL address contained in the page. |
flush | Boolean property that defines whether the cache should be flushed before including the resource. |
Below we define two files date.jsp and main.jsp, the code is as follows:
date.jsp file code:
<p> Today's date: <%= (new java.util.Date()).toLocaleString()%></p>
main.jsp file code:
<html><head><title>The include Action Example</title></head><body><center><h2>The include action Example</h2><jsp:include page="date.jsp" flush ="true" /></center></body></html>
Now place the above two files in the root directory of the server and access the main.jsp file. The displayed results are as follows:
The include action ExampleToday's date: 12-Sep-2013 14:54:22
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 take advantage of Java component reuse while avoiding the loss of the convenience that distinguishes JSP from Servlets.
The simplest syntax for the jsp:useBean action is:
<jsp:useBean id="name" />
After the class is loaded, we can modify and retrieve the bean's properties through the jsp:setProperty and jsp:getProperty actions.
The following is a list of properties related to the useBean action.
property | describe |
---|---|
class | Specify the complete package name of the bean. |
type | Specifies the type that will reference the object variable. |
beanName | Specify the name of the Bean through the instantiate() method of java.beans.Beans. |
Before giving specific examples, let us first look at the jsp:setProperty and jsp:getProperty action elements:
jsp:setProperty is used to set the properties of the instantiated Bean object. There are two ways to use it. First, you can use jsp:setProperty outside (behind) the jsp:useBean element, like this:
<jsp:useBean id="myName" ... />...<jsp:setProperty name="myName" property="someProperty" .../>
At this time, whether jsp:useBean finds an existing Bean or creates a new Bean instance, jsp:setProperty will be executed. The second usage is to put jsp:setProperty inside the jsp:useBean element, as shown below:
<jsp:useBean id="myName" ... >... <jsp:setProperty name="myName" property="someProperty" .../></jsp:useBean>
At this time, jsp:setProperty will only be executed when creating a new Bean instance. If an existing instance is used, jsp:setProperty will not be executed.
property | describe |
---|---|
name | The name attribute is required. It indicates which Bean the property is to be set on. |
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 set method. |
value | The value attribute is optional. This property is used to specify the value of the Bean property. String data will be automatically converted into numbers, boolean, Boolean, byte, Byte, char, and Character through the standard valueOf method in the target class. For example, boolean and Boolean type attribute values (such as "true") are converted by Boolean.valueOf, and int and Integer type attribute values (such as "42") are converted by 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 is used as the value of the Bean property. If the current request has no parameters, nothing will be done, and the system will not pass null to the set method of the Bean property. Therefore, you can let the bean provide the default property value itself, and only modify the default property value when the request parameter explicitly specifies a new value. |
The jsp:getProperty action extracts the value of the specified Bean property, converts it into a string, and then outputs it. The syntax format is as follows:
<jsp:useBean id="myName" ... />...<jsp:getProperty name="myName" property="someProperty" .../>
The following table is the properties associated with getProperty:
property | describe |
---|---|
name | The name of the Bean property to retrieve. Bean must be defined. |
property | Indicates that the value of the Bean property is to be extracted |
In the following examples we use Beans:
/* File: TestBean.java */package action; public class TestBean { private String message = "No message specified"; public String getMessage() { return(message); } public void setMessage(String message) { this.message = message; }}
Compile the above example and generate the TestBean.class file. Copy the file to the directory where the server officially stores Java classes, rather than the directory reserved for classes that can be automatically loaded after modification (such as: C:apache-tomcat-7.0.2 webappsWEB-INFclassesaction directory, the CLASSPATH variable must contain this path). For example, for Java Web Server, beans and all classes used by beans should be placed in the classes directory, or encapsulated into jar files and placed in the lib directory, but they should not be placed under servlets. The following is a very simple example. Its function is to load a Bean and then set/read its message property.
Now let us call the bean in the main.jsp file:
<html><head><title>Using JavaBeans in JSP</title></head><body><center><h2>Using JavaBeans in JSP</h2> <jsp:useBean id="test" /> < jsp:setProperty name="test" property="message" value="Hello JSP..." /> <p>Got message....</p> <jsp:getProperty name="test" property="message " /> </center></body></html>
Execute the above file and the output will be as follows:
Using JavaBeans in JSPGot message....Hello JSP...
The jsp:forward action forwards the request to another page. The jsp:forward tag has only one attribute, page. The syntax format is as follows:
<jsp:forward page="Relative URL" />
The following are the attributes associated with forward:
property | describe |
---|---|
page | The page attribute contains a relative URL. The value of page can be given directly or dynamically calculated during request. It can be a JSP page or a Java Servlet. |
We use two files in the following examples: date.jsp and main.jsp.
The date.jsp file code is as follows:
<p> Today's date: <%= (new java.util.Date()).toLocaleString()%></p>
main.jsp file code:
<html><head><title>The forward Action Example</title></head><body><center><h2>The forward action Example</h2><jsp:forward page="date.jsp" / ></center></body>
Now place the above two files in the root directory of the server and access the main.jsp file. The displayed results are as follows:
Today's date: 12-Sep-2010 14:54:22
The jsp:plugin action is used to insert the OBJECT or EMBED elements necessary to run the Java Applet through the Java plug-in, depending on the browser type.
If the required plug-in does not exist, it downloads the plug-in and then executes the Java component. A Java component can be an applet or a JavaBean.
The plugin action has multiple attributes corresponding to HTML elements used to format Java components. The param element can be used to pass parameters to Applet or Bean.
The following is a typical example of using the plugin action element:
<jsp:plugin type="applet" codebase="dirname" code="MyApplet.class" > <jsp:param name="fontcolor" value="red" /> <jsp:param name="background" value=" black" /> <jsp:fallback> Unable to initialize Java Plugin </jsp:fallback> </jsp:plugin>
If you are interested, you can try using an applet to test the jsp:plugin action element. The <fallback> element is a new element. When the component fails, an error message is sent to the user.
<jsp:element>, <jsp:attribute>, <jsp:body> action elements dynamically define XML elements. Dynamic is very important, which means that XML elements are dynamically generated at compile time rather than static.
The following example dynamically defines XML elements:
<%@page language="java" contentType="text/html"%><html xmlns="http://www.w3c.org/1999/xhtml" xmlns:jsp="http://java.sun. com/JSP/Page"><head><title>Generate XML Element</title></head><body><jsp:element name="xmlElement"><jsp:attribute name="xmlElementAttr"> Value for the attribute</jsp:attribute><jsp:body> Body for XML element</jsp:body></jsp:element></body></html>
The HTML code generated during execution is as follows:
<html xmlns="http://www.w3c.org/1999/xhtml" xmlns:jsp="http://java.sun.com/JSP/Page"> <head><title>Generate XML Element</ title></head><body><xmlElement xmlElementAttr="Value for the attribute"> Body for XML element</xmlElement></body></html>
The <jsp:text> action element allows the use of templates for writing text in JSP pages and documents. The syntax format is as follows:
<jsp:text>Template data</jsp:text>
The above text template cannot contain other elements, but 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 such as ${whatever > 0} because the > symbol is illegal. You can use the ${whatever gt 0} expression or the value embedded in a CDATA section.
<jsp:text><![CDATA[<br>]]></jsp:text>
If you need to declare DOCTYPE in XHTML, you must use the <jsp:text> action element. An example is as follows:
<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 above example to see the difference in execution results using <jsp:text> and not using this action element.