13, JSP action _jsp programming

Source: Internet
Author: User
Tags flush java web
JSP actions control the behavior of the servlet engine using tags in the XML syntax format. Using JSP actions, you can dynamically insert files, reuse JavaBean components, redirect users to another page, and generate HTML code for Java plug-ins.

JSP actions include:

Jsp:include: Introduces a file when the page is requested.
Jsp:usebean: Find or instantiate a JavaBean.
Jsp:setproperty: Sets the properties of the JavaBean.
Jsp:getproperty: Outputs a JavaBean attribute.
Jsp:forward: Move the request to a new page.
Jsp:plugin: Generates an 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 directive, introduced earlier, introduces files when a JSP file is converted to a servlet, and where the jsp:include action is different, the time to insert the file is when the page is requested. The Jsp:include action's file introduction time determines that it is slightly more efficient, and the referenced file cannot contain some JSP code (for example, HTTP headers cannot be set), but its flexibility is much better.

For example, the following JSP page inserts a 4 News digest into a "What ' s New?" Page. Changes to the news digest only need to change these four files, 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>
We are 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

The Jsp:usebean action is used to load a javabean that will be used in the JSP page. This feature is useful because it allows us to leverage the benefits of Java component reuse while avoiding the convenience of loss JSP that distinguishes it from the 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 attribute, and then bind it to the variable whose name is given by the id attribute." However, as we'll see next, defining a scope property allows the bean to be associated with more pages. At this point, the Jsp:usebean action creates a new object instance only if a bean with the same ID and scope does not exist, and it becomes necessary to obtain a reference to an existing bean.

After you get the bean instance, you can modify the bean's properties either through the Jsp:setproperty action or by using the object variable named by the id attribute in Scriptlet to modify its properties explicitly by invoking the object's method. This reminds us that when we say "a bean has a property of type X", it means "this class has a Getfoo method with a return value type of x, and a Setfoo method with a value of type X as a parameter".

Detailed information about the Jsp:setproperty action is discussed later. But it must now be understood that we can either provide a value directly from the Value property of the Jsp:setproperty action, or declare the property value of the bean from the specified request parameter through the Param property. You can also list the Bean property to indicate that its value should come from a variable of the same name in the request parameter.

The Bean property is read in a JSP expression or scriptlet by invoking the corresponding GetXXX method, or, more generally, by using the Jsp:getproperty action.

Note the class file containing the bean should be placed in the directory where the server formally hosts the Java class, not the directory of the classes that can be automatically loaded after the modification. For example, for a Java WEB server, classes used by beans and all beans should be put into the classes directory, or packaged into a jar file and put into the Lib directory, but should not be placed under Servlets.

The following is a very simple example of the function of loading a bean and then setting/reading 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"/>
Message: <I>
<jsp:getproperty name= "test" property= "message"/>
</I>

</BODY>
</HTML>



Simplebean.java

The Beantest page uses a simplebean. The code for Simplebean 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 notes on the Jsp:usebean

The easiest way to use a bean is to load the bean with the following code:
<jsp:usebean id= "name" class= "Package.class"/>



The Bean's properties are then modified and extracted through Jsp:setproperty and Jsp:getproperty. But there are two points to be aware of. 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 the first time the bean is instantiated, and if the existing bean instance is used, the body part is not executed. As will be described below, Jsp:usebean does not always mean creating a new bean instance.

Second, in addition to the ID and class, Jsp:usebean has three other properties, namely: Scope,type,beanname. The following table briefly describes the use of these properties. Property usage
The ID names the variable that references the bean. If you can find a bean instance with the same ID and scope, the Jsp:usebean action uses an existing bean instance instead of creating a new instance.
class specifies the full package name of the Bean.
SCOPE Specifies the context in which the bean is available, and can take one of the following four values: Page,request,session and application.
The default value is page, which means that the bean is available only on the current page (saved in the PageContext of the current page).
Request indicates that the bean is valid within the current customer request (stored within the ServletRequest object).
Session indicates that the bean is valid for all pages within the current httpsession.
Finally, if the value is application, it means that the bean is valid for all pages that have the same servletcontext.
Scope is important because Jsp:usebean instantiates new objects only if there is no object with the same ID and scope, and if an existing ID and scope have the same object, then the JSP: Any content between the Usebean start and end tags is ignored.

Type specifies a variable that refers to the object, which must be the name of the Bean class, the superclass name, and one of the interface names that the class implements. Keep in mind that the name of the variable is specified by the id attribute.
BEANNAME Specifies the name of the bean. If the type attribute and the Beanname property are provided, the class attribute is allowed to be omitted.


13.4 Jsp:setproperty Action

There are two uses for Jsp:setproperty to set properties of a Bean object that has been instantiated. First, you can use Jsp:setproperty on the outside (back) of the Jsp:usebean element, as follows:
<jsp:usebean id= "MyName" .../>
...
<jsp:setproperty name= "MyName"
property= "Someproperty" .../>



At this point, the Jsp:setproperty executes whether Jsp:usebean finds an existing bean or a new instance of the bean is created. The second use is to put jsp:setproperty into the interior of the Jsp:usebean element, as follows:
<jsp:usebean id= "MyName" ... >
...
<jsp:setproperty name= "MyName"
property= "Someproperty" .../>
</jsp:useBean>



At this point, Jsp:setproperty executes only when a new bean instance is created, and if the existing instance is used, the jsp:setproperty is not executed.

The Jsp:setproperty action has the following four properties: attribute description
The name Name property is required. that represents which bean to set the property to.
Property properties are required. that represents which property to set. There is a special usage: if the value of the property is "*", the request parameters that represent all the names and the bean attribute names are passed to the corresponding property set method.
The value Value property 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, and Character in the target class by means of a standard valueof method. For example, Boolean-and Boolean-type property values (such as "true") are converted by boolean.valueof, int and integer-type property values (such as "42") by integer.valueof.
Value and param cannot be used at the same time, but any of them can be used.

param param is optional. It specifies which request parameter is used as the value of the Bean property. If the current request has no parameters, nothing is done and the system does not pass NULL to the set method of the Bean property. Therefore, you can let the bean itself provide default property values and modify the default property values only if the request parameter explicitly specifies a new value.
For example, the following code fragment indicates that the value of the Numberofitems property is set to the value of the request parameter NumItems if there is a NumItems request parameter;

<jsp:setproperty name= "Orderbean"
Property= "Numberofitems"
Param= "NumItems"/>

If both value and Param are omitted, the effect is equivalent to providing a param with a value equal to the property. Further using this idea of automatic assignment with the same request parameters and attribute names, you can also specify "*" in the property (name of the bean attribute) and omit value and Param. At this point, the server looks at all the bean properties and request parameters, and automatically assigns them if they have the same name.



Here is an example of using JavaBean to compute prime numbers. If there is a numdigits parameter in the request, the value is passed to the Bean's Numdigits property; Numprimes is similar.

jspprimes.jsp
! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en"
<HTML>
<HEAD>
<TITLE> use JavaBean in JSPs </TITLE>
</HEAD>

<BODY>

<CENTER>
<table border=5>
<TR> <th class= "TITLE" >
Use JavaBean </TABLE> in a JSP
</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: Numberedprimes code is slightly.

13.5 Jsp:getproperty Action

The 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 properties, namely: Name, which represents the bean's name, property, which represents the value of which attribute to extract. Here 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 is each:
<jsp:getproperty name= "Itembean" property= "UnitCost"
</UL>



13.6 Jsp:forward Action

The Jsp:forward action moves the request to another page. The Jsp:forward tag has only one property page. The page property contains a relative URL. The value of the page can be either given directly or dynamically computed at the time of the request, as shown in the following example:
<jsp:forward page= "/utils/errorreporter.jsp"/>
<jsp:forward page= "<%= somejavaexpression%>"/>



13.7 Jsp:plugin Action

The Jsp:plugin action is used to insert the object or EMBED element necessary to run Java applets through a Java plug-in, depending on the type of browser.

Appendix: JSP Annotations and character reference conventions

Here are some special tags or characters that you can use to insert comments or characters that may be considered to have special meaning. Grammatical use
<%--Comment--%> jsp annotations, also known as "hidden annotations." The JSP engine ignores it. All JSP script elements, directives, and actions within the tag will not work.
The!--comment--> html annotations, also known as "output annotations," appear directly in the resulting HTML document. All JSP script elements, instructions, and actions within the tag are performed correctly.
<\% in the template text (static HTML) where you actually want the "<%" to appear.
%\> in the script element where you actually want the "%>" to appear.
\ ' Use single quotes within the properties of single quotes. However, you can use either single or double quotes, while another one will have a common meaning.
\ "Use double quotes within the properties of double quotes." See the description of "\".
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.