JSP Tutorial (v) The use of-jsp actions

Source: Internet
Author: User
Tags object expression integer modify string variable java web
js| Tutorial


Use of Jsp:usebean Action

First, the grammar:
<jsp:usebean
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
</jsp:useBean>
}

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:

<jsp:usebean id= "name" class= "Package.class"/>

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
<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"/>
<jsp:getproperty name= "test" property= "message"/>
</I></H1>
</BODY>
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

B>message:hello WWW

Second, the detailed usage of Jsp:usebean

The easiest way to use beans is to:

<jsp:usebean id= "name" class= "Package.class"/>
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:

<jsp:usebean ...>
Body
</jsp:useBean>

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

Usage

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:

<jsp:setproperty
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):

<jsp:usebean id= "myname" .../>
...
<jsp:setproperty name= "MyName"
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:

<jsp:usebean id= "MyName" ... >
...
<jsp:setproperty name= "MyName"
property= "Someproperty" .../>
</jsp:useBean>

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." ”

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

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:

<jsp:getproperty name= "Beaninstancename" property= "PropertyName"/>

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.