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:
- 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>
- <% @ PageLanguage="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 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.
- Session-based online user statistical analysis in JSP
- Experience in Servlet and JSP Performance Optimization
- JSP tag library Parsing
- Import event-driven technology in JSP Servlet Development
- Six common issues for JSP Programming