I have read different articles on using @Stateful
and @SessionScoped
annonations and their differences including this post. From a definition point, @sessionScoped
is used when a session is needed/created between client/web tier, while @Stateful
is needed in Bussiness Logic layer. But I still do not get a hold of the real differences when it comes to implementing them. Here is a simple example
@Named
@SessionScoped
ShoppingCartUIBean {
@inject
shoppingCart cart;
// more code
}
@Stateful
ShoppingCart {
//business logic of adding/updating/deleting cart items
}
- How is the Http session maintained by
@SessionScoped
bean between a given user and server? That is, if I have a shopping cart opened in different computers, I should be able to see my shopping cart, which is associated with my user profile. How is this established? - what happens if I switch the two
annonations
on the above beans? will it have any effect? (sorry this might sound stupid. I am getting into Java EE world, so I want to get basics correct). - According to this great post on Differences : @SessionScoped vs @Stateful and @ApplicationScoped vs @Singleton, @Stateful beans are hardly used in web applications. Is there a case where
@Stateful
is absolutely necessary? - ON a related note: is it legal to inject a
@stateful
bean into@ApplicatonScoped
bean? This would mean entire the application has a single@stateful
bean and all clients uses the same instance of one stateful bean via proxy. (Just as is demonstrated here, not to inject@Stateful
inservlets
EJB example for stateless and stateful beans difference).
Thanks.