问题描述
我正在阅读Java EE 6教程,我正在尝试了解无状态和有状态会话bean之间的区别。如果无状态会话bean不会在方法调用之间保留状态,为什么我的程序会按照它的方式执行? package mybeans ;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
@LocalBean
@Stateless
public class MyBean {
private int number = 0;
public int getNumber(){
return number;
}
public void increment(){
this.number ++;
}
}
客户端
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet。*;
import javax.servlet.http。*;
import javax.servlet.annotation.WebServlet;
import mybeans.MyBean;
import java.io.PrintWriter;
@WebServlet(name =ServletClient,urlPatterns = {/ ServletClient})
public class ServletClient扩展HttpServlet {
private static final long serialVersionUID = 1L;
@EJB
MyBean mybean;
protected void doGet(HttpServletRequest request,
HttpServletResponse response)throws ServletException,IOException {
PrintWriter out = response.getWriter();
mybean.increment();
out.println(mybean.getNumber());
}
}
我期待getNumber返回0每次都是返回1,并且在浏览器中重新加载servlet会增加它。当然,问题在于我对无状态会话bean的工作原理以及库或应用程序服务器的工作原理。有人可以给我一个简单的hello world类型的无状态会话bean的例子,当你将它更改为状态时,行为不同?
重要的区别不在于私有成员变量,而是将状态与特定用户关联起来(认为购物车)。
有状态的会话bean就像会话一样在servlets中。有状态会话bean允许您的应用程序仍然拥有该会话,即使没有Web客户端。当应用服务器从对象池中获取无状态会话bean时,它知道它可以用于满足任何请求,因为它不与特定用户相关联。
有状态的会话bean必须被首先发现给用户,因为他们的购物车信息应该仅被他们知道。应用服务器确保这样做。想象一下,如果您可以开始购物,应用程序将会如何受欢迎,然后应用程序服务器在遇到问题时向我发送状态会话bean!
所以您的私人数据成员是确实是国家,但不是购物车。尝试重做(非常好)的例子,以使增量变量与特定用户相关联。增加它,创建一个新的用户,看看他们是否仍然可以看到递增的值。如果操作正确,每个用户都应该看到他们的计数器版本。
I am going through the Java EE 6 tutorial and I am trying to understand the difference between stateless and stateful session beans. If stateless session beans do not retain their state in between method calls, why is my program acting the way it is?
package mybeans;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
@LocalBean
@Stateless
public class MyBean {
private int number = 0;
public int getNumber() {
return number;
}
public void increment() {
this.number++;
}
}
The client
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import mybeans.MyBean;
import java.io.PrintWriter;
@WebServlet(name = "ServletClient", urlPatterns = { "/ServletClient" })
public class ServletClient extends HttpServlet {
private static final long serialVersionUID = 1L;
@EJB
MyBean mybean;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
mybean.increment();
out.println(mybean.getNumber());
}
}
I was expecting getNumber to return 0 every time but it is returning 1 and reloads of the servlet in my browser increase it more. The problem is with my understanding of how stateless session beans work and not with the libraries or application server, of course. Can somebody give me a simple hello world type example of a stateless session bean that behaves differently when you change it to stateful?
The important difference is not private member variables, but associating state with a particular user (think "shopping cart").
The stateful piece of stateful session bean is like the session in servlets. Stateful session beans allow your app to still have that session even if there isn't a web client. When the app server fetches a stateless session bean out of the object pool, it knows that it can be used to satisfy ANY request, because it's not associated with a particular user.
A stateful session bean has to be doled out to the user that got it in the first place, because their shopping cart info should be known only to them. The app server ensures that this is so. Imagine how popular your app would be if you could start shopping and then the app server gave your stateful session bean to me when I came along!
So your private data member is indeed "state", but it's not "shopping cart". Try to redo your (very good) example to make it so the incremented variable is associated with a particular user. Increment it, create a new user, and see if they can still see the incremented value. If done correctly, every user should see just their version of the counter.
这篇关于无状态和有状态的企业Java Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!