Search code examples
formsjspjavabeans

The value for the useBean class attribute is invalid


I am new in JSP and trying to to simple power calculater. So I take 2 numbers from user and later I get result of calculation and show on page. Here is my bean class:

package org.mypackage.power;

public class MyPow {

    private double base;
    private double pow;
    private double result;

    MyPow()
    {
        base = 0;
        pow=1;
    }

    /**
     * @return the base
     */
    public double getBase() {
        return base;
    }

    /**
     * @param base the base to set
     */
    public void setBase(double base) {
        this.base = base;
    }

    /**
     * @return the pow
     */
    public double getPow() {
        return pow;
    }

    /**
     * @param pow the pow to set
     */
    public void setPow(double pow) {
        this.pow = pow;
    }


    /**
     * @return the result
     */
    public double getResult() {
       return Math.pow(base, pow);
    }

    /**
     * @param result the result to set
     */
    public void setResult(double result) {
        this.result = result;
    }
}

And here is the index page:

<HTML>
<BODY>
<FORM METHOD=POST ACTION="result.jsp">
What's your base? <INPUT TYPE=TEXT NAME=base SIZE=20>
What is your power <INPUT TYPE=TEXT NAME=power SIZE=10>

<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>

And here is the JSP page that will show the result

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <jsp:useBean id="powerBean" scope="session" class="org.mypackage.power.MyPow" />
        <jsp:setProperty name="powerBean" property="*"/>
        <jsp:getProperty name="powerBean" property="result"/>
    </body>
</html>

And this code gives

The value for the useBean class attribute is invalid

My class is under the org.mypackage.power.MyPow package. Before I update this it was a simple hello world and was working correctly. But I just change class and add new fields and changed JSP page. Could anyone help me please?

I am using Tomcat 7.0.14 and Netbeans 7.01


Solution

  • This error basically means that

    MyPow powerBean = new MyPow();
    

    has failed.

    The beans are required to have a public constructor. So, change the package-private constructor

    MyPow() {
        // ...
    }
    

    to a public constructor

    public MyPow() {
        // ...
    }
    

    This way JSP (which is by itself in a different package) will be able to access and invoke the bean's constructor.