English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Different from JSP directive elements, JSP action elements take effect during the request handling phase. JSP action elements are written in XML syntax.
JSP actions 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 basically predefined functions, the JSP specification defines a series of standard actions, which use JSP as the prefix, and the available standard action elements are as follows:
Syntax | Description |
---|---|
jsp:include | Include a file when the page is requested. |
jsp:useBean | Search or instantiate a JavaBean. |
jsp:setProperty | Set the property of a JavaBean. |
jsp:getProperty | Output the property of a JavaBean. |
jsp:forward | Redirect the request to a new page. |
jsp:plugin | Generate OBJECT or EMBED tags for Java plugins based on the browser type. |
jsp:element | Define dynamic XML elements |
jsp:attribute | Set the attributes of dynamically defined XML elements. |
jsp:body | Set the content of dynamically defined XML elements. |
jsp:text | Template for writing text used 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, which 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 lifecycle of the action element. The id attribute is directly related to the scope attribute, which 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 address" flush="true"> />
The include directive has been introduced before, which is introduced when the JSP file is converted to a Servlet. However, the jsp:include action here is different, the time to insert the file is when the page is requested.
The following is a list of attributes related to the include action.
Attribute | Description |
---|---|
page | Relative URL address included in the page. |
flush | Boolean attribute, defining whether to refresh the cache area before including resources. |
In the following, we define two files. date.jsp and main.jsp, as shown below:
The code of the 'date.jsp' file is as follows:
<%@ 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>Basic Tutorial Website(oldtoolbag.com)</title> </head> <body> <h2>include action example</h2> <jsp:include page="date.jsp" flush="true" /> </body> </html>
Now place the above two files in the root directory of the server and access the main.jsp file. The result is as follows:
include action example Today's date is: 2016-6-25 14:08:17
jsp:useBean The 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 the reusability of Java components.
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.
Attribute | Description |
---|---|
class | Specifies the full package name of the Bean. |
type | Specifies the type of the object variable to be referenced. |
beanName | The name of the Bean is specified through the 'java.beans.Beans' instantiate() method. |
Before giving specific examples, let's first take a look at the 'jsp:setProperty' and 'jsp:getProperty' action elements:
The 'jsp:setProperty' action is used to set the properties of a bean that has already been instantiated, and there are two usages. 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" ...>/>
At this point, whether 'jsp:useBean' finds an existing Bean or creates a new Bean instance, 'jsp:setProperty' will always execute. 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>
At this point, '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 the attribute is to be set on. |
property | The property attribute is required. It represents which property to set. There is a special usage: if the value of property is "}}*" indicates that all request parameters whose names match the names and Bean property names will be passed to the corresponding property set method. |
value | value attribute is optional. This attribute is used to specify the value of the Bean property. String data will be automatically converted to numbers, boolean, Boolean, byte, Byte, char, Character in the target class through the standard valueOf method. For example, boolean and Boolean type property values (such as "true") are converted through Boolean.valueOf, int and Integer type property values (such as "42is converted through 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 there are no parameters in the current request, nothing is done, and the system does not pass null to the Bean property's set method. Therefore, you can let the Bean provide default property values, and the default property values are only modified when the new value is explicitly specified by the request parameter. |
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:
<jsp:useBean id="myName" ... /> ... <jsp:getProperty name="myName" property="someProperty" .../>
The following table lists the properties associated with getProperty:
Attribute | Description |
---|---|
name | The name of the Bean property to be retrieved. The Bean must be defined. |
property | Represents the value of the Bean property to be extracted |
In the following example, we use Bean:
package com.w3codebox.main; public class TestBean { private String message = "Basic Tutorial Website"; 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 file will be generated in the current directory. TestBean.class file, Copy this file to the current JSP project's WebContent/WEB-INF/classes/com/w3codebox/maindown( com/w3codebox/main package path, no need to create manually).
Below is a directory structure diagram in Eclipse:
Below is a very simple example, which loads a Bean and then sets/Read its message attribute.
Now let's call this Bean in the main.jsp file:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Basic Tutorial Website(oldtoolbag.com)</title> </head> <body> <h2>Jsp use JavaBean example</h2> <jsp:useBean id="test" /> <jsp:setProperty name="test" property="message" value="Basic Tutorial..." /> <p>Output information....</p> <jsp:getProperty name="test" property="message" /> </body> </html>
Browser access, execute the above files, and the output is as follows:
jsp:forward action redirects 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 address" />
The following are the properties associated with forward:
Attribute | Description |
---|---|
page | The page attribute contains a relative URL. The value of page can be provided directly or dynamically calculated at the time of the request, which can be a JSP page or a Java Servlet. |
In the following example, we use two files, namely: date.jsp and main.jsp.
The code of the date.jsp file is as follows:
<%@ 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>Basic Tutorial Website(oldtoolbag.com)</title> </head> <body> <h2>forward action example</h2> <jsp:forward page="date.jsp" /> </body> </html>
Now place the above two files in the root directory of the server and access the main.jsp file. The result is as follows:
Today's date is: 2016-6-25 14:37:25
The jsp:plugin action is used to insert the necessary OBJECT or EMBED elements required for running Java Applets through Java plugins based on the browser type.
If the required plugin is not available, it will download the plugin and then execute the Java component. Java components can be an applet or a JavaBean.
The plugin action has multiple corresponding HTML element attributes for formatting 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" width="60" height="80"> <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 applet to test the jsp:plugin action element, the <fallback> element is a new element that sends error information to the user when the component fails.
<jsp:element> , <jsp:attribute>, <jsp:body>Dynamic definition of XML elements by Action Element. Dynamic is very important, which means that XML elements are dynamically generated at compile time rather than statically.
The following example dynamically defines XML elements:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Basic Tutorial Website(oldtoolbag.com)</title> </head> <body> <jsp:element name="xmlElement"> <jsp:attribute name="xmlElementAttr"> Attribute Value </jsp:attribute> <jsp:body> The main body of the XML element </jsp:body> </jsp:element> </body> </html>
The browser accesses the following page and the output result is as shown below:
<jsp:text> action element allows the use of template data for writing text in JSP pages and documents, with the syntax format as follows:
jsp:text template data/jsp:text
The above text template cannot contain repeated elements; it can only contain text and EL expressions (note: EL expressions will be introduced in subsequent chapters). Please note that you cannot use expressions like ${whatever > 0} in XML files because the > symbol is illegal. You can use the expression ${whatever gt 0} or embed a value within 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, as shown in the following 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 between using <jsp:text> and not using this action element for the above examples.