views:

180

answers:

2

I have to get 2 numbers and an operation from a jsp file, using a java bean. After submitting the numbers, take them to a servlet from that Java Bean and return an result of them. Problem is that the java bean fields are never completed with the numbers wtitten in textboxes. So, I have, index.jsp's body:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
    <h1>Calculator</h1>
    <FORM METHOD="POST" action="Controller">
        N1: <input type ="text" name="nr1" value="0">
        op: <input type ="text" name="op" value="+">
        N2: <input type ="text" name="nr2" value="0">
        <INPUT class ="button" TYPE="submit" NAME="actiune" VALUE="Calculate"/>
    </FORM>
    <jsp:useBean id="binOp" class="beans.BinaryOperation" scope="session"/>
    <jsp:setProperty name="binOp" property="*"/>
</body>

The servlet's processRequest method, Controller.java, placed in package servlets:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    HttpSession session = request.getSession(true);
    BinaryOperation binOp = (BinaryOperation) session.getAttribute("binOp");

    try {
        if (!binOp.isComplete()) {
            System.out.println(binOp.getNr1() + binOp.getNr2() + binOp.getOp());
            response.sendRedirect("index.jsp");
        } else {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Controller</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Bean in controller " + binOp.getNr1() + "__" + binOp.getOp() + "__" + binOp.getNr2() + "</h1>");
            out.println(binOp.toString());
            out.println("</body>");
            out.println("</html>");
        }



    } finally {
        out.close();
    }
}

And the bean, BinaryOperation, placed in package beans:

package beans;


public class BinaryOperation {

private String nr1;
private String op;
private String nr2;

public void setNr1(String nr1) {
    this.nr1 = nr1;
}

public void setOp(String op) {
    this.op = op;
}

public void setNr2(String nr2) {
    this.nr2 = nr2;
}

public String getNr1() {
    return nr1;
}

public String getOp() {
    return op;
}

public String getNr2() {
    return nr2;
}

public boolean isComplete() {
    return !(((nr1 == null) || (nr1.length() == 0))
            || ((op == null) || (op.length() == 0))
            || ((nr2 == null) || (nr2.length() == 0)));
}
}

In the Apache log I have next output from the if statement(see the servlet - System.out.println(binOp.getNr1() + binOp.getNr2() + binOp.getOp());): nullnullnull

Where is my mistake?

A: 

The bean is only a Java class for hodling properites with getters and setters. It does not have any magic properties and does not fill itself. It just a type of object, like a pattern. This is how they work. You must manually fill the properties you want with the appopriate setSmth method.

avok00
How do I get the values from textboxes? And set them into the bean?
Csaryus
@avok00 - Can you tell us what his jsp's `<jsp:useBean ...` and `<jsp:setProperty...` do?
Tony Ennis
They do nothing in this case obviously :) I am just answering the title of the question. I don't have time to go over all the code. Sorry.
avok00
A: 

I've did one more jsp file between index.jsp and Servlet:

 <jsp:useBean id="binOp" class="beans.BinaryOperation" scope="session"/>
<jsp:setProperty name="binOp" property="*"/>.

This did the "magic".

Csaryus
@BalusC in one way you were right.
Csaryus
My answer resolved the problem. Your answer was very good, but did not solved the problem. I am sorry.
Csaryus
You shouldn't be angry because of this, and unrate my answer.
Csaryus