java - Session bean returns the wrong value -


i have following html page, servlet class , bean class implemented in eclipse project runs on wildfly 8.

the problem have every time call servlet values add operation in bean class returns zero. know i'm not doing trivial thing correct. should change correct value 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 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 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 lazy, passive or on-demand) 18:14:46,936 info  [stdout] (default task-2) 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 there tell container instances of bean can used interchangeably. when send request stateless bean, container chooses bean instance bean pool, , there no guarantee 2 requests served same instance.

the behaviour you're seeing here when send "set" requests each being served bean instances, when call "get" methods being served totally different instance. instance container, , not defined in ejb spec.

if you'd maintain conversational state between calls, @ayhan says, should use @stateful session bean. if need share state between clients, want @singleton bean.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -