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);
}
}
The tag is used to apply the above Bean to the JSP page. Depending on the specific JSP engine used, there may be a slightly different way of configuring and configuring the Bean. This article places the. class file of this bean in the C:jswdk-1.0examplesweb-infjspeans ax directory, where tax is a directory dedicated to storing the bean. 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>
Within the tag, several properties are defined, 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 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, which is to use the tag. The Name property in is the ID of the bean defined in , 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.