How to apply the javabean component on the jsp page

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.