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:jswdk-1.0examplesweb-infjspeansax 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 class file of the bean (starting with the package name).
This JSP page not only uses the bean's set and get methods to set and extract the property values, but also uses the second method of extracting the Bean property values, that is, using the jsp:getproperty> tag. The Name property in jsp:getproperty> is the ID of the Bean defined in jsp:usebean>, whose 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.