Use of the actions of the JSP introductory beginner tutorial

Source: Internet
Author: User
Tags object expression flush include integer string variable java web
js| Tutorial JSP Actions use you can dynamically insert a file, reuse the JavaBeans component, advance to another page, or generate an HTML for the Java plug-in. The action you can use is:

(1) Jsp:include--Included in a file when the page is requested.

(2) jsp:usebean--Find or instantiate a JavaBean.

(3) jsp:setproperty--sets a JavaBean property.

(4) jsp:getproperty--inserts the JavaBean attribute into the output.

(5) jsp:forward--allows the requester to move forward to a new page.

(6) jsp:plugin--uses the object or embed tag to generate a specific browser code for the Java plugins.

1. Jsp:include Action

This action allows you to include some files on the page you are about to generate:


  
Unlike include directive, this action is to include a file when the page is requested, and include directive contains files when the JSP page is converted to a servlet. In order to improve efficiency, include action makes a small sacrifice, that is, it does not allow the included pages contain generic JSP code (for example, cannot set HTTP headers), but it has significant flexibility, such as the following JSP code, it implements four different fragments into the following page. Each time the title changes, you only need to modify the four files without changing the main JSP page.

whatsnew.jsp



JSP Tutorials


What "s New at Chinese comic sites



We are a summary of our four most recent news stories:








Of course you can define yourself? ml file, but one thing to note:

You should put the file in the news directory under your JSP directory.

Use of Jsp:usebean Action

First, the grammar:

Id= "Beaninstancename"
Scope= "Page|request|session|application"
{class= "Package.class" |
Type= "Package.class" |
class= "Package.class" type= "Package.class" |
Beanname= "{package.class | <%= expression%>}" Type= "Package.class"
}
{/> |
> other elements

}

This action enables you to load a javabean into a JSP page. This is a very useful capability because it allows you to use reusable Java classes without sacrificing performance. The simplest syntax is to specify a bean:

This usually means "instantiating an object of a class by specifying a class and binding it to a variable with a name specified by ID." However, as we have seen, you can specify a scope property to make the bean not only associated with the current page. In this case, it is useful to get a reference to an existing bean, and to create a new one only if there are no identical IDs and scope beans. Now that you have a bean, you can modify it by Jsp:setproperty, or use the Scriptlet or explicit invocation method by using a name that was previously specified with an ID. When you say "This bean has a property of type X called foo", what you really mean is "this class has a method called Getfoo that returns a class value of type X, and another method called Setfoo, which takes X as an argument." "This jsp:setproperty action will be described in detail in the next unit, but now you can either give a definite value, give a property that the value is inherited from the request's arguments, or simply list the property to flag that the value should inherit from a parameter with the same name as the property name. You can get an existing JSP expression or Scriptlet property by calling the applicable GetXXX method, or more generally, using the Jsp:getproperty action.

Note that the class specified for the bean must be under the class path of the server's rule, not the path of the class that is being automatically loaded when the change occurs. For example, on a Java WEB server, it and the classes it uses must be in the class's directory or within a jar file in the Lib directory, not in the Servlets directory.

Let's look at a very simple example that loads a bean and sets/gets a simple string parameter.

beantest.jsp


reusing JavaBeans in JSP





Reusing JavaBeans in JSP




property= "Message"
Value= "Hello WWW"/>
Message:


Simplebean.java

The following is the original code for the Bean:

Package Hall;
public class Simplebean {
Private String message = ' No message specified ';
Public String GetMessage () {
return (message);
}
public void Setmessage (String message) {
this.message = message;
}
}

The results of the operation are: page output: Reusing JavaBeans in JSP

Message:hello WWW

Second, the detailed usage of Jsp:usebean

The easiest way to use beans is to:


In order to load the bean, you need to modify and retrieve the Bean's properties with Jsp:setproperty and Jsp:getproperty. And, there are two other options. First, you can use the format of the container, which is:


Body

To point out, the body section should be executed only when the bean is first instantiated, not every time it is found and used. Beans can be shared, so not all jsp:usebean statements produce an instance of a new bean. Second, in addition to the ID or class, there are three properties you can use: Scope,type, and Beanname. These attributes are summarized as follows:

Property Meaning
Id Give a variable a name, and this variable will point to the bean. If a bean with the same ID and scope is found, it is used without creating a new one.
Class Indicates the full package name of the Bean.
Scope Indicates the relationship that the bean can be used on top. There are four possible values: Page,request,session, and application. The default is page, which indicates that the bean is available only on the current page (saved in the current PageContext). A value for request indicates that the bean is used only for the current client's request (saved in the ServletRequest object). The value of the session indicates that the object is available to all pages during the current HttpSession lifecycle. Finally, the application value indicates that the object is available for all shared servletscontext pages. Use Jsp:usebean to create a new bean only if there are no identical IDs and scope beans, if they are already used, and ignore the code that starts and ends with the Jsp:usebean flag.
Type Indicates the type of the variable that will point to the object. This must match the class name or a superclass or an interface to the implementation class. Remember, the name of the variable is specified by the id attribute.
Beanname Give the bean a name, which you should provide in the beans instantiation method. It allows you to give a type and a beanname and omit the class attribute.

Third, Jsp:setproperty Action

Grammar:

Name= "Beaninstancename"
{
property= "*" | property= "PropertyName" [param= "ParameterName"] | Property= "PropertyName" value= "{string | <%= expression%>}"
}
/>

Before we knew it. You can use Jsp:setproperty to assign a value to a bean's properties. You can implement it in two ways. The first is to use the Jsp:setproperty after Jsp:usebean (not within):


...
property= "Someproperty" .../>

In this way, Jsp:setproperty will be executed regardless of whether there is already a bean with the same ID and scope. Alternatively, the jsp:setproperty appears within the Jsp:usebean element, such as:


...

In this case, Jsp:setproperty executes only if a new object is instantiated.

The following are the available properties for four jsp:setproperty:

Property Usage
Name This is a required attribute. It indicates which Bean's properties will be set. The Jsp:usebean must appear before jsp:setproperty.
Property This is a required attribute. Indicates which property you will set. However, there is a special case: if the value "*" means that all the request parameters that match the name to the Bean's properties are passed to the appropriate property-setting method.
Value This is an optional attribute. that specifies the value of the property being set. The value of the string is automatically converted to Numbers,boolean,boolean,byte,byte,char, and character by the corresponding object or the standard valueof method of the package. For example, the value "true" for a Boolean or Boolean property is transformed by the Boolean.valueof method, and the value "42" of an int or an integer property is transformed by Integer.valueof. You cannot use the value and Param properties at the same time, but none of the two is allowed.
Param This is an optional attribute. It indicates the parameters of the request that the bean's properties should inherit. If the current request does not have such an argument, nothing is done: the system does not pass NULL to the method that sets the property. Therefore, you can use the bean's default value. For example, the following program executes "set the Numberofitems property to the value of any NumItems request parameter, and if there is such a request parameter, do nothing." ”

If you default both value and Param, this is the same as the property name where you set the Param name to the bean. You can set the property of the request to use the property of the bean by setting the value of name to ' * ' and omitting the value and param from the application. In this case, the server will repeatedly find the available properties and request parameters to match the same name.

Iv. Jsp:getproperty Action

Grammar:

 
This property retrieves the value of the Bean's property and converts it to a string, which is then inserted into the output. It has two required attributes: Name, which was previously introduced with Jsp:usebean, property, and must be inserted into the attribute of the value.



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.