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>
Following are the sysout logs
18:14:20,601 INFO [org.jboss.as] (Controller Boot Thread) JBAS015874: WildFly 8.2.0.Final "Tweek" started in 9104ms - Started 296 of 349 services (92 services are lazy, passive or on-demand)
18:14:46,936 INFO [stdout] (default task-2) i set78
18:14:46,942 INFO [stdout] (default task-2) j set22
18:14:46,945 INFO [stdout] (default task-2) k set0
18:14:46,949 INFO [stdout] (default task-2) K returned0
18:14:46,950 INFO [stdout] (default task-2) K returned0
The @Stateless
annotation is there to tell the container that instances of this bean can be used interchangeably. When you send a request to a stateless bean, the container chooses a bean instance from the bean pool, and there is no guarantee that any two requests are served by the same instance.
The behaviour you're seeing here is that when you send the "set" requests they are each being served by some bean instances, then when you call the "get" methods they are being served by a totally different instance. Exactly which instance you get is up to the container, and is not defined in the EJB spec.
If you'd like to maintain conversational state between calls, as @Ayhan says, you should use a @Stateful
session bean. If you need to share this state between clients, you probably want a @Singleton
bean.