What are the actions of the JSP?

Source: Internet
Author: User
Tags flush include integer modify relative return variable java web
Js

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>

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;
}
}

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.

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.

...


  • Number of items:
  • Cost is each:


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:



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.