Learning to use JavaBeans in JSP

Source: Internet
Author: User

Although you can add a large number of code blocks to the Java program code (scriptlets), in fact, most Java code should be used in reusable components called JavaBeans. Like Activex controls, JavaBeans provide well-known functionality and reuse features.


The values of JavaBean are available through the definition of a set of attributes, which provide the settings for accessing JavaBean. For example, if a person is treated as a JavaBean, his/her name, social insurance number and address are his/her attributes. In jsp (the preferred site for SUN Enterprise applications), you primarily access your site dynamically by executing connection beans.

Assuming that the JavaBeans have been constructed before creating the site, you must first let your jsp (preferred for SUN Enterprise Applications) page know that you want to use a bean. This step is completed by the <jsp (preferred for SUN Enterprise Applications): useBean> tag:

<Jsp (preferred for SUN Enterprise Applications): useBean id = "localName" class = "com. jguru. Person" scope = "application"/>.

<Jsp (preferred for SUN Enterprise Applications): useBean> the tag requires you to use the id attribute to determine the bean. Here you need to provide a name to identify the bean on the static jsp (preferred for SUN Enterprise Applications) page. In addition, you must tell the page where to find the bean or Java class name. The class attribute provides this information in a few other places. The last necessary element is the scope attribute. With its help, you can use beans to request [scope = "request"] for a page (the default) [scope = "page"], life cycle [scope = "session"]; or the entire application [scope = "application"] provides information. By using session scope, you can select the preferred type for SUN Enterprise Applications in jsp) add other projects, such as shopping cards, to the page.

Once you define a JavaBean, you have the right to use its attributes to define it. To obtain an appropriate value, you must use the <jsp (preferred for SUN Enterprise Applications): getProperty> tag. You can define a bean in detail using this tag. Its actual indicators will be placed in the output:

<Jsp (preferred for SUN Enterprise Applications): getProperty id = "localName" property = "name"/>.

To change the attributes of JavaBean, you need to use the <jsp (preferred for SUN Enterprise Applications): setProperty> tag, with this tag, you can redefine your bean and attributes and provide new values. If you want to change the naming attributes, these can be obtained directly from the following form:

<Jsp (preferred for SUN Enterprise Applications): setProperty id = "localName" property = "*"/>;

You can directly name attributes and parameters in the following format:

<Jsp (preferred for SUN Enterprise Applications): setProperty id = "localName" property = "address" param = "parameterName"/>;

Or directly set the name and value:

<Jsp (preferred for SUN Enterprise Applications): setProperty id = "localName" property = "serialNumber" value = "string"/> or <jsp (preferred for SUN Enterprise Applications ): setProperty id = "localName" property = "serialNumber" value = <% = expression %>/>.

The last step is: to enable the Web server to find JavaBeans, You need to place their class files in a special place. Using JSWDK, the most explicit place is to create a class directory under the installation directory, such as jswdk-1.0.1classes.

 

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.