Tips for using: applying JavaBean to JSP pages

Source: Internet
Author: User
Tags html page return stock prices
js| Tips | page

JavaBean is a Java-based software component. JSP provides complete support for integrating JavaBean components in WEB applications. This support not only shortens development time (can directly leverage tested and trusted existing components, avoids duplication of development), but also provides more scalability for JSP applications.

The JavaBean component can be used to perform complex computational tasks, or to interact with the database and to extract data. If we have three JavaBean, each with a feature that displays news, stock prices, and weather conditions, then creating a Web page that contains all three features requires only instantiating the three beans and using HTML tables to locate them in turn.

To illustrate the application of JavaBean in the JSP environment, we created a Bean named TaxRate. It has two attributes, namely product (products) and Rate (tax rate). The two set methods are used to set these two properties, and two get methods are used to extract the two properties. In practice, this Bean should generally extract the tax value from the database, where we simplified the process and allowed arbitrary tax rates. Here is the code listing for this Bean:

       
        
         
        Package tax;   public class TaxRate {String Product;   Double Rate; Public TaxRate () {this.   Product = "A001"; This.   Rate = 5; } public void Setproduct (String ProductName) {this.   Product = ProductName; Public String getproduct () {return (this.   PRODUCT); public void Setrate (double ratevalue) {this.   Rate = RateValue; Public double getrate () {return (this.   Rate); }   }
       
        

Applying the above Bean to the JSP page uses the < jsp:usebean> tag. Depending on the specific JSP engine used, there may be a slightly different way of configuring and configuring the Bean. This article puts the. class file of this bean in the C:.0inf directory, where tax is a directory that stores the bean specifically. The following is a sample page that applies the above Bean:

       
        
         
        < html> < body> <%@ page language= "java"%> < Jsp:usebean id= "Taxbean" scope= "Application" CLA ss= "tax.   TaxRate "/> <% taxbean.setproduct (" A002 ");   Taxbean.setrate (17); %> Use Method 1: < P> products: <%= taxbean.getproduct ()%> < br> tax rate: <%= taxbean.getrate ()%> ;  P> <% taxbean.setproduct ("A003");  Taxbean.setrate (3);  %> < B> use Method 2: </b> < P> products: < Jsp:getproperty name= "Taxbean" property= "Product"/> < Br> Tax rate: < Jsp:getproperty name= "Taxbean" property= "Rate"/> </body> 

Several properties are defined within the < Jsp:usebean > tag, where the ID is the identity of the bean within the entire JSP page, and the scope property defines the lifetime of the Bean, which describes the Bean's class file (starting with the package name).

This JSP page uses not only the bean's set and get methods to set and extract the property values, but also the second way to extract the value of the Bean property, using the < Jsp:getproperty > tag. The Name property in < Jsp:getproperty > is the ID of the Bean defined in < Jsp:usebean >, and its property attribute specifies the name of the target attribute.

As it turns out, the Java Servlet is an ideal framework for developing WEB applications. JSP is based on Servlet technology and has been improved in many ways. The JSP page looks like a normal HTML page, but it lets you embed execution code, which is very similar to the ASP technology. Using JavaBean components that run across platforms, JSP provides an excellent solution for separating processing logic and display styles. JSP will become a strong competitor of ASP technology.



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.