JSP and JavaBeans

Source: Internet
Author: User
Tags expression range valid web services tomcat

1. Use of beans

In order to use Benas in the JSP page, we must use the JSP action tag: Usebean

Usebean format:

<jsp:usebean id= "class= The name of the bean to create beans class" scope= "Bean valid range" ></jsp:useBean>

When a JSP page containing a Usebean action tag is loaded on the server, the JSP engine first finds in a synchronized block, based on the ID name, whether the JSP engine built-in PageContent object contains the name ID and scope scope object, if the object exists, The JSP engine assigns an object like this to the customer, and if you do not find an object in PageContent that specifies the scope, name is ID, create a name that is an ID object based on the class specified by class, that is, create a beans with the name ID. and adds it to the PageContent built-in object and specifies that the scope of the beans is scope, and that the JSP engine assigns a scope to the customer, beans with the name ID.

The different scenarios for scope values in the Usebean label are described as follows:

Scope Take value page

The beans that the JSP engine assigns to each customer are not the same, that is, although each customer's beans function is the same, they occupy a different memory space. The valid range for this beans is the current page, and when the customer leaves this page, the JSP engine is beans to the customer.

Scope Value Session

The beans that the JSP engine assigns to each customer is not the same, and the beans is valid for the customer's session, that is, if the customer is connected to multiple pages, each page contains a Usebeans label with the same value as the ID in the Usebean label. and the value of scope is the session, then the customer gets the same beans on these pages. If the customer changes the Beans property on a page, the same beans properties of the other page will change. When the customer closes the browser, the JSP engine cancels the beans assigned to the customer.

Scope Value Request

The beans that the JSP engine assigns to each customer are not the same, and the beans valid range is the request period. Customers may request multiple pages during their visit to the site, and if these pages contain socope values that are Usebeans tags for request, then the beans that Pagecotent objects assign to customers on each page is also different. After the JSP engine responds to the request, it cancels the beans assigned to the customer.

Scope Value Application

The JSP engine assigns a shared beans to each customer, that is, all customers share the beans, and if a customer changes the value of one of the beans's properties, then all of the customer's beans attribute values have changed. The beans was canceled until the server was shut down.

Note: Ensure that the client supports cookies when the scope is a session beans.

In order for the JSP paging file in all of the server's Web services directory to use our beans, we must compile the above code-generated bytecode class file: Circle.class copy to the JSP engine classes folder, d:tomcat\ Jakarta-tomcat-4.0\classes, in addition, in JSP pages that use beans, you must have the following import directives:< @page import= "Circle" > If you want to get a beans Only the JSP pages in the examples directory are available, the bytecode files that create the beans need to be stored in the Webapps/example/web-inf/classes directory.

2. Get and modify the properties of beans

When we create a beans using the Usebean action tag, this beans in a Java program can invoke method generation behavior, such as modifying attributes. Get or modify beans properties you can also use the action label GetProperty, SetProperty

2.1getProperty Action Label

Use this tab to get the property value of beans and display this value as a string to the customer

GetProperty action Tag:

<jsp:getproperty name= "Beans's name" property= "Beans's Properties"/></jsp:getproperty>

Where a name value is the name of the beans that specifies the value of which beans property to get; The property value is the name of an attribute of the beans. This instruction is equivalent to using beans to invoke the GetXXX () method in a program slice.

2.2 SetProperty Action Label

Use this tab to set property values for beans.

The SetProperty action label can set the value of the Beans property in 3 ways.

(1) Set the value of the Benas property to the value or string of an expression.

This column more highlights: http://www.bianceng.cn/webkf/JSP/

The value of the Benas property is set to the value of an expression: <jsp:setproperty name= "Beans's name" property= "Beans's Properties" value= "<%=expression%>"/>

The value of the Benas property is set to a string: <jsp:setproperty name= "Beans's name" property= "Beans's Properties" value= string/>

(2) The 2nd way to set the Beans property value using SetProperty is:

Sets the value of the corresponding property of the beans by the value of the parameter of the HTTP form, requiring the form parameter name to be the same as the Beans property, and the JSP engine automatically converts the string to the type of the Beans property.

<jsp:setproperty name= "Beans's name" property= "*"/>

The label does not need to specify the value that the value of the Beans property will specify for which parameter in the form, and the system automatically matches the matching by name.

(3) The 3rd way to set the Beans property value using SetProperty is:

Set the value of the corresponding property of the beans by the value of the parameter of the request, requiring that the name of the request parameter must be the same as the name of the Beans property, and the JSP engine will automatically retrieve the

The string data type is converted to the type of the corresponding property of the beans.

<jsp:setproperty name= "Beans name" property= "attribute name" param= "parameter name"/>

Note: It is necessary to note that the directive tag is not executed until the form that corresponds to the beans is submitted.

Note: value and param cannot be used simultaneously in <jsp:setProperty>.

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.