Learn to use JavaBeans in JSP

Source: Internet
Author: User
Tags define
Js


Although you can add a lot of code blocks to Java code (scriptlets), the fact is that most Java codes should be used in reusable components called JavaBeans. JavaBeans are like ActiveX controls: They provide the familiar features of functionality and reuse.


JavaBean values are available through the definition of a set of properties that provide access to JavaBean settings. To use a person as a javabean, the person's name, social security number, and address are his attributes. In the JSP site, you are mainly through the implementation of connection beans to dynamically access your site.

Assuming that JavaBeans has been constructed before the site is created, you first have to let your JSP page know that you want to use a bean. This step is done by the <jsp:useBean> tag:

<jsp:usebean id= "LocalName" class= "Com.jguru.Person" scope= "Application"/>.

<jsp:useBean> tags require you to use the attributes of the ID to determine the bean. Here you need to provide a name in the static JSP page to identify the bean. In addition, you must tell this page where to find the name of the Bean or Java class, and the attributes of the class provide this information in a few other places. One of the last necessary elements is the scope property. With its help, you can use beans for a page (the default) [scope= "page"], request [scope= "request"], lifetime [scope= "session"], or the entire application [scope=] Application "] Provides information that, by using session scope, you can add other items, such as shopping cards, to your JSP pages.

Once you have defined a JavaBean, you have the right to use its properties to define it. To get an appropriate value, use the <jsp:getProperty> tag, which allows you to define a bean in detail. Its actual metrics will be placed in the output:

<jsp:getproperty id= "LocalName" property= "name"/>.

If you want to change the properties of JavaBean, you need to use the <jsp:setProperty> tag, which allows you to redefine your bean and properties and provide new values. If you want to change the named property, these can be obtained directly from the following form:

<jsp:setproperty id= "LocalName" property= "*"/>;

You can name the properties and parameters directly from the following form:

<jsp:setproperty id= "LocalName" property= "Address" param= "parametername"/>;

or set the name and value directly:

<jsp:setproperty id= "LocalName" property= "SerialNumber" value= "string"/> or <jsp:setproperty id= "LocalName" Property= "serialnumber" value= <%= expression%>/>.

The final step is: to get the Web server to find JavaBeans, you need to place their class files in a special location. The most obvious place to use JSWDK is to create a class 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.