使用有状态会话 Bean 来跟踪用户的会话 [英] Using a Stateful Session Bean to track an user's session

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

问题描述

这是我在这里的第一个问题,我希望我做对了.

it's my first question here and I hope that I'm doing it right.

我需要处理一个 Java EE 项目,因此,在开始之前,我会尝试做一些简单的事情,看看我是否可以做到.

I need to work on a Java EE project, so, before starting, I'm trying to do something simple and see if I can do that.

我坚持使用有状态会话 Bean.

问题来了:我如何使用 SFSB 来跟踪用户的会话?我看到的所有示例都以将"SFSB放入"HttpSession 属性结束.但我不明白为什么!我的意思是,如果 bean 是 STATEFUL,为什么我必须使用 HttpSession 来保留它?

Here's the question : How can I use a SFSB to track an user's session? All the examples that I saw, ended up in "putting" the SFSB into a HttpSession attribute. But I don't understand why! I mean, if the bean is STATEFUL, why do I have to use the HttpSession to keep it?

EJB 容器的任务不是将正确的 SFSB 返回给客户端吗?

Isn't an EJB Container's task to return the right SFSB to the client?

我尝试过一个简单的计数器豆.在不使用会话的情况下,两个不同的浏览器具有相同的计数器 bean(单击增量"更改了它们的值).使用会话,我有两个不同的值,每个值对应于每个浏览器(在 Firefox 上单击增量",仅向 Firefox 的 bean 添加一个).

I've tried with a simple counter bean. Without using the session, two different browsers have the same counter bean (clicking on "increment" changed the value for both of them). Using session, I have two different values, each for every browser (clicking on "increment" on Firefox, added one just to Firefox's bean).

但是我的老师告诉我 SFSB 保持与客户的对话状态",那么为什么它不使用 HttpSession 就不能工作?

But my teacher told that a SFSB keeps the "conversational state with a client", so why it doesn't just work without using a HttpSession ?

如果我理解正确,使用 HttpSessionSFSB 是不是和使用 SLSB 一样?

If I understood correctly , isn't using HttpSession with a SFSB the same of doing it with a SLSB instead?

我希望我的问题很清楚,而且我的英语没有那么差!

I hope that my question(s) is clear and that my English is not that poor!

我正在研究登录系统.一切顺利,完成登录后,它会将我带到显示用户数据的个人资料页面.但是重新加载页面会使我的数据消失!我尝试在记录时添加 HttpSession 但这样做会使数据在注销后保持不变!

EDIT : I'm working on a login system. Everything goes fine and after completing the login it takes me to a profile page that show user's data. But reloading the page makes my data disappear! I've tried adding HttpSession while logging but doing in this way makes the data stay even after the logout!

推荐答案

有状态会话 Bean (SFSB) 必须与 Web 环境中的 HTTP 会话结合,因为它是一个纯业务 Bean,本身对 Web 一无所知层.

A Stateful Session Bean (SFSB) has to be combined with the HTTP session in a web environment, since it's a pure business bean that itself knows nothing about the web layer.

传统上,EJB 甚至强制存在于他们自己的模块(EJB 模块)中,如果他们愿意,甚至无法访问 Web 工件.这是分层系统的一个方面.请参阅在 JavaEE 6 WAR 与 EAR 中打包 EJB 了解更多相关信息.

Traditionally EJBs even mandatory lived inside their own module (the EJB module), that couldn't even access web artifacts if they wanted to. This is an aspect of layered systems. See Packaging EJB in JavaEE 6 WAR vs EAR for more information about that.

有状态会话 Bean 的原始客户端包括 Swing 桌面应用程序,它们通过二进制协议与远程 EJB 服务器进行通信.Swing 应用程序将通过代理/存根对象获得到远程有状态会话 Bean 的连接.嵌入在此代理中的是某种 ID,服务器可以将其与特定的 SFSB 相关联.通过持有这个代理对象,Swing 客户端可以重复调用它,这些调用将转到同一个 bean 实例.这将因此在客户端和服务器之间创建一个会话.

The original clients for Stateful Session Beans were among others Swing desktop applications, that communicated with the remote EJB server via a binary protocol. A Swing application would obtain a connection to a remote Stateful Session Bean via a proxy/stub object. Embedded in this proxy is an ID of some kind that the server can associate with a specific SFSB. By holding on to this proxy object, the Swing client can make repeated calls to it and those will go to the same bean instance. This will thus create a session between the client and the server.

对于 Web 应用程序,当浏览器向 Java EE Web 应用程序发出初始请求时,它会获得一个 JSESSIONID,服务器可以将其与特定的 HTTPSession 关联> 实例.通过持有这个 JSESSIONID,浏览器可以为它提供每个后续请求,这将激活相同的 http 会话服务器端.

In the case of a web application, when a browser makes an initial request to a Java EE web application it gets a JSESSIONID that the server can associate with a specific HTTPSession instance. By holding on to this JSESSIONID, the browser can provide it with each followup request and this will activate the same http session server-side.

因此,这些概念非常相似,但它们不会自动相互映射.

So, those concepts are very similar, but they do not automatically map to each other.

浏览器只获取JSESSIONID,不知道任何SFSB ID.与 Swing 应用程序不同的是,浏览器与网页通信,而不是直接与 Java bean 通信.

The browser only gets the JSESSIONID and has no knowledge about any SFSB ID. Unlike the Swing application, the browser communicates with web pages, not directly with Java beans.

为了将客户端的请求映射到特定的有状态会话 bean,EJB 容器只关心通过 SFSB 代理提供的 ID.它无法查看调用是否来自 Web 模块中的代码,并且不能/不应该真正访问任何 HTTP 上下文.

For mapping the client's request to a specific stateful session bean, the EJB container only cares about the ID provided via the SFSB proxy. It can't see if the call happened to originate from code in the web module and can't/shouldn't really access any HTTP contexts.

作为访问 SFSB 的客户端代码的 Web 层必须保持"特定的代理引用.保留 Web 层中的某些内容通常意味着将其存储在 HTTP 会话中.

The web layer being the client code that accesses the SFSB must 'hold on' to a specific proxy reference. Holding on to something in the web layer typically means storing it in the HTTP session.

然而,有一种称为CDI 的桥接技术可以实现这种自动连接.如果您使用 CDI 的 @SessionScoped 注释您的 SFSB 并通过 CDI 获取对 SFSB 的引用(例如使用 @Inject),您不必手动将您的 SFSB 放入http 会话.但是,无论如何,CDI 在幕后都会这样做.

There is however a bridge technology called CDI that can make this automatic connection. If you annotate your SFSB with CDI's @SessionScoped and obtain a reference to the SFSB via CDI (e.g. using @Inject), you don't have to manually put your SFSB into the http session. However, behind the scenes CDI will do exactly that anyway.

这篇关于使用有状态会话 Bean 来跟踪用户的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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