Apply JavaBean on the JSP page

Source: Internet
Author: User
Tags stock prices

JavaBean is a Java-based software component. JSP provides comprehensive support for the integration of JavaBean components in Web applications. This kind of support not only shortens the development time, but can directly use tested and trusted existing components to avoid repeated development), and also brings more scalability to JSP applications.

The JavaBean Component can be used to execute complex computing tasks, interact with databases, and extract data. If we have three JavaBean that display news, stock prices, and weather conditions, you can create a Web page that contains all these three functions to instantiate the three beans, you can use HTML tables to locate them in sequence.

To illustrate the application of JavaBean in the JSP environment, we created a Bean named TaxRate. It has two attributes: Product) and Rate ). The two set methods are used to set these two attributes respectively, and the two get methods are used to extract these two attributes. In practical applications, such beans should generally extract tax rates from the database. Here we simplify this process and allow any tax rate setting. The following is the code list of this Bean:

 
 
  1. package tax;   
  2.   public class TaxRate {   
  3.   String Product;   
  4.   double Rate;   
  5.   public TaxRate() {   
  6.   this.Product = "A001";   
  7.   this.Rate = 5;   
  8.   }   
  9.   public void setProduct (String ProductName) {   
  10.   this.Product = ProductName;   
  11.   }   
  12.   public String getProduct() {   
  13.   return (this.Product);   
  14.   }   
  15.   public void setRate (double rateValue) {   
  16.   this.Rate = rateValue;   
  17.   }   
  18.   public double getRate () { 
  19.   return (this.Rate); 
  20.   } 
  21.   } 

The <JSP: useBean> tag is used to apply the preceding Bean On the jsp page. Depending on the specific JSP engine used, where to configure and how to configure Bean may be slightly different. In this article, we put the. class file of this Bean under the c:. 0INF directory. The tax here is a directory dedicated to storing this Bean. The following is an example page that applies the above Bean:

 
 
  1. < HTML>
  2. < BODY>
  3. <% @ PageLanguage="Java"%>
  4. < Jsp: useBean Id="Taxbean" Scope="Application" Class="Tax. TaxRate" />
  5. <% Taxbean. setProduct ("A002 ");
  6. Taxbean. setRate (17 );
  7. %>
  8. Method 1:< P>
  9. Product:<% = Taxbean. getProduct () %> < Br>
  10. Tax Rate:<% = Taxbean. getRate () %>
  11.  < P>
  12.  <% Taxbean. setProduct ("A003 ");
  13. Taxbean. setRate (3 );
  14. %>
  15.  < B>Method 2:</B> < P>
  16. Product:< Jsp: getProperty Name="Taxbean" Property="Product" />
  17.  < Br>
  18. Tax Rate:< Jsp: getProperty Name="Taxbean" Property="Rate" />
  19.  </BODY>
  20.  </HTML> 

Several attributes are defined in the <jsp: useBean> tag. The id is the id of the Bean on the entire JSP page, and the scope attribute defines the survival time of the Bean, the class attribute indicates that the class file of the Bean starts from the package name ).

This JSP page uses not only the set and get methods of Bean to set and extract attribute values, but also the second method to extract Bean attribute values, that is, the <jsp: getProperty> tag. The name attribute in <jsp: getProperty> is the id of the Bean defined in <jsp: useBean>. Its property Attribute specifies the name of the target property.

It turns out that Java Servlet is an ideal framework for developing Web applications. JSP is based on Servlet technology and has been improved in many aspects. The JSP page looks like a common HTML page, but it allows embedding and executing code. At this point, it is very similar to ASP technology. Using the cross-platform running JavaBean Component, JSP provides an excellent solution for separating processing logic and display styles. JSP will surely become a powerful competitor of ASP technology.

  1. Session-based online user statistical analysis in JSP
  2. Experience in Servlet and JSP Performance Optimization
  3. JSP tag library Parsing
  4. Import event-driven technology in JSP Servlet Development
  5. Six common issues for JSP Programming

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.