Javabean is a Java-based software component. JSP provides comprehensive support for the integration of JavaBean components in Web applications. This support not only shortens the development time (you can directly use tested and trusted existing components to avoid repeated development), but 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:
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 <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:
<HTML>
<Body>
<% @ Page Language = "Java" %>
<JSP: usebean id = "taxbean" Scope = "application" class = "tax. taxrate"/>
<% Taxbean. setproduct ("a002 ");
Taxbean. setrate (17 );
%>
Method 1: <p>
Product: <% = taxbean. getproduct () %> <br>
Tax Rate: <% = taxbean. getrate () %>
<P>
<% Taxbean. setproduct ("a003 ");
Taxbean. setrate (3 );
%>
<B> Method 2: </B> <p>
Product: <JSP: getproperty name = "taxbean" property = "product"/>
<Br>
Tax Rate: <JSP: getproperty name = "taxbean" property = "rate"/>
</Body>
</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 specifies the class file of the bean (starting 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