JSP with JavaBean:

Source: Internet
Author: User
Js
While you can put large chunks of code in a script fragment (scriptlet), most Java code is a reusable component called Javabea. JavaBean are similar to ActiveX controls: they all provide common functionality and can be reused.

JavaBean values are obtained by using properties that you can use to access JavaBean settings. To use a person as an analogy, this person is a javabean, the person's name, ID number and address is its attribute. In the JSP website, you basically is by playing "Connect the beans" to make your website dynamic.

Assuming that JavaBean is created before the site, the first thing you need to do is tell the JSP page that it will use a "Bean". You can do this with the "<jsp:useBean>" tag:
<jsp:usebean id= "LocalName" class= "Com.jguru.Person" scope= "Application"/>.

The "<jsp:useBean>" tag requires you to identify the bean using the "id" attribute. Here you provide a name for the rest of the JSP page of the Bean. In addition to the ID attribute, you must tell the page where to look for the bean, or what its Java class alias is. This category property provides the ability to confirm the bean, and some other methods can do the same. The last required element is the scope attribute. With the help of the scope attribute, you can tell the bean to make a single page (by default) [scope= ' page], a request for [scope= requests] as a session [scope=], or an entire application [scope= ' Application "] Keep the message. With a session range, you can easily maintain items such as shopping carts on JSP Web pages.

Once you have a javabean, you can access its properties to customize it. To get the property value, use the "<jsp:getProperty>" tag. With this tag, you can specify the name of the bean to use (from the Usebean "id" field) and the attribute you want to get its value. The actual values are placed in the output:
<jsp:getproperty id= "LocalName" property= "name"/>.

To change the JavaBean property, you must use the "<jsp:setProperty>" tag. For this tag, you need to recognize the bean and attributes again to modify and provide additional new values. If the name is correct, these values can be obtained directly from a submitted table: parameters are obtained:
<jsp:setproperty id= "LocalName" property= "*"/>;

Can be obtained from one parameter, but you must directly name the attributes and parameters:
<jsp:setproperty id= "LocalName" property= "SerialNumber" value= "string"/>

or directly by a name and value to set: <jsp:setproperty id= "LocalName" property= "serialnumber" value= <%= expression%>/>.

The last thing about JavaBean: for a Web server to find JavaBean, you need to place its category files in a special location. With JSWDK, the most convenient location is the category directory under the installation directory, such as \jswdk-1.0.1\classes.




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.