13. JSP action

Source: Internet
Author: User
Tags html comment

JSP actions use tags in XML syntax to control the behavior of the Servlet engine. JSP actions can be used to dynamically Insert Files, reuse the JavaBean Component, redirect users to another page, and generate HTML code for the Java Plug-in.

JSP actions include:

Jsp: include: Introduce a file when the page is requested.
Jsp: useBean: Find or instantiate a JavaBean.
Jsp: setProperty: Set attributes of JavaBean.
Jsp: getProperty: outputs the attributes of a JavaBean.
Jsp: forward: Transfers requests to a new page.
Jsp: plugin: generate the OBJECT or EMBED tag for the Java Plug-in based on the browser type.
13.1 jsp: include action

This action inserts the specified file into the page being generated. The syntax is as follows:
<Jsp: include page = "relative URL" flush = "true"/>



The include command has been introduced earlier. It introduces the file when the JSP file is converted into a Servlet. the jsp: include action here is different, the file is inserted when the page is requested. The introduction time of the jsp: include action file determines that its efficiency is slightly lower, and the Referenced File cannot contain some JSP code (for example, the HTTP header cannot be set ), but it is much more flexible.

For example, the following JSP page inserts four news summaries into a "What's New ?" Page. Only the four files need to be changed when the news abstract is changed, but the main JSP page can not be modified:

WhatsNew. jsp
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<TITLE> What's New </TITLE>
</HEAD>

<Body bgcolor = "# FDF5E6" TEXT = "#000000" LINK = "# 0000EE"
VLINK = "# 551A8B" ALINK = "# FF0000">

<CENTER>
<Table border = 5 BGCOLOR = "# EF8429">
<TR> <th class = "TITLE">
What's New at JspNews.com </TABLE>
</CENTER>
<P>
Here is a summary of our four most recent news stories:
<OL>
<LI> <jsp: include page = "news/Item1.html" flush = "true"/>
<LI> <jsp: include page = "news/Item2.html" flush = "true"/>
<LI> <jsp: include page = "news/Item3.html" flush = "true"/>
<LI> <jsp: include page = "news/Item4.html" flush = "true"/>
</OL>
</BODY>
</HTML>



13.2 jsp: useBean action

Jsp: useBean action is used to load a JavaBean that will be used on the JSP page. This function is very useful, because it allows us to take advantage of Java Component Reuse, but also avoids the convenience of separating JSP from Servlet. The simplest syntax for jsp: useBean actions is:
<Jsp: useBean id = "name" class = "package. class"/>



The meaning of this line of code is: "Create an instance of the class specified by the class property, and then bind it to the variable whose name is given by the id property ". However, as we will see next, defining a scope attribute can associate Bean with more pages. In this case, the jsp: useBean action creates an object instance only when the Bean with the same id and scope does not exist. At the same time, it is necessary to obtain the reference of the existing Bean.

After obtaining the Bean instance, you can modify the Bean attributes either through the jsp: setProperty action or by using the object variable named by the id attribute in the Scriptlet, you can call this object to explicitly modify its properties. This reminds us that when we say "a Bean has a property foo with the type of X", it means "this class has a getFoo method with the return value type of X, there is also a setFoo method that takes the value of the X type as the parameter ".

Details about jsp: setProperty actions will be discussed later. However, we must understand that we can either directly provide a value through the value attribute of the jsp: setProperty action, or declare the Bean property value from the specified request parameter through the param attribute, the Bean attribute can also be listed to indicate that its value should come from the variable with the same name in the request parameter.

To read Bean attributes in a JSP expression or Scriptlet, call the corresponding getXXX method, or, more generally, use the jsp: getProperty action.

Note that class files containing beans should be placed in the directory where the server officially stores Java classes, rather than retaining them to the directories of classes that can be automatically loaded after modification. For example, for Java Web Server, the classes used by Bean and all beans should be put into the classes directory, or encapsulated into the jar file and put into the lib directory, but should not be put into the servlets.

The following is a simple example. Its function is to load a Bean and set/read its message attribute.

BeanTest. jsp
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<TITLE> Reusing JavaBeans in JSP </TITLE>
</HEAD>

<BODY>
<CENTER>
<Table border = 5>
<TR> <th class = "TITLE">
Reusing JavaBeans in JSP </TABLE>
</CENTER>
<P>

<Jsp: useBean id = "test" class = "hall. SimpleBean"/>
<Jsp: setProperty name = "test"
Property = "message"
Value = "Hello WWW"/>
<H1> Message: <I>
<Jsp: getProperty name = "test" property = "message"/>
</I> </H1>

</BODY>
</HTML>



SimpleBean. java

The BeanTest page uses a SimpleBean. The SimpleBean code is as follows:
Package hall;

Public class SimpleBean {
Private String message = "No message specified ";

Public String getMessage (){
Return (message );
}

Public void setMessage (String message ){
This. message = message;
}
}



13.3 further description of jsp: useBean

The simplest way to use Bean is to load the Bean with the following code:
<Jsp: useBean id = "name" class = "package. class"/>



Then, modify and extract Bean attributes through jsp: setProperty and jsp: getProperty. But there are two points to note. First, we can also instantiate the Bean in the following format:
<Jsp: useBean...>
Body
</Jsp: useBean>



It means that the Body part is executed only when the Bean is instantiated for the first time. If an existing Bean instance is used, the Body part is not executed. As we will introduce below, jsp: useBean does not always mean creating a new Bean instance.

Second, in addition to id and class, jsp: useBean has three other attributes: scope, type, and beanName. The following table briefly describes the usage of these attributes. Attribute usage
Id: Name the variable that references the Bean. If a Bean instance with the same id and scope can be found, the jsp: useBean Action uses an existing Bean instance instead of creating a new instance.
Class specifies the full package name of Bean.
Scope specifies the context in which the Bean is available. One of the following values can be taken down: page, request, session, and application.
The default value is page, indicating that the Bean is only available on the current page (stored in the PageContext of the current page ).
Request indicates that the Bean is valid in the current customer request (stored in the ServletRequest object ).
Session indicates that the Bean is valid for all pages in the current HttpSession.
Finally, if the value is application, the Bean is valid for all pages with the same ServletContext.
Scope is important because jsp: useBean instances new objects only when there are no objects with the same id and scope; if the existing objects with the same id and scope are used, the existing objects are used directly. In this case, any content between the start and end tags of jsp: useBean will be ignored.

Type specifies the type of the variable that references this object. It must be the name of the Bean class, the super class name, or one of the interfaces implemented by this class. Remember that the variable name is specified by the id attribute.
BeanName specifies the Bean name. If the type and beanName attributes are provided, the class attributes can be omitted.


13.4 jsp: setProperty action

Jsp: setProperty is used to set attributes of instantiated Bean objects. First, you can use jsp: setProperty outside the jsp: useBean element (later), as shown below:
<Jsp: useBean id = "myName".../>
...
<Jsp: setProperty name = "myName"
Property = "someProperty".../>



In this case, whether jsp: useBean finds an existing Bean or creates a new Bean instance, jsp: setProperty will be executed. The second method 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>



In this case, jsp: setProperty is executed only when the Bean instance is created. If an existing instance is used, jsp: setProperty is not executed.

Jsp: setProperty action has the following four attributes: attribute description
The name attribute is required. It indicates which Bean to set the attribute.
Property is required. It indicates the attribute to be set. There is a special usage: If the property value is "*", it indicates that all request parameters whose names match the Bean property name will be passed to the corresponding property set method.
The value property is optional. This attribute is used to specify the Bean property value. String data is automatically converted to numbers, boolean, Boolean, byte, Byte, char, and Character by using the standard valueOf method in the target class. For example, values of the boolean and Boolean types (such as "true") are converted using Boolean. valueOf, and values of the int and Integer types (such as "42") are converted using Integer. valueOf.
Value and param cannot be used at the same time, but any of them can be used.

Param is optional. It specifies the request parameter used as the Bean attribute value. If the current request has no parameters, nothing will be done. The system will not pass null to the set Method of the Bean attribute. Therefore, you can allow the Bean to provide its own default attribute value. The default attribute value can be modified only when the new value is explicitly specified in the request parameter.
For example, the following code snippet indicates that if a numItems request parameter exists, set the value of the numberOfItems attribute to the value of the Request Parameter numItems; otherwise, nothing will be done.

<Jsp: setProperty name = "orderBean"
Property = "numberOfItems"
Param = "numItems"/>

If both value and param are omitted, the effect is equivalent to providing a param and its value is equal to the value of property. Further, with the idea of automatically assigning values using the same request parameter and attribute name, you can also specify "*" in the property (Bean attribute name), and then omit the value and param. In this case, the server will view all Bean attributes and request parameters. If the two names are the same, the values are automatically assigned.



The following is an example of using JavaBean to calculate the prime number. If the request contains a numDigits parameter, the value is passed to the numDigits attribute of the Bean. numPrimes is similar.

JspPrimes. jsp
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<TITLE> Use JavaBean in JSP </TITLE>
</HEAD>

<BODY>

<CENTER>
<Table border = 5>
<TR> <th class = "TITLE">
Use JavaBean in JSP </TABLE>
</CENTER>
<P>

<Jsp: useBean id = "primeTable" class = "hall. NumberedPrimes"/>
<Jsp: setProperty name = "primeTable" property = "numDigits"/>
<Jsp: setProperty name = "primeTable" property = "numPrimes"/>

Some <jsp: getProperty name = "primeTable" property = "numDigits"/>
Digit primes:
<Jsp: getProperty name = "primeTable" property = "numberedList"/>

</BODY>
</HTML>



Note: The NumberedPrimes code is omitted.

13.5 jsp: getProperty action

Jsp: getProperty action extracts the value of the specified Bean property, converts it to a string, and then outputs it. Jsp: getProperty has two required attributes: name, indicating the Bean name; property, indicating the value of the attribute to be extracted. The following is an example. More examples can be found in the previous article.
<Jsp: useBean id = "itemBean".../>
...
<UL>
<LI> Number of items:
<Jsp: getProperty name = "itemBean" property = "numItems"/>
<LI> Cost of each:
<Jsp: getProperty name = "itemBean" property = "unitCost"/>
</UL>



13.6 jsp: forward action

Jsp: forward action transfers the request to another page. Jsp: forward only has one property page. The page attribute contains a relative URL. The page value can be provided directly or dynamically calculated during the request, as shown in the following example:
<Jsp: forward page = "/utils/errorReporter. jsp"/>
<Jsp: forward page = "<% = someJavaExpression %>"/>



13.7 jsp: plugin action

Jsp: the plugin action is used to insert the OBJECT or EMBED elements necessary to run the Java Applet through the Java Plug-in based on the browser type.

Appendix: JSP comments and character reference conventions

Below are some special tags or characters that you can use to insert comments or may be considered to have special meanings. Syntax usage
<% -- Comment -- %> JSP annotation, also known as "Hide annotation ". The JSP Engine ignores it. All JSP script elements, commands, and actions in the tag do not work.
<! -- Comment --> HTML comment, also known as "output comment", is directly displayed in the result HTML document. All JSP script elements, commands, and actions in the tag are executed normally.
<\ % In the template text (static HTML), you actually want to use "<%.
% \> In the script element, you actually want to use "%>.
\ 'Use single quotes in single quotes. However, you can use either single or double quotation marks, and the other one will have a common meaning.
\ "Double quotation marks in the attribute of double quotation marks. For more information, see.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.