netbeans JSP中的JavaBean

7fyelxc5  于 2022-11-10  发布在  Java
关注(0)|答案(1)|浏览(160)

我是这个世界的新代码。我试图建立一个简单的Web应用程序,有CoffeeOrder.html表示欢迎页面,CoffeeProcess.jsp处理计算(在这种情况下,我不做任何计算)和CoffeeBean.java类。
这些是我的代码附件,
CoffeeBean.java

package coffee.bean;

public class CoffeeBean implements java.io.Serializable
{
    private int numSugar;
    private double price;
    private String typeCoffee;

    //default constructor
    public CoffeeBean()
    {}

    //normal constructor
    public CoffeeBean(int numSugar, double price, String typeCoffee)
    {
        this.numSugar = numSugar;
        this.price = price;
        this.typeCoffee = typeCoffee;
    }

    //accessor method
    public int getSugar()
    { return numSugar; }

    public double getPrice()
    { return price; }

    public String getType()
    { return typeCoffee; }

    //mutator method
    public void setSugar(int numSugar)
    { this.numSugar = numSugar; }

    public void setPrice(double price)
    { this.price = price; }

    public void setCoffee(String typeCoffee)
    { this.typeCoffee = typeCoffee; }

}

CoffeeOrder.html

<!DOCTYPE html>
<html>
    <head>
        <title>Order</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <h1>Javabeans in JSP</h1><br>
        <h1>Coffee Order</h1>

        <form action='CoffeeProcess.jsp' method='POST'>
            Type of Coffee  <input type='text' name='typeCoffee'/><br>
            Number of Sugar <input type='text' name='numSugar'/><br>
            Price           <input type='text' name='price'/><br>

            <input type='Submit' value='Submit'/>
        </form>

    </body>

</html>

CoffeeProcess.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Javabeans in JSP</title>
    </head>
    <body>
        <h1>Customer Order</h1>

        <jsp:useBean id = "coffee" scope="request" class= "coffee.bean.CoffeeBean"> 
            <jsp:setProperty name = "coffee" property = "numSugar" value="1"/>
            <jsp:setProperty name = "coffee" property = "price" value="23"/>
            <jsp:setProperty name = "coffee" property = "typeCoffee" value="asf"/>
        </jsp:useBean>

        <p>Type of Coffee:<jsp:getProperty name="coffee" property="typeCoffee"/></p>
        <p>Number of Sugar:<jsp:getProperty name="coffee" property="numSugar"/></p>
        <p>Price:<jsp:getProperty name="coffee" property="price"/></p>

    </body>
</html>

这是我在CoffeeOrder.html中点击提交按钮时得到的错误
HTTP Status 500
某人:c

k4ymrczo

k4ymrczo1#

出现此错误是因为jsp能够找到typeCoffee getter setter方法,因为您为方法指定的名称是getType()setType(),与numSugar相同,并且不匹配。相反,您的getter-setter应如下所示:

//getter for numSugar
    public int getNumSugar() {
        return numSugar;
    }
      //setter for numSugar
    public void setNumSugar(int numSugar) {
        this.numSugar = numSugar;
    }

   //getter for typeCoffee
    public String getTypeCoffee() {
        return typeCoffee;
    }
   //setter for typeCoffee
    public void setTypeCoffee(String typeCoffee) {
        this.typeCoffee = typeCoffee;
    }

同样,如果你需要将值从form传递到你的<jsp:setProperty..>,你可以如下所示:

<jsp:useBean id = "coffee" scope="request" class= "coffee.bean.CoffeeBean"> 
      <jsp:setProperty name = "coffee" property = "numSugar" value="${param.numSugar}"/>
      <jsp:setProperty name = "coffee" property = "price" value="${param.price}"/>
      <jsp:setProperty name = "coffee" property = "typeCoffee" value="${param.typeCoffee}"/>
  </jsp:useBean>

相关问题