(v) JSP and JavaBean

Source: Internet
Author: User

1. A standard JavaBean should have the following characteristics:

(1) must be a public class, that is, the class access permission for JavaBean must be publicly.

(2) must have a parameterless construction method. If you define a custom, parameterized construction method in JavaBean, you must add a parameterless construction method, otherwise you will not be able to set the property, or you can use the compiler to automatically add an parameterless construction method if you do not define a custom, parametric construction method.

(3) JavaBean generally set properties to private, using the GetXXX () method and the Setxxx () method to obtain and set properties.

The syntax format is as follows:

<jsp:usebean id= "Object name" scope= "Storage Scope" class= "package name + class name" ></jsp:useBean>

Note: (1) The id attribute represents the name of the object after the JavaBean instantiation.

(2) The scope attribute is used to specify the range of the JavaBean. There are four range values: page, request, session, application

(3) The class attribute is used to specify the name of the JavaBean, to write the full-package name and the class name.

2. Setting the JavaBean Property

JSP provides a <jsp:setProperty> action instruction to set the JavaBean property, which has the following four syntax formats:

(1) <jsp:setproperty name= "instantiating object name" property= "*"/>

(2) <jsp:serproperty name= "Instantiate object Name" property= "property name"/>

(3) <jsp:setproperty name= "Instantiate object Name" property= "property name" param= "Accept parameter name"/>

(4) <jsp:setproperty name= "instantiating the object name" property= "property name" Value= "property value"/>

Note: Where the Name property is set to instantiate the object name, and the id attribute in <jsp:useBean> is to be consistent.

3. Get JavaBean Properties

The syntax format is as follows:

<jsp:getproperty name= "Instantiating the object name" property= "property name"/>

4. Removal of JavaBean

JavaBean will determine its life cycle based on its set range, and JavaBean will be removed automatically when the lifecycle ends. It can also be manually removed, saving memory.

JavaBean removal is different for different ranges of JavaBean, calling PageContext, request, Session, application RemoveAttribute (String name), respectively method to remove the JavaBean. Where the Name property is set to instantiate the object name and must be consistent with the id attribute in <jsp:useBean>.

(v) JSP and JavaBean

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.