How create and keep POJO in SessionScope - java

I have app with J2EE, EJB 3.0 and JSF. My session are been used with JSFSession. I want store user data in EJB that I can read and write in any point of my app (EJB, ManagedBean, Interceptor). Any idea?
THX

I'm not really sure, but if your data is not user specific, you should probably use #stateless Bean

Related

EJB injections vs only JSF managed beans

I don't know may be my question is completely meaningless, but I can't find any straight information about it.
I had my JSF + Tomcat application and it worked just fine. I want to implement Restful services and JAAS logic. In order to do that, I switched to TomEE++.
Switching to TomEE means that my server will now be more heavy and I'm afraid than it will require more cpu and ram resources than I have in my Amazon AWS micro instance.
The question is: Will the performance drop down if I switch from only JSF managed beans to JSF + EJB?
The sub-question is: what EJB injections can do, and JSF managed beans can not do? (JSF does not allow cyclic injections for example)
Will the performance drop down if I switch from only JSF managed beans to JSF + EJB?
Measuring is knowing. But generally, this concern makes no sense.
what EJB injections can do, and JSF managed beans can not do? (JSF does not allow cyclic injections for example)
Generally, they are not intented to be interchangeable. Each have their own clear responsibility. You use JSF bean management annotations to manage beans for front-end (JSF pages). You use EJB bean management annotations to manage beans for business services (BOs/DAOs). That's it.
To learn how JSF+EJB(+JPA) are supposed to work together head to the concrete examples and explanations shown in the following answers:
JSF Service Layer
JSF managed-bean EJB injection
Filter do not initialize EntityManager
JSF request scoped bean keeps recreating new Stateful session beans on every request?

Storing entities for simultaneous edition in one session in EJB JSF

We have an ideological problem while creating a Web Application in JSF, EJB and JPA.
Our example situation is:
Admin displays the list of users in datatable. Next, he selects user1, which leads him to new user-edition site. The issue occure if he tries to open second card or window and select user2 for simultaneous edition in the same session.
When we try to save user1 data after edition it is not possible, because it is being overwritten in Endpoint by user2.
Data storing:
Because we do not store any data in View part of our project [diagram available below], after displaying it the Managed Beans are being destroyed. Therefore in Controller part we decided to keep the currently selected user as a field in Endpoint [Stateful EJB Bean] which is constant for a session as it is held by Session Scoped Managed Bean.
We believe we should not store any Collections in Endpoint or Session Scope Managed Bean.
Problem:
Particular case is an overview of the situation. In our application we want to edit multiple entities of the same type during on session.
Question:
Where and how should we store the current selections of User/Admin, which lead to edition of that selected entity.
Storing data in view, request scoped part allowed us to control multiple entities in the same session, though we think it is not appropriate approach. But now storing it in controller part leads to limit of one entity of the same type being edited in the same session.
DIAGRAM HERE: http://i.stack.imgur.com/9PyYr.jpg
So the lesson you learned hopefully is to not use the session scope for editing data or for transferring data between pages.
What you should do here is use a GET request with merely the id of the user to be edited. Then on the edit page, use a single view scoped backing bean.
Using this pattern you do not need extra extensions. Only if you use CDI beans as backing beans would you need CODI, since the default #ViewScoped unfortunately does not work with CDI beans. CODI provides a version that does work with CDI.
But if you use JSF managed beans, follow the pattern outlined above and you'll be fine.
Since you are using a Java EE server (Glassfish 3.1) you could get advantage of using CDI which supports different scopes than JSF. There is a CDI extension called CODI which provides the so called "window scope", allowing you to scope your beans per browser window which will solve your problem. More info about the window scope can be found here.
Another option is to use IceFaces JSF library that also supports its own window scope. More info can be found here.

Applet and JSF Integration - example

Is there a tutorial or a simple applet example with JSF? How do I perform a request to a Managed Bean from an applet?
Don't use a JSF managed bean. It is not suitable for this job. Use a servlet or a webservice. To exchange data, use the session scope with an unique autogenerated key which you pass as parameter to the applet beforehand. This way the data will be available to JSF as well.
JSF (and hence managed beans) executes on the server to produce HTML; An applet executes on the client's machine - so you can't just pass a reference to a managed bean to an applet.
If you just need to pass a value from a managed bean to an Applet at start time, you can use the <param> sub-element of the tag to pass this value.
If you need some kind of dynamic access to the managed bean, it's going to be a lot harder - basically, you'll need to build some kind of web service that's backed by the managed bean so that the applet can make http requests back to the server to get the values it needs.

Session aware spring bean

Is there any way to define a spring bean which will be notified when data in session has changed?
I would also like to know pure java solution if possible. All I want is when i add/edit/delete data in httpsession then I want one java class to be notified to do some processing on that data.
Thanks
You don't need Spring for this, the Servlet API provides that out-of-the-box, via the HttpSessionAttributeListener interface:
This listener interface can be implemented in order to get notifications of changes to the attribute lists of sessions within this web application.
You declare it as a <listener> in your web.xml file. See here for an example.
In don't know of a specific Spring-friendly way of doing this, though, I think you'll have to use the above listener approach, and notify your Spring beans from there.

Use of servletcontext?

I created one web application so i want to store my past logged user name list for comparing new users which are going to login. so how i do that by using ServletContext ?
or is there any other way?
In JSF, application scoped managed beans are stored in the servletcontext. So, you could basically just create and declare an application scoped managed bean and put the list in there.
However, there are probably better ways for the particular functional requirement which is yet unclear in the question. At least, implementing a HttpSessionListener or HttpSessionBindingListener is probably a better idea since logins are usually coupled to the HttpSession.
Here are several examples:
How to invalidate session when user logs in twice?
How to check who's online?

Categories