JSP with JavaBean

Source: Internet
Author: User
Author: Tianma
Although you can place a large segment of code in a script segment (scriptlet), the vast majority of Java code is a reusable component named JavaBea. JavaBean is similar to ActiveX controls: they provide common functions and can be reused.
The value of JavaBean is obtained through some attributes. You can access the JavaBean settings through these attributes. For example, a person is a JavaBean, whose name, ID number, and address are attributes. On the JSP website, you basically make your website dynamic by playing "Connect the beans.
Suppose that the JavaBean is created before the website. The first thing you need to do is to tell the JSP page that it will use a "Bean ". You can use the <jsp: useBean> Mark to do this:
<Jsp: useBean id = "localName" class = "com. jguru. Person" scope = "application"/>.
The "<jsp: useBean>" tag requires that you use the "id" attribute to identify the Bean. Here you provide a Bean named for the rest of the JSP page. In addition to the "id" attribute, you must also tell the webpage where to find the Bean or its Java class name. This type attribute provides the Bean Validation function. Other methods can also do this. The last required element is the "scope" attribute. With the help of the "scope" attribute, you can tell the Bean to be a single page (default) [scope = "page"], [scope = "request"] The request is a session [scope = "session"], or leave information for the entire application [scope = "application. With the session range, you can easily maintain items such as shopping carts on the JSP page.
Once you become a JavaBean, you can access its attributes to customize it. To obtain the property value, mark it with <jsp: getProperty>. With this tag, you can specify the name of the Bean to be used (obtained from the "id" field of useBean) and the attribute of the Bean value you want. The actual value is put in the output:
<Jsp: getProperty id = "localName" property = "name"/>.
To change the JavaBean attribute, you must use the <jsp: setProperty> tag. For this tag, you need to identify the Bean and attributes again to modify and provide additional values. If the name is correct, these values can be obtained directly from a submitted table: parameter:
<Jsp: setProperty id = "localName" property = "*"/>;
It can be obtained from a parameter, but you must directly name the attributes and parameters:
<Jsp: setProperty id = "localName" property = "serialNumber" value = "string"/>
You can also directly set the value using a name and value: <jsp: setProperty id = "localName" property = "serialNumber" value = <% = expression %>/>.
The last thing about JavaBean: In order for the Web server to find JavaBean, you need to place its category files in a special location. With JSWDK, the most convenient location is the class directory under the installation directory, such as the 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.