简体   繁体   English

如何在EJB项目中创建会话?

[英]How to create a session in an EJB Project?

I am developing an eCommerce application. 我正在开发一个电子商务应用程序。 For that I want to create a session for each customer to maintain their cart information. 为此,我想为每个客户创建一个会话以维护其购物车信息。

If it is a normal Java EE Project I would have used HttpSession session = request.getSession(); 如果它是一个普通的Java EE项目,我将使用HttpSession session = request.getSession(); and add all the cart information to that session. 并将所有购物车信息添加到该会话。

My question is for an EJB project, what is the procedure to create a session for the above mentioned purpose? 我的问题是对于EJB项目,为上述目的创建会话的过程是什么?

EJB is nothing but a Business logic running independently in a same/separated location EJB不过是在相同/分开的位置独立运行的业务逻辑

It does not know, who is invoking the logic. 它不知道是谁在调用逻辑。 In that case, the word session will not fit here. 在这种情况下,会话一词将不适合此处。

You can call it in a different manner as Maintaining the state of an EJB Session bean with the use of Stateful session bean 您可以用不同的方式来调用它,如使用有状态会话Bean 维护EJB会话Bean的状态。

It is nothing but a session bean with instance variables. 它不过是带有实例变量的会话bean。 On receiving the instance, you can assign a value of an attribute in the bean 接收到实例后,您可以在Bean中分配属性值

This is usually used in Online shopping, where the transaction has to be traced and maintained 这通常用于在线购物中,必须跟踪和维护交易

Remote EJB differs from HTTP/Servlet in that the container doesn't associate some kind of map with an identified that you keep sending along, but instead is based on stateful beans with which the client maintains a session. 远程EJB与HTTP / Servlet的不同之处在于,容器不将某种映射与您一直发送的标识关联,而是基于客户端与之维护会话的有状态Bean。

If you use a stateful session bean (SFSB) as the edge of your server, then bean is your session. 如果将有状态会话Bean(SFSB)用作服务器的边缘,则Bean是您的会话。 From this bean you can reference eg a Map or any amount of others beans that can keep session state. 您可以从该bean中引用Map或可以保持会话状态的任何数量的其他bean。

Using the CDI request scope you can inject request scope beans into arbitrary other beans down the call chain. 使用CDI请求范围,可以将请求范围Bean注入到调用链中的任意其他Bean中。 This works just like the http request. 就像http请求一样工作。 There's no session scope that works like this, but from your facade bean you could put the mentioned Map into a request scoped bean. 没有像这样的会话作用域,但是可以从外观bean中将提到的Map放入请求作用域bean中。 Inject this bean or use a producer method, and you'll effectively have an http session map equivalent. 注入此bean或使用生产者方法,您将有效地获得等效的http会话映射。

If with "EJB project" you mean the entry to your application is an EJB bean that's contacted via a remote client using RMI, then the answer is to use a Stateful session bean for the entry call. 如果使用“ EJB项目”,则意味着应用程序的条目是使用RMI通过远程客户端联系的EJB bean,那么答案是将Stateful session bean用于条目调用。

The client needs to request this particular bean from the server (which typically happens via a remote JNDI lookup) and then simply hold on to the reference. 客户端需要从服务器请求这个特定的bean(通常是通过远程JNDI查找发生),然后简单地保留引用。 Every time a call is done on this exact same reference, you go on the server side to the same bean instance. 每次在完全相同的引用上进行调用时,您都会在服务器端访问相同的bean实例。

Servlet Session : A session in a Servlet, is maintained by the Servlet Container through the HttpSession object, that is acquired through the request object. Servlet会话:Servlet容器中的Servlet会话通过HttpSession对象维护该会话,该会话是通过请求对象获取的。 You cannot really instantiate a new HttpSession object, and it doesn't contains any business logic, but is more of a place where to store objects. 您不能真正实例化一个新的HttpSession对象,它不包含任何业务逻辑,而更像是一个存储对象的地方。

EJB Session : A session in EJB is maintained using the SessionBeans. EJB会话:使用SessionBeans维护EJB中的会话。 You design beans that can contain business logic, and that can be used by the clients. 您设计的bean可以包含业务逻辑,并且可以由客户端使用。 You have two different session beans: Stateful and Stateless. 您有两个不同的会话bean:有状态和无状态。 The first one is somehow connected with a single client. 第一个以某种方式与单个客户端连接。 It maintains the state for that client, can be used only by that client and when the client "dies" then the session bean is "lost". 它维护该客户端的状态,只能由该客户端使用,并且当客户端“死亡”时,会话bean将“丢失”。

When to use SessionBean 何时使用SessionBean

http://docs.oracle.com/javaee/6/tutorial/doc/gipjg.html#gipmt http://docs.oracle.com/javaee/6/tutorial/doc/gipjg.html#gipmt

How to create SessionBean 如何创建SessionBean

http://www.roseindia.net/jboss/sessionbeanservlet.shtml http://www.roseindia.net/jboss/sessionbeanservlet.shtml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM