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

查看:196
本文介绍了使用有状态会话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

I'm stuck with Stateful Session Beans.

以下是一个问题:
如何使用 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。
不使用会话,两个不同的浏览器具有相同的计数器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 ?

如果我理解正确,则没有使用 HttpSession SFSB 相同的方式,使用 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 vs 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的连接。嵌入在这个代理服务器中可以与某个特定SFSB关联的某种ID。通过持有这个代理对象,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应用程序发出初始请求时获取服务器可以与特定的 HTTPSession 实例关联的 JSESSIONID 。通过持有 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应用程序不同,浏览器与Web页面进行通信,而不是直接与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.

Web层为访问SFSB的客户端代码必须保持到特定的代理引用。在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天全站免登陆