Why shouldn't I use the JSF SessionScoped bean for logic?

I am developing a java EE web application using JSF with a shopping cart style process, so I want to collect user data across multiple pages and then do something with it.

I thought of using a session with an EJB 3 bean state, but my research makes me think that SFSB is not tied to the http client session, so I would have to manually track it through httpSession, some side questions here.,.

1) Why it is called a bean session, as far as I can see, it has nothing to do with the session, I could achieve this by saving the pojo in the session.

2) What is the point of inserting if everything that I am going to introduce? Is this a new instance of this SFSB, then could I also use pojo?

Thus, the main problem that I see is written throughout the fact that JSF is a presentation technology, so it cannot be used for logic, but it seems to be an ideal option for collecting user data.

I can set the scope of the JSF bean session as the managed property of my entire beans request, which means it was entered into them, but unlike SFSB, the bean-bound JSF session is bound to the http session and therefore the same instance always entered until the http session has been canceled.

So I have several levels

Level 1) A managed JSF request with beans span that deals with presentation, 1 per page.
Level 2) A managed JSF session limited by a bean that has the values ​​specified in it by the beans query.
Level 3) A stateless EB session that runs data logic in a JSF session in a bean.

Why is it so bad?

An alternative is to use SFSB, but then I have to enter it in my initial bean request and then save it in an http session and grab it in every subsequent bean request - it just seems messy.

Or I could just keep everything in the session, but this is not ideal, because it involves using alphabetic keys and castings. and the like .. and the like what a prone mistake., and randomly!

Any thoughts are appreciated, I feel like I'm struggling with this technology, not with it.

thanks

+4
source share
3 answers

Why it is called a bean session, as far as I can see, it has nothing to do with the session, I could achieve this by saving the pojo in the session.

From the old J2EE 1.3 tutorial :

What is a Bean session?

A bean session is a single client inside a J2EE server. to access the deployment of the application on the server, the client calls a session bean. The bean session does the work for its client, protecting the client from complexity by completing business tasks inside the server.

As the name implies, a bean session is similar to an interactive session. A bean session is not used - it can only have one client, in the same way that only one user can have an interactive session. Like an interactive session, a bean session is not stubborn. (That is, his data is not stored in the database.) When the client completes, it appears its session bean cease and is no longer associated with the client.

Thus, it is associated with a “session”. But a session does not necessarily mean an "HTTP session"

What is the point of inserting it, if all I'm going to introduce is a new instance of this SFSB, then I could also use pojo?

Well, first of all, you do not inject SFSB into a stateless component (injection into another SFSB will be ok), you need to search. Secondly, the choice between an HTTP session and an SFSB really depends on your application and your needs. From a purely theoretical point of view, an HTTP session should be used for presentation logic state (for example, where you are in your multi-page form), while SFSB should be used for business logic state. This is well explained in the "old" HttpSession vs Session session beans stream on TSS, which also has a good example where SFSB makes sense:

You might want to use a bean session to track the status of a particular transaction. that is, buying a train ticket.

The web session monitors the state where the user is on the html page flow. However, if the user then got access to the system through another channel, for example, a wap-phone or through a call center, you still want to know the ticket purchase transaction status.

But SFSBs are not simple, and if you don’t need to justify their use, my practical advice is to stick with an HTTP session (especially if this is not the case for you). Just in case, see

Thus, the main problem that I see is written throughout the fact that JSF is a presentation technology, so it cannot be used for logic, but it seems to be an ideal option for collecting user data.

This is not business logic, presentation logic.

So, I have several levels (...)

Not. You probably have a client tier, a presentation tier, a business tier, a data tier. What you describe is more like layers (not even sure). Cm:

Why is it so bad?

I don’t know, I don’t know what you are talking about :) But you probably should just collect information about the form of the multi-page page in the SessionScoped bean and call the Stateless Session bean (SLSB) at the end of the process.

+9
source

1) Why it is called a bean session, as far as I can see, it has nothing to do with the session, I could achieve this by saving the pojo in the session.

Correction: An EJB session has nothing to do with an HTTP session. In EJB, roughly speaking, a client is a servlet container, and a server is an EJB container (both run on a web application server). In HTTP, the client is a web browser and the server is a web application server.

Does that make sense now?

2) What is the point of inserting if everything that I am going to introduce? Is this a new instance of this SFSB, then could I also use pojo?

Use EJB for transactional business tasks. Use a session-driven bean to store data related to an HTTP session. By the way, none of them are POJO. Just Javavian.

Why shouldn't I use the JSF SessionScoped bean for logic?

If you don't use the transactional business tasks and the abstraction that EJB provides, then just doing it in a simple JSF managed bean is really not a bad alternative. This is also the normal approach in major JSF applications. However, actions are typically performed in a request-driven bean, where the area in which the session is being entered is entered as @ManagedProperty .

But since you are already using EJB, I would ask if there was any specific reason for using EJB. If this is a business requirement from hand to hand, I would just stick to it. At least your session confusion should be cleared.

+5
source

Just in case, if you don’t know this, and as a small contribution to the answers you have, you can really announce SFSB with @SessionScoped, and CDI will handle the EJB life cycle ... That would connect EJB with Http- session managed by CDI. Just let you know, because in your question you say:

 but my research leads me to believe that a SFSB is not tied to a client http session, so I would have to manually keep track of it via an httpSession, some side questions here . . . 

In addition, you can do what you offer, but it depends on your requirements, until CDI beans receives declarative transaction support or extended save contexts, etc., you will find that you write a lot of templates that will make your bean no clean. Of course, you can also use frameworks such as Seam (now go to DeltaSpike ) to extend some of your beans' capabilities through their extensions.

So, I would say yes, at first glance you may feel that you do not need to use EJB with state, but some use cases can be better solved through them. If a user adds a product to his cart and another user adds the same product later, but there is only one unit in the warehouse, who gets it? who faster checks? or the one who added it first? What if you want to access the entity manager to save cardboard in case the user decides to accidentally close his browser or what to do if you have transactions that spawn multiple pages and you want each step to be synchronized with db ? (To keep the transaction open for so long is not recommended, but maybe there may be a scenario where this is necessary?) You can use SLSB, but sometimes it is better and cleaner to use SFSB.

+2
source

All Articles