JAVABean components in JSP

xiaoxiao2021-03-06  103

JavaBean is a Java-based software component. JSP provides perfect support for integrating Javabean components in web applications. This support not only reduces development time (can directly utilize test and trusted components, avoid repeated development), but also bring more scalability for JSP applications. JavaBean components can be used to perform complex computing tasks or are responsible for interaction with the database and data extraction. If we have three Javabeans, they have functions of displaying news, stock prices, and weather conditions, and create a web page containing all three functions. It only needs to instantiate these three beans. You can use the HTML form to position them. .

To illustrate the application of JavaBean in a JSP environment, we created a bean called TaxRate. It has two properties, namely Product (product) and RATE (tax rate). Two set methods are used to set these two properties, and the two GET methods are used to extract these two properties. In practical applications, this bean should generally extract tax rates from the database, which we simplifies this process, allowing any set tax rate. Here is the code list of this bean:

Package Tax;

Public class Taxrate {

String products;

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 rateue) {

THIS.RATE = RateValue;

}

Public double getrate () {

Return (this.rate);

}

}

Apply the above Bean to the tag in the JSP page. Depending on the different JSP engine, where it is configured and how to configure bean may also be slightly different. This article puts this bean .class file in the C: .0inf directory, the Tax here is a directory that is specifically stored. Below is an example page that applies the above bean:

<% @ Page language = "java"%>

<% TaxBean.SetProduct ("A002");

TaxBean.Setrate (17);

%>

How to use 1:

Product: <% = TaxBean.getProduct ()%>

Tax rate: <% = TaxBean.Getrate ()%>

<% TaxBean.SetProduct ("A003");

TaxBean.Setrate (3);

%>

How to use 2:

Product:

Tax rate:

Several properties are defined within the tag, where the ID is an identifier of the BEAN in the entire JSP page, the Scope property defines the time of life, the class attribute illustrates the bean class file (starting from the package ).

This JSP page not only uses the BEAN's SET and GET method settings and extraction attribute values, but also uses the second method of extracting the bean property value, even if the tag is used. The name attribute in is the ID of the bean, which specifies the name of the target property.

It turns out that Java Servlet is an ideal architecture for developing web applications. JSP is based on servlet technology and has been improved in many respects. The JSP page looks like a normal HTML page, but it allows embedding code, at this point, it is very similar to the ASP technology. With a cross-platform JavaBean component, JSP provides excellent solutions for separation processing logic and display style. JSP will become a strong competitor of ASP technology

转载请注明原文地址:https://www.9cbs.com/read-123536.html

New Post(0)