1

Getting my feet wet with JSP/bean action. I'm getting An exception occurred processing JSP page /foo/output.jsp at line 14 on my output page. The java file compiles ok.

input.jsp

<p>First Number: <input type="text" name="first" value="" /></p>
<p>Second Number: <input type="text" name="second" value="" /></p>
<p>Action: <select name="compu">
                <option value="1">+</option>
                <option value="2">-</option>
                <option value="3">*</option>
                <option value="4">/</option>
            </select></p>

<input type="submit" />
</form>

output.jsp

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.util.*" errorPage="" %>

<jsp:useBean id="foo" class="stuff.DerInput" scope="page" />
<jsp:setProperty name="foo" property="*" />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<jsp:getProperty name="foo" property="calculation" />
<p>The result: <%= foo.getCalculation() %></p>

</body>
</html>

DerInput.Java

package stuff;

public class DerInput {
    int first, second, compu, theresult;
    String notation;

    public void setFirst( int value )
    {
        first = value;
    }

    public void setSecond( int value )
    {
        second = value;
    }

    public void setCompu( int value )
    {
        compu = value;
    }

    public String getCalculation() {

        switch (compu) {
            case 1:
                theresult = first + second;
                notation = "+";
                break;
            case 2:
                theresult = first - second;
                notation = "-";
                break;
            case 3:
                theresult = first * second;
                notation = "*";
                break;
            case 4:
                theresult = first / second;
                notation = "/";
                break;
        }

        return first + " " + notation + " " + second + " = " + theresult;
    }
}
justacoder
  • 2,684
  • 6
  • 47
  • 78
  • You forgot to share the exception (it contains the answer). Please edit your question to include it as well. – BalusC Sep 26 '11 at 20:19
  • You're saying that switch statements have to have the default exception case always? – justacoder Sep 27 '11 at 12:37
  • No.. You got an exception, but you didn't include it and its stacktrace in your question. With it, your question would be much easier answerable without the need to guess based on the posted code (which could in turn have many possible problem causes), because the exception contains detailed information about the cause of the problem and therefore implicitly contains the answer as well (you know, once the cause is *understood*, the solution speaks for itself). – BalusC Sep 27 '11 at 14:48

1 Answers1

2

Look here,

<jsp:getProperty name="foo" property="calculation" />
<p>The result: <%= foo.getCalculation() %></p>

You're expecting that the page scoped bean ${foo} is available in scriptlet scope <% foo %> as well. This is not true. They do not share the same variable scope. This would only result in a NullPointerException on the getCalculation() call, because <% foo %> is null.

Use EL instead.

<p>The result: ${foo.calculation}</p>

(Note: that <jsp:getProperty> line is superfluous here, so I removed it)

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • What would I have to change in my java file to output as a function call and not with EL? Or do I change the scope of the page to something broader, like request? – justacoder Sep 27 '11 at 12:48
  • You do not want to do this. *Scriptlets* are considered a poor practice since JSP 2.0 (almost a decade ago already). You should avoid them at all extent. But, for learning purposes, `pageContext.findAttribute()` will find EL variables for you in *scriptlet*. See also http://stackoverflow.com/tags/el/info and http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files – BalusC Sep 27 '11 at 12:56
  • Thanks for the clarification. This shows how much I need to brush up on what's going on. – justacoder Sep 27 '11 at 14:04