Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JSP action element


May 12, 2021 JSP


Table of contents


JSP action element

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

JSP actions allow you to dynamically insert files, reuse JavaBean components, redirect users to another page, and generate HTML code for Java plug-ins.

Action elements have only one syntax, which conforms to XML standards:

<jsp:action_name attribute="value" />

Action elements are basically predefined functions, and the JSP specification defines a series of standard actions, with JSP as a prefix, and the available standard action elements are as follows:

Grammar Describe
jsp:include A file is introduced when the page is requested.
jsp:useBean Look for or instantiate a JavaBean.
jsp:setProperty Set the properties of JavaBean.
jsp:getProperty Output the properties of a JavaBean.
jsp:forward Take the request to a new page.
jsp:plugin Generate OBJECT or EMBED tags for Java plug-ins based on browser type.
jsp:element Define dynamic XML elements
jsp:attribute Set dynamically defined XML element properties.
jsp:body Set up dynamically defined XML element content.
jsp:text Use a template that writes text in JSP pages and documents

Common properties

All action features have two properties: id properties and scope properties.

  • id properties:

    The id property is the unique identity of the action element and can be referenced on the JSP page. T he id value created by the action element can be called by PageContext.

  • scope properties:

    This property is used to identify the life cycle of an action element. T he id property is directly related to the scope property, which defines the lifetime of the associated id object. S cope properties have four possible values: (a) page, (b) request, (c) session, and (d) application.


the action element of the .lt;jsp:include.gt;

The action element is used to contain static and dynamic files. T his 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, which was introduced when the JSP file was converted to a servlet, is introduced earlier, and here the jsp:include action is different, the time to insert the file 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, which defines whether the cache is refreshed before the resource is included.

Instance

Here's how we define the two .jsp and the main .jsp, 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 put the above two files under the root of the server and access the main .jsp files. The results are as follows:

The include action Example
Today's date: 12-Sep-2013 14:54:22

lt;jsp: useBean.gt; action elements

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

This feature is useful because it allows us to take advantage of Java component reuse while avoiding the loss of JSP convenience from servlets.

The simplest syntax for jsp:useBean action is:

<jsp:useBean id="name" class="package.class" />

After the class is loaded, we can modify and retrieve the bean 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 full package name of the bean.
type Specifies the type of object variable that will be referenced.
beanName The name of the bean is specified by the instantiate() method of java.beans.Beans.

Before giving specific examples, let's look at the jsp:setProperty and jsp:getProperty action elements:


the action element of the setProperty

jsp:setProperty is used to set the properties of an instantiated bean object in two ways. F irst, you can use jsp:setProperty outside (back) of the jsp:useBean element, as follows:

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

At this point, jsp:setProperty executes whether jp:useBean finds an existing bean or creates a new bean instance. T he second usage is to put jsp:setProperty inside the jp:useBean element, as follows:

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

At this point, jsp:setProperty is executed only when a new Bean instance is created, and jsp:setProperty is not executed if an existing instance is used.

< />< p="" />
Property Describe
name Name properties are required. I t indicates which bean you want to set the property for.
property Property properties are required. I t indicates which property to set. T here is a special use: if the value of the property is """
value The value property is optional. T his property is used to specify the value of the bean property. S tring data is automatically converted to numbers, boolean, Boolean, byte, Byte, char, Character in the target class using the standard valueOf method. F or example, property values of the Boolean and Boolean types (such as "true") are converted by Boolean.valueOf, and property values of the int and Integer types (such as "42") are converted by Integer.valueOf. V alue and param cannot be used at the same time, but you can use any of them.
param param is optional. I t specifies which request parameter to use as the value of the bean property. I f the current request does not have parameters, nothing is done and the system does not pass the null to the set method of the bean property. T herefore, you can have the bean provide the default property values himself, modifying the default property values only if the request parameter explicitly specifies a new value.

lt;jsp: getProperty and action elements

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. The bean must be defined.
property Represents the value of the bean property to extract

Instance

Here's an example of us using a bean:

/* 文件: 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 instance and generate the TestBean.class file, which is copied to the server's directory where the Java class is officially stored, rather than being reserved for the directory of the modified class that can be automatically loaded (e.g., C: .apache-tomcat-7.0.2.2.webapps. ) 。 F or example, for Java Web Server, Bean and all the classes used by Bean should be placed in the classes directory, or encapsulated in a jar file and placed in the lib directory, but not under servlets. H ere's a very simple example of how it functions to mount a bean and then set/read its message properties.

Now let's call the .jsp in the main file:

 <html>
<head>
<title>Using JavaBeans in JSP</title>
</head>
<body>
<center>
<h2>Using JavaBeans in JSP</h2>
 
<jsp:useBean id="test" class="action.TestBean" />
 
<jsp:setProperty name="test"                      property="message"                      value="Hello JSP..." />
 
<p>Got message....</p>
 
<jsp:getProperty name="test" property="message" />
 
</center>
</body>
</html>

To execute the above file, the output looks like this:

Using JavaBeans in JSP
Got message....
Hello JSP...

the action element of the action

jsp:forward action to take the request to a different page. T he jsp:forward tag has only one property page. T he syntax format looks like this:

<jsp:forward page="Relative URL" />

Here are the properties associated with forward:

Property Describe
page The page property contains a relative URL. The value of the page can be given either directly or dynamically when requested, either as a JSP page or as a Java Servlet.

Instance

The following examples we use are two files: 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 put the above two files under the root of the server and access the main .jsp files. The results are as follows:

Today's date: 12-Sep-2010 14:54:22

the action element of the plugin

jsp:plugin action is used to insert OBJECT or EMBED elements necessary to run Java Applet through a Java plug-in, depending on the type of browser.

If a plug-in does not exist, it downloads the plug-in and executes the Java component. T he Java component can be an applet or a JavaBean.

Plugin actions have several properties that correspond to HTML elements to format Java components. Param elements can be used to pass parameters to an applet or bean.

Here are some typical examples of using plugin action elements:

<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 an applet to test the jsp:plugin action element, the element is a new element, and the error in component failure is sent to the user with an error message.


The action elements of slt;jsp:element> slt;jsp:attribute>, slt;jsp:body?gt; action elements

The XML elements are dynamically defined by the action elements. Dynamics are important, which means that XML elements are dynamically generated rather than static at compile time.

The following examples dynamically define 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 at 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 action element of the action

The action element allows the use of text-writing templates in JSP pages and documents in the following syntax format:

<jsp:text>Template data</jsp:text>

The above text template cannot contain other elements, only text and EL expressions (Note: EL expressions will be covered in subsequent chapters). N ote that in an XML file, you can't use expressions such as $? Y ou can either use the $(whatever gt 0) expression or a value embedded in a CDATA section.

<jsp:text><![CDATA[<br>]]></jsp:text>

If you need to declare DOCTYPE in XHTML, you must use the action element, 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 to use the difference between using the above instance, and not using the action element to execute the results.

< />