Application in JSP page JavaBean

Source: Internet
Author: User
Tags html page return string stock prices

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" class="tax.TaxRate" />
  < % taxbean.setProduct("A002");
  taxbean.setRate(17);
  %>
 使用方法 1 : < p>
 产品 : < %= taxbean.getProduct() %> < br>
 税率 : < %= taxbean.getRate() %>
 < p>
 < % taxbean.setProduct("A003");
 taxbean.setRate(3);
 %>
 < b> 使用方法 2 : < /b> < p>
 产品 : < jsp:getProperty name="taxbean" property="Product" />
 < br>
 税率 : < jsp:getProperty name="taxbean" property="Rate" />
 < /BODY>
 < /HTML>

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.