Reputation: 299
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();
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?
Upvotes: 2
Views: 1521
Reputation: 3769
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.
If you use a stateful session bean (SFSB) as the edge of your server, then bean is your session. From this bean you can reference eg a Map or any amount of others beans that can keep session state.
Using the CDI request scope you can inject request scope beans into arbitrary other beans down the call chain. This works just like the http request. 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. Inject this bean or use a producer method, and you'll effectively have an http session map equivalent.
Upvotes: 2
Reputation: 3880
EJB is nothing but a Business logic running independently in a same/separated location
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
It is nothing but a session bean with instance variables. On receiving the instance, you can assign a value of an attribute in the bean
This is usually used in Online shopping, where the transaction has to be traced and maintained
Upvotes: 2
Reputation: 12743
Servlet Session : A session in a Servlet, is maintained by the Servlet Container through the HttpSession object, that is acquired through the request object. 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.
EJB Session : A session in EJB is maintained using the SessionBeans. You design beans that can contain business logic, and that can be used by the clients. You have two different session beans: Stateful and Stateless. 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".
When to use SessionBean
http://docs.oracle.com/javaee/6/tutorial/doc/gipjg.html#gipmt
How to create SessionBean
http://www.roseindia.net/jboss/sessionbeanservlet.shtml
Upvotes: 1
Reputation: 38163
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.
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. Every time a call is done on this exact same reference, you go on the server side to the same bean instance.
Upvotes: 1