JSP syntax action)

Source: Internet
Author: User

 

JSP action
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>
</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 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.

 

 

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.