JSP actions

Unlike JSP actions elements, JSP action elements work during the request processing phase. JSP action elements are written in XML syntax.

Using JSP actions 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 that conforms to the XML standard:

<jsp:action_name attribute="value" />

Action elements are basically pre-defined functions. The JSP specification defines a set of standard actions, which are prefixed with JSP. The standard action elements available are as follows:

Syntax Description
jsp:include Introduces a file when the page is requested.
jsp:useBeanFind or instantiate a JavaBean.
jsp:setPropertySet the properties of the JavaBean.
jsp:getPropertyExports the properties of a JavaBean.
jsp:forwardGo the request to a new page.
jsp:pluginGenerate OBJECT or EMBED tags for Java plugins based on browser type.
jsp:elementDefine dynamic XML elements
jsp:attributeSets dynamically defined XML element attributes.
jsp:bodySets the content of dynamically defined XML elements.
jsp:textUse a template for writing text in JSP pages and documents

Common Attributes

All action elements have two properties: the id attribute and the scope attribute.

  • 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 called through the PageContext.

  • Scope attribute:

    This attribute is used to identify the life cycle of an action element. The id attribute has a direct relationship with the scope attribute, and the scope attribute defines the lifetime of the associated id object. The scope attribute has four possible values: (a) page, (b) request, (c)session, and (d) application.

<jsp:include>Action element

<jsp:include>Action elements are used to contain 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" flush="true" />

I have already introduced the include directive, which introduces the file when the JSP file is converted to a servlet. 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 The relative URL address contained in the page.
flushBoolean attribute, which defines whether to flush the buffer before containing the resource.


The following we have defined two files date.jsp and main.jsp, the code looks like this:

date.jsp file code:

<%@ page language="java" contentType="text/html; charset=UTF-8"
Today's date is: <%= (new java.util.Date()).toLocaleString()%>

main.jsp file code:

<%@ page language="java" contentType="text/html; charset=UTF-8"
<!DOCTYPE html>
<meta charset="utf-8">
<title> tutorial(welookups.com)</title>

<h2>include Action instance</h2>
<jsp:include page="date.jsp" flush="true" />


Now put the above two files in the root directory of the server and access the main.jsp file. The results are as follows:

include Action instance

Today's date is: 2016-6-25 14:08:17

<jsp:useBean>Action element


jsp:useBean action is used to load a JavaBean that will be used in a JSP page.

This feature is very useful because it allows us to take advantage of the reuse of Java components.

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 through the jsp:setProperty and jsp:getProperty actions.

The following is a list of attributes related to the useBean action.

Properties Description
classSpecifies the full package name of the bean.
type specifies the type that will reference the object variable.
beanNameSpecifies the name of the bean via the instantiate() method of java.beans.Beans .

Before giving a concrete 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 a bean object that has been instantiated. There are two uses. 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 execute. The second usage is to put jsp:setProperty inside the jsp:useBean element as follows:

<jsp:useBean id="myName" ... >
   <jsp:setProperty name="myName" property="someProperty" .../>

At this point, jsp:setProperty will only be executed when a new bean instance is created. If an existing instance is used, jsp:setProperty will not be executed.

jsp: The setProperty action has the following four properties, as shown in the following table:

The The The
Attribute Description
namename attribute is required. It indicates which bean to set the property to.
propertyproperty attribute is required. It indicates which property to set. There is a special usage: If the value of the property is "*", it means that all request parameters whose name matches the name of the bean property will be passed to the corresponding property set method.
value value attribute is optional. This property is used to specify the value of the bean property. String data is automatically converted to numbers, boolean, Boolean, byte, Byte, char, Character in the target class via the standard valueOf method. For example, boolean and Boolean type property values ​​(such as "true") are converted by Boolean.valueOf, and int and Integer type property values ​​(such as "42") are converted by Integer.valueOf. Value and param cannot be used at the same time, but any one of them 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, then nothing is done, the system will not pass null to the set method of the bean property. Therefore, you can let the bean itself provide the default property value, and only modify the default property value when the request parameter explicitly specifies a new value.

<jsp:getProperty>Action element

jsp: The getProperty action extracts the value of the specified bean property, converts it to a string, and outputs it. The syntax is as follows:

<jsp:useBean id="myName" ... />
<jsp:getProperty name="myName" property="someProperty" .../>

The following table is the properties associated with getProperty:

Properties Description
nameThe name of the bean property to retrieve. The bean must be defined.
property indicates that you want to extract the value of the bean attribute


The following example we used Bean:

package com.welookups.main;

public class TestBean {
   private String message = "tutorial";
   public String getMessage() {
   public void setMessage(String message) {
      this.message = message;

Compile the above example file TestBean.java :

$ javac TestBean.java

A TestBean.class file will be generated in the current directory after compilation. Copy the file to WebContent/WEB-INF/classes/com/welookups/main of the current JSP project Under (com/welookups/main package path, no need to manually create).