Harith Azwar
Harith Azwar

Reputation: 83

JavaBean in JSP

I am new to this world of code. I am trying to build a simple web application that have CoffeeOrder.html which represents welcome page, CoffeeProcess.jsp which process the calculation (in this case Im not doing any calculation) and CoffeeBean.java class.

These are attachment of my code,

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>

and this is the error I got when I click submit button in CoffeeOrder.html

HTTP Status 500

somebody :c

Upvotes: 2

Views: 790

Answers (1)

Swati
Swati

Reputation: 28522

You have got this error because jsp is able to find the typeCoffee getter setter method as the name which you have given to your method are getType() and setType() same for numSugar as well is missmatched. Instead your getter-setter should look like below :

    //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;
    }

Also , if you need to pass the value from form to your <jsp:setProperty..> you can do like below :

  <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>

Upvotes: 1

Related Questions