jsp调用javabean实例

jsp调用javabean实例

goodsbean.java

package sale;
public class goodsbean{
    String Product;
    double Price;
    public goodsbean (){
    this.Product = "box";
    this.Price = 5.0;
    }
public void setProduct (String ProductName){
    this.Product = ProductName;
}
public String getProduct(){
    return (this.Product);
}
public void setPrice (double priceValue){
    this.Price = priceValue;
}
public double getPrice(){
    return (this.Price);
}
}

编译完生成的.class文件,放到:Tomcat 5.5\webapps\ROOT\WEB-INF\classes\sale 目录下。

调用方法:

<%@ page contentType="text/html;charset=GB2312"%>
<%//request.setCharacterEncoding("iso_8859_1");%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>goodsbean</title>
</head>

<body>

<jsp:useBean id="goodsbean" scope="application" class="sale.goodsbean" />
<%
goodsbean.setProduct("clock");
goodsbean.setPrice(17);
%>
使用方法1:
产品:<%= goodsbean.getProduct()%><br>
税率:<%= goodsbean.getPrice()%>

<%goodsbean.setProduct("chair");
goodsbean.setPrice(3);
%>
<br>使用方法2:
产品:<jsp:getProperty name="goodsbean" property = "Product" />
<br>
税率:<jsp:getProperty name="goodsbean" property = "Price" />

</body>
</html>

注意:设置bean的属性如下

<jsp:setProperty name="goodsbean" property = "Product"   value="asdf"/>
<jsp:setProperty name="goodsbean" property = "Price" value="adad" />

原文地址:https://www.cnblogs.com/qqnnhhbb/p/877356.html