How to apply the javabean component on the jsp tutorial page
I. bean technology basics
1) jsp: usebean
<Jsp: usebean id = "beanname" class = "package. class"/>
Build a new bean. For example:
<Jsp: usebean id = "book1" class = "coreservlets. book"/>
Equivalent to the following jsp statement ----
<% Coreservlets. book book1 = new coreservlets. book (); %>
2) jsp: getproperty
<Jsp: getproperty name = "beanname" property = "propertyname"/>
Read or output bean attribute values. For example:
<Jsp: getproperty name = "book1" property = "title"/>
Equivalent ---
<% = Book1.gettitle () %>
3) jsp: setproperty
<Jsp: setproperty name = "beanname" property = "propertyname" value = "propertyvalue"/>
Modify bean attributes. For example:
<Jsp: setproperty name = "book1" property = "title" value = "core servlets and assumerver pages"/>
Equivalent ----
<% Book1.settitle ("core servlets and assumerver pages"); %>
Using these three methods to operate beans is beneficial for web designers who are not familiar with java programming.
II. Shared 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>