Latest web development tutorials

JSP action elements

JSP and instruction elements is different, JSP action elements play a role in the request processing phase. JSP action element is written using XML syntax.

Use JSP actions can dynamically insert a file, reuse JavaBean components, redirect the user to another page, HTML code is generated for a Java plug-in.

Action is only one syntax element, it conforms to XML standards:

<jsp:action_name attribute="value" />

Action elements are basically predefined functions, JSP specification defines a set of standard action, it uses JSP as a prefix, standard action elements are available:

grammar description
jsp: include The introduction of a document when the page is requested.
jsp: useBean Find or instantiate a JavaBean.
jsp: setProperty Set JavaBean properties.
jsp: getProperty The output of a JavaBean property.
jsp: forward The request to a new page.
jsp: plugin Generated OBJECT or EMBED tag for the Java plug-in based on the type of browser.
jsp: element Define dynamic XML element
jsp: attribute Set dynamically defined XML element attributes.
jsp: body Set dynamically defined XML element content.
jsp: text Use written text in JSP pages and document templates

Common attributes

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

  • id attribute:

    id attribute uniquely identifies the action element can be referenced in JSP pages. id value of the action element created can be called by PageContext.

  • scope attribute:

    This attribute is used to identify the action elements of the life cycle. id attribute and scope attributes are directly related, scope attribute defines the lifetime of the associated object id. scope property has four possible values: (a) page, (b) request, (c) session, and (d) application.


<Jsp: include> action elements

<Jsp: include> action element is used to contain static and dynamic document. The action of the specified file into the page being generated. Syntax is as follows:

<jsp:include page="相对 URL 地址" flush="true" />

It has been introduced include instruction, which was introduced in the JSP document file is converted into a Servlet when and where the jsp: include action is different from the time when the file is inserted in the page being requested.

The following is a list of action include the relevant properties.

Attributes description
page Contained in the page relative URL address.
flush Boolean property that contains the resource definitions before whether to refresh the cache.

Examples

We define the following two files date.jsp and main.jsp, code as follows:

date.jsp file code:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<p>
   今天的日期是: <%= (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>本教程(w3big.com)</title>
</head>
<body>

<h2>include 动作实例</h2>
<jsp:include page="date.jsp" flush="true" />

</body>
</html>

Now more than two files in the root directory of the server, access main.jsp file. Display results as follows:

include 动作实例

今天的日期是: 2016-6-25 14:08:17

<Jsp: useBean> action elements

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

This feature is very useful because it allows us to play a Java component reuse advantage.

jsp: useBean action simplest syntax is:

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

After the class is loaded, we can either through jsp: to modify and retrieve bean properties getProperty action: setProperty and jsp.

The following is a list of attributes associated useBean action.

Attributes description
class Specify the full package name of Bean.
type Specifies the object reference type variable.
beanName () Method to specify the name of Bean java.beans.Beans by the instantiate.

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


<Jsp: setProperty> action elements

jsp: setProperty to set the already instantiated Bean object attributes, in two ways. First, you can jsp: useBean use outside element (back) jsp: setProperty, as follows:

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

In this case, regardless of jsp: useBean is to find an existing Bean, or create a new Bean instance, jsp: setProperty is executed. The second is to use jsp: setProperty into jsp: useBean element of interior, as follows:

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

At this time, jsp: setProperty only in the new Bean instance will be implemented, if you are using an existing instance is not performed jsp: setProperty.

jsp: setProperty action has the following four properties in the following table:

Attributes description
name name attribute is required. It is said to set the properties of which Bean.
property property attribute is required. It indicates which attribute to set. There is a special usage: If the value of the property is "*" indicates a request for the names of all parameters and Bean attribute names match will be passed to the corresponding attribute set method.
value value attribute is optional. This attribute is used to specify the value of the Bean property. String data in the target class through standard valueOf method automatically converted into digital, boolean, Boolean, byte, Byte, char, Character. For example, boolean and Boolean property value type (for example, "true") by Boolean.valueOf conversion, the property value of type int and Integer (such as "42") by Integer.valueOf conversion. param value and can not be used, but you can use any one of them.
param param is optional. It specifies which request parameters as the value of Bean property. If the current request has no parameters, then do not do anything, the system will not be passed to the null set method Bean property. So you can make your own Bean provides a default property value, only when the request parameters explicitly specify a new value to modify the default property values.

<Jsp: getProperty> action elements

jsp: getProperty action to extract the value of the specified Bean property, converted to a string, and then output. Syntax is as follows:

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

The following table is a property associated with the getProperty:

Attributes description
name Bean property name to retrieve. Bean must be defined.
property It represents the value of the property to extract Bean

Examples

The following example we use the Bean:

package com.w3big.main;

public class TestBean {
   private String message = "本教程";
 
   public String getMessage() {
      return(message);
   }
   public void setMessage(String message) {
      this.message = message;
   }
}

Compile the above sample files TestBean.java:

$ javac TestBean.java

After compilation will generate in the current directory TestBean.class a file, copy the file to the project under WebContent JSP / WEB-INF / classes / com / w3big / main (com / w3big / main current path of the package, there is no need to manually create).

Here is an Eclipse directory structure:

Here is a very simple example, its function is to load a Bean, and then set / read its message attributes.

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

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>本教程(w3big.com)</title>
</head>
<body>

<h2>Jsp 使用 JavaBean 实例</h2>
<jsp:useBean id="test" class="com.w3big.main.TestBean" />
 
<jsp:setProperty name="test" 
                    property="message" 
                    value="本教程..." />
 
<p>输出信息....</p>
 
<jsp:getProperty name="test" property="message" />

</body>
</html>

Browser access, the implementation of the above documents, output is as follows:


<Jsp: forward> action elements

jsp: forward action the request to another page. jsp: forward tag has only one property page. The syntax is as follows:

<jsp:forward page="相对 URL 地址" />

The following is a forward associated properties:

Attributes description
page page attribute contains a relative URL. page value either given directly to be dynamically calculated at the time of the request, which can be a JSP page or a Java Servlet.

Examples

The following example we use two documents, namely: date.jsp and main.jsp.

date.jsp file code is as follows:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<p>
   今天的日期是: <%= (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>本教程(w3big.com)</title>
</head>
<body>

<h2>forward 动作实例</h2>
<jsp:forward page="date.jsp" />
</body>
</html>

Now more than two files in the root directory of the server, access main.jsp file. Display results as follows:

今天的日期是: 2016-6-25 14:37:25

<Jsp: plugin> action elements

jsp: plugin action is used according to the type of browser, insert OBJECT or EMBED element running through the Java plug-in Java Applet necessary.

If the plug does not need to exist, it will download the plug-in, and then execute Java components. Java components can be an applet or a JavaBean.

plugin action corresponding to a plurality of HTML element attributes for formatting Java components. param element can be used to pass parameters to Applet or Bean.

Here is a typical example of the 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 to use the applet to test jsp: plugin action elements, <fallback> element is a new element, an error occurs in the component failure is sent to the user error message.


<Jsp: element>, <jsp: attribute>, <jsp: body> element Action

<Jsp: element>, <jsp: attribute>, <jsp: body> element Action dynamically defined XML elements. Dynamic is very important, which means that XML elements are dynamically generated at compile time rather than static.

The following example defines a dynamic XML elements:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>本教程(w3big.com)</title>
</head>
<body>
<jsp:element name="xmlElement">
<jsp:attribute name="xmlElementAttr">
   属性值
</jsp:attribute>
<jsp:body>
   XML 元素的主体
</jsp:body>
</jsp:element>
</body>
</html>

Browser to access the following pages, the output is as follows:


<Jsp: text> element Action

<Jsp: text> action element allows the use of templates to write a text in JSP pages and documents, the syntax is as follows:

<jsp:text>模板数据</jsp:text>

The above text template can not contain other elements, can only contain text and EL expressions (Note: EL expression will be described in subsequent sections). Please note that in the XML file, you can not use an expression such as $ {whatever> 0}, because the> symbol is illegal. You can use the $ {whatever gt 0} value expression or 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 elements, examples are 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 above example <jsp: text> element differences and not to use the results of the action.