将有状态会话 bean 与 servlet 一起使用 [英] Using stateful session beans with servlets

查看:31
本文介绍了将有状态会话 bean 与 servlet 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Glassfish Server(一个简单的购物车)尝试我的第一个 EJB.我打算为每个 Http 会话使用 CartBean.如果我的 Cart Bean 正在关注-

I am trying my first EJBs with Glassfish Server ( A simple shopping cart). I intended to use the CartBean for each Http Session. If my Cart Bean is following-

public interface CartLocal {
 public void addItem(String item);
 public void removeItem(String item);
}

@Stateful
public class CartBean implements CartLocal {
 List<String> item = new java.util.ArrayList<String>();
 public void addItem(String item) {
  ....
 }
 public void removeItem(String item) {
  ....
 }
}

我必须在 web servlet 客户端中使用上述有状态会话 bean,以便,对于每个新的 Http 会话,我们都会获得一个新的有状态会话 bean.这样一个用户就有一个购物车.我对在 servlet 中使用会话 bean 的理解是错误的还是下面的代码是错误的,这为所有用户创建了一个有状态的会话 bean.

I have to use the above stateful session bean in a web servlet client such that, for each new Http Session, we get a new stateful session bean. So that there is one shopping cart for one user. Is my understanding wrong of using session beans in servlet or the below code is wrong, this creates one stateful session bean for all the users.

@EJB CartLocal cart;

protected void doGet(....) throws IOException...... {
 cart.addItem(....);
}

推荐答案

http servlet 由该 servlet 的所有客户端共享,因此将有状态会话 bean 注入它是不正确的,并且会导致不良影响.会话 bean 旨在为每个客户端使用,它们通常存储在 http 会话中,以便该会话的所有请求都可以访问会话 bean.您必须在 doGet 方法中使用 jndi 查找并将该引用存储在 http 会话中.存储后,您需要从 http 会话中检索并使用它.

The http servlet is shared by all clients of that servlet so injecting it with a stateful session bean is not correct and will cause undesirable effects. Session beans are meant to be used per client and they are typically stored in http session so that all requests for that session can have access to session bean. You will have to use jndi lookup inside the doGet method and store that reference in http session. Once stored you would need to retrieve from http session and use that.

更多信息

你的理解有点不对.有状态会话 bean 类表示您如何对有状态数据进行建模,这些数据可以是细粒度相关类的图.你从容器中请求一个 statful bean(它创建/管理/激活/钝化它).这些服务是我们使用有状态会话 bean 的目的.然而,容器不知道它移交的任何对象属于哪个客户端.那么 http 会话就出现了.这部分没有得到应有的强调,因此您会感到困惑.Http Session 是一个完美的存储位置,来自同一 Web 客户端的所有请求都可以访问存储在 http 会话中的所有属性.因此,您从容器中请求它并将其保存在一个地方(http 会话),在那里它可以被同一会话的所有请求再次引用.现在想象一下非 Web 客户端的情况.你没有 http 会话或类似的机制.您将必须创建自己的存储机制,以识别具有各自有状态 bean 的不同客户端.一种可能的方法可能是将有状态 bean 引用存储在某处(例如,在键为 clientId 且值为 bean 引用的映射中),以及下次客户端想要访问 bean 时,它会传递 clientId 并从地图中获取它.

You are getting it a bit wrong. Stateful session bean class represents how you model your stateful data which could be a graph of fine grained related classes. You ask for a statful bean from the container (which creates/manages/activates/passivates it).These services are what we use Stateful session bean for. However container has no knowledge that whatever object it is handing over belongs to which client. So then the http session comes into picture. This part is what is not emphasised as much as it should be hence your confusion. Http Session is a perfect storage place where all request from same web client have access to all attributes stored in http session. So you ask for it from the container and keep it in a place (http session) from where it can be referred again by all requests for same session. Now imagine the case of non-web clients. There you do not have http session or similar mechanism. You will have to create a storage mechanism of your own to identify different client with their respective stateful beans.A possible way could be possibly storing the stateful bean reference somewhere ( say in a map with key as clientId and value as bean reference), and the next time client wants to access the bean it passes the clientId and gets it from the map.

就 http 会话优先于有状态 bean 而言,重要的是要了解 http 会话不是线程安全的.因此,如果您有一个 ajax 繁重的应用程序同时访问会话中的对象,您将必须提供自己的同步机制,这将不可扩展,会严重影响性能.在有状态会话 bean 的情况下,容器管理同步.

As far as preference for http session over stateful bean is concerned, it important to understand that http session is not thread safe. So in case you have an ajax heavy application accessing an object in session simultaneously you will have to provide your own synchronization mechanism which will not be scalable and will impact performance heavily. In case of stateful session beans the container manages the synchronization.

这里是一个关于使用http会话的有趣讨论用于存储状态.讨论来自 Brian Goetz 的非常有用的文章所有有状态的 Java Web 应用程序都被破坏了吗?".不幸的是,我无法在 IBM 网站上找到原始文章,但讨论主题提供了足够的材料来思考.

Here is an interesting discussion on the topic of using http session for storing state. The discussion is on Brian Goetz's very informative article "Are All Stateful Java Web Applications Broken?". Unfortunately I cannot locate the original article on IBM site but the discussion thread gives enough material to ponder.

这篇关于将有状态会话 bean 与 servlet 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆