lundi 27 juillet 2015

Session bean returns the wrong value

I have the following html page, the servlet class and the bean class implemented in my eclipse project that runs on wildfly 8.

The problem I have is every time I call the servlet with some values the add operation in the bean class returns a zero. I know I'm not doing a trivial thing correct. What should I change to get the correct value from the add operation.

AddServlet.java

 import java.io.IOException;
 import java.io.PrintWriter;
 import javax.ejb.EJB;
 import javax.servlet.ServletException;
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import ejb.AddEjb;

@WebServlet("/AddServlet")

public class AddServlet extends HttpServlet {
  private static final long serialVersionUID=1L;
  @EJB

AddEjb bean;
  public AddServlet() {
    super();
  }
  protected void doPost(HttpServletRequest request,
  HttpServletResponse response) throws ServletException,
  IOException {
    PrintWriter out=response.getWriter();
    int i=Integer.parseInt(request.getParameter("t1"));
    int j=Integer.parseInt(request.getParameter("t2"));
    bean.setI(i);
    bean.setJ(j);
    bean.add();
    out.print("Addition using bean is test EJB : " + bean.getK());
  }
}

AddEjb.java

package ejb;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;


@Stateless
@LocalBean
public class AddEjb
{
    private int i,j,k;

    public AddEjb() {

    }

    public int getI() {
        return i;
    }

    public void setI(int i) {
        this.i = i;
        System.out.println("i set" + this.i);
    }

    public int getJ() {
        return j;
    }

    public void setJ(int j) {
        this.j = j;
        System.out.println("j set" + this.j);
    }


    public int getK() {
        System.out.println("K returned" + this.k);
        System.out.println("K returned" + k);
        return this.k;
    }


    public void setK(int k) {
        this.k = k;
        System.out.println("k set" + this.k);
    }

    public void add()
    {
        this.k = this.i + this.j;
        System.out.println("k set" + this.k);
    }


}

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>First EJB</title>
</head>
<body>
        <form action="AddServlet" method="post">
                Enter 1st Number : <input type="text" name="t1"><br>
                Enter 2nd Number : <input type="text" name="t2"><br>
        
                <input type=submit>
        </form>
</body>
</html>

Aucun commentaire:

Enregistrer un commentaire