JSP and JavaBeans

Source: Internet
Author: User

JSP and JavaBeans
1. Use beans
To use benas on a JSP page, we must use the JSP action tag useBean.
UseBean format:

When a JSP page containing useBean action tags on the server is loaded and executed, the JSP Engine first searches for the built-in JSP engine in a synchronization Block Based on the id name.
Whether the pageContent object contains an object with the name id and scope. If this object exists, the JSP Engine assigns such an object to the customer.
In pageContent, no objects with the specified scope and id are found. Create an object named id based on the class specified by the class, that is, create a beans named id,
Add it to the built-in pageContent object and specify the scope of the beans as scope. Meanwhile, the JSP Engine assigns the customer a beans with the scope and name as id.
The differences between scope values in useBean labels are described as follows:
Scope value page
The beans allocated by the JSP Engine to each customer are different. That is to say, although each customer has the same function, they occupy different memory space. This beans
The valid range is the current page. When the customer leaves the page, the JSP Engine will cancel the beans allocated to the customer.
Scope value session
The beans allocated by the JSP Engine to each customer are different. The valid range of the beans is the client's session period. That is to say, if the customer connects to each other on multiple pages,
Each page contains a useBeans tag. These useBean tags have the same id value and the scope value is session.
Beans are the same. If the customer changes the attribute of the bean on a page, the attribute of the bean on other pages will also change. When the customer closes browsing
The JSP Engine cancels the beans allocated to the customer.
Scope value request
The beans allocated by the JSP Engine to each customer are different. The valid range of the beans is the request period. Customers may request too many pages during Website access,
If these pages contain the useBeans tag where the sobeans value is the request, the pageCotent object also has different beans assigned to the customer on each page. JSP
After the engine responds to the request, cancel the beans allocated to the customer.
Scope value application
The JSP Engine allocates a shared beans to each customer. That is to say, all customers share the beans. If a customer changes the value of a certain attribute of the beans
The attribute values of beans of all customers have changed. This beans is not canceled until the server is shut down.
Note: When session beans are used, ensure that the client supports cookies.
To make the JSP page files in all the web service directories of the server use our beans, We must compile the bytecode file generated above:
Circle. class copy to the JSP Engine classes folder, that is, D: tomcat \ Jakarta-tomcat-4.0 \ classes, in addition, in the JSP page using beans,
The following import commands must be available: <@ page import = "Circle">
To make a beans available only to the JSP page under the examples directory, the bytecode file for creating the beans must be stored in the webapps/example/Web-inf/classes directory,
2. Get and modify attributes of beans
After we create a beans using the useBean action tag, the beans in the java program can call methods to generate behaviors, such as modifying attributes. Get or modify the properties of beans and
You can use the action tags getProperty and setProperty.
2.1 getProperty action tag
You can use this label to obtain the attribute value of beans and display the value as a string to the customer.
GetProperty action Tag:

The value of name is the name of the bean, which is used to specify the value of the attribute of which the bean is to be obtained; the value of property is the name of an attribute of the bean. The operation of this command
Call the getXxx () method using beans in the program.
2.2 setProperty action tag
You can use this label to set the attribute values of beans.
The setProperty action tag can be used to set the value of the beans attribute in three ways.
(1) set the value of the benas attribute to the value or string of an expression.
Set the value of the benas attribute to the value of an expression: "/>
The value of the benas attribute is set to a string:
(2) Use setProperty to set the attribute values of beans in 2nd ways:
You can set the value of the corresponding attribute of beans through the value of the HTTP form parameter. The name of the form parameter must be the same as that of the beans attribute, the JSP Engine automatically converts a string to the type of the beans attribute.

The tag does not need to specify the value of the beans attribute to match the value specified by the parameter in the form. The system automatically matches the value according to the name.
(3) Use setProperty to set the attribute values of beans in the following 3rd ways:
The value of the request parameter is used to set the value of the corresponding attribute of beans. The request parameter name must be the same as that of the beans attribute. the JSP Engine automatically obtains
Convert the string data type to the type of the corresponding attribute of beans.

Note: The command label is executed only after the form corresponding to the bean is submitted.
Note: Use both value and param.

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.