JavaBean instance-Application

Source: Internet
Author: User

The code for test. jsp is as follows:
<HTML>
<Head>
<Title> test using Java Bean in JSP </title>
</Head>
<Body>
<JSP: usebean id = "test" class = "simlebean"/>
<JSP: setproperty name = "test" property = "message" value = "Hello jsp"/>
<P> message:
<JSP: getproperty name = "test" property = "message"/>
</Body>
</Html>

Explanation:

<JSP: usebean id = "test" class = "simlebean"/>: defines a Java Bean instance. The instance name is test and the class name is simlebean.
<JSP: setproperty name = "test" property = "message" value = "Hello jsp"/>: sets the message of the property of the test instance. The value is "Hello jsp ".
<JSP: getproperty name = "test" property = "message"/>: displays the value of the message property of the test instance on the page.

· The Java Bean code simplebean. java used in the test. jsp file is as follows:
Package test;

// Note here. It seems that there must be a package name, no package I have tried not, simplebean. class together with the parent folder in the WEB-INF/classes/below

Public class simplebean
{
Private string message;

Public String getmessage ()
{
Return message;
}

Public void setmessage (string message)
{
This. Message = message;
}
}

 

1. <JSP: usebean>

Javabean is a special type of Java class. Compared with common Java classes, it has two special methods: Set... (Method for setting attributes), get... (Method for retrieving attribute values ).
<JSP: usebean> the action is used to find or instantiate the JavaBeans component used by a JSP page.
In a program, logical control and database operations can be placed in the JavaBeans component, and then called in the JSP file. This increases the definition of the program and the reusability of the program.

· Syntax:

<JSP: usebean id = "beaninstancename"
Scope = "Page | request | session | application"
Class = "package. claaa"
Type = "package. claaa"
Other elements
/>

· Attributes:

1) id = "beaninstancename"
The variable used to bind the bean instance.
2) Class | type = "package. claaa"
Specifies the class to which the bean is instantiated.
3) Scope = "Page | request | session | application"
The valid range of the bean.
Page: the effective range of the instance is the current page.
Request: The validity period of an instance is within the lifecycle of a single customer request.
Session: the effective range of the instance is within the entire user session lifecycle.
Application: the effective range of an instance is within the life cycle of the application.

2. <JSP: setproperty>

This action is used to set the values of one or more attributes using the set () method in bean. The source of the values is clearly given through the value attribute, or use the corresponding parameters in the request object.
Assume that a bean has a string-type property myproperty that can be used by JSP files, it must have a public method setmyproperty (string value), <JSP: setproperty> the Action uses this method.

· Syntax:

<JSP: setproperty name = "beaninstancename"
Property = "*" | property = "propertyname"
Value = "specific value"
/>

· Attributes:

1) name = "beaninstancename"
This attribute is required to indicate which bean instance to perform the following action. The value must correspond to the ID defined in the Action <JSP: usebean>, including the case sensitivity.
2) Property = "*" | property = "propertyname"
This attribute is required to indicate which attribute to set. If the property value is "*", it indicates all the values entered on the visible JSP page, which are stored in the matching bean property. The matching method is: the bean property name must be the same as the name in the input box.
3) value = "specific value"
It is used to specify the bean attribute value.

3. <JSP: getproperty>

This action extracts the value of the specified bean attribute, converts it to a string, and then outputs it. This action actually calls the get () method of bean.
As mentioned above, a bean has a string-type property myproperty that can be used by JSP files. It must have a public method getmyproperty () with the return type of string (). <JSP: getproperty> uses this method.

Syntax:

<JSP: getproperty name = "beaninstancename" property = "propertyname"/>

· Attributes:

1) name = "beaninstancename"
This attribute is required to indicate which bean instance to perform the following action. The value must correspond to the ID defined in the Action <JSP: usebean>, including the case sensitivity.
2) Property = "*" | property = "propertyname"
This attribute is required to indicate the attribute to be obtained.

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.