How to store session in Spring MVC - java

What's the best way of storing session related data of a user (like, for example a log of recent actions a user has done) in a Spring MVC (2.5) web application ?
Using the classic javax.servlet.http.HttpSession or by specifying scope="session" in controller beans, and storing the data in a session object ?

Session-scoped beans (using scope="session") is the cleanest approach. This removes the need to interact with the session yourself.
If you want to autowire a session-scoped bean in to the controller, you either need to make the controller session-scoped itself, or use a scoped-proxy to wire it into a singleton controller, as described here. Either approach is valid.

Related

Bytearray in multithreaded environment in JSF [duplicate]

I noticed that there are different bean scopes like:
#RequestScoped
#ViewScoped
#FlowScoped
#SessionScoped
#ApplicationScoped
What is the purpose of each? How do I choose a proper scope for my bean?
Introduction
It represents the scope (the lifetime) of the bean. This is easier to understand if you are familiar with "under the covers" working of a basic servlet web application: How do servlets work? Instantiation, sessions, shared variables and multithreading.
#Request/View/Flow/Session/ApplicationScoped
A #RequestScoped bean lives as long as a single HTTP request-response cycle (note that an Ajax request counts as a single HTTP request too). A #ViewScoped bean lives as long as you're interacting with the same JSF view by postbacks which call action methods returning null/void without any navigation/redirect. A #FlowScoped bean lives as long as you're navigating through the specified collection of views registered in the flow configuration file. A #SessionScoped bean lives as long as the established HTTP session. An #ApplicationScoped bean lives as long as the web application runs. Note that the CDI #Model is basically a stereotype for #Named #RequestScoped, so same rules apply.
Which scope to choose depends solely on the data (the state) the bean holds and represents. Use #RequestScoped for simple and non-ajax forms/presentations. Use #ViewScoped for rich ajax-enabled dynamic views (ajaxbased validation, rendering, dialogs, etc). Use #FlowScoped for the "wizard" ("questionnaire") pattern of collecting input data spread over multiple pages. Use #SessionScoped for client specific data, such as the logged-in user and user preferences (language, etc). Use #ApplicationScoped for application wide data/constants, such as dropdown lists which are the same for everyone, or managed beans without any instance variables and having only methods.
Abusing an #ApplicationScoped bean for session/view/request scoped data would make it to be shared among all users, so anyone else can see each other's data which is just plain wrong. Abusing a #SessionScoped bean for view/request scoped data would make it to be shared among all tabs/windows in a single browser session, so the enduser may experience inconsitenties when interacting with every view after switching between tabs which is bad for user experience. Abusing a #RequestScoped bean for view scoped data would make view scoped data to be reinitialized to default on every single (ajax) postback, causing possibly non-working forms (see also points 4 and 5 here). Abusing a #ViewScoped bean for request, session or application scoped data, and abusing a #SessionScoped bean for application scoped data doesn't affect the client, but it unnecessarily occupies server memory and is plain inefficient.
Note that the scope should rather not be chosen based on performance implications, unless you really have a low memory footprint and want to go completely stateless; you'd need to use exclusively #RequestScoped beans and fiddle with request parameters to maintain the client's state. Also note that when you have a single JSF page with differently scoped data, then it's perfectly valid to put them in separate backing beans in a scope matching the data's scope. The beans can just access each other via #ManagedProperty in case of JSF managed beans or #Inject in case of CDI managed beans.
See also:
Difference between View and Request scope in managed beans
Advantages of using JSF Faces Flow instead of the normal navigation system
Communication in JSF2 - Managed bean scopes
#CustomScoped/NoneScoped/Dependent
It's not mentioned in your question, but (legacy) JSF also supports #CustomScoped and #NoneScoped, which are rarely used in real world. The #CustomScoped must refer a custom Map<K, Bean> implementation in some broader scope which has overridden Map#put() and/or Map#get() in order to have more fine grained control over bean creation and/or destroy.
The JSF #NoneScoped and CDI #Dependent basically lives as long as a single EL-evaluation on the bean. Imagine a login form with two input fields referring a bean property and a command button referring a bean action, thus with in total three EL expressions, then effectively three instances will be created. One with the username set, one with the password set and one on which the action is invoked. You normally want to use this scope only on beans which should live as long as the bean where it's being injected. So if a #NoneScoped or #Dependent is injected in a #SessionScoped, then it will live as long as the #SessionScoped bean.
See also:
Expire specific managed bean instance after time interval
what is none scope bean and when to use it?
What is the default Managed Bean Scope in a JSF 2 application?
Flash scope
As last, JSF also supports the flash scope. It is backed by a short living cookie which is associated with a data entry in the session scope. Before the redirect, a cookie will be set on the HTTP response with a value which is uniquely associated with the data entry in the session scope. After the redirect, the presence of the flash scope cookie will be checked and the data entry associated with the cookie will be removed from the session scope and be put in the request scope of the redirected request. Finally the cookie will be removed from the HTTP response. This way the redirected request has access to request scoped data which was been prepared in the initial request.
This is actually not available as a managed bean scope, i.e. there's no such thing as #FlashScoped. The flash scope is only available as a map via ExternalContext#getFlash() in managed beans and #{flash} in EL.
See also:
How to show faces message in the redirected page
Pass an object between #ViewScoped beans without using GET params
CDI missing #ViewScoped and #FlashScoped
Since JSF 2.3 all the bean scopes defined in package javax.faces.bean package have been deprecated to align the scopes with CDI. Moreover they're only applicable if your bean is using #ManagedBean annotation. If you are using JSF versions below 2.3 refer to the legacy answer at the end.
From JSF 2.3 here are scopes that can be used on JSF Backing Beans:
1. #javax.enterprise.context.ApplicationScoped: The application scope persists for the entire duration of the web application. That scope is shared among all requests and all sessions. This is useful when you have data for whole application.
2. #javax.enterprise.context.SessionScoped: The session scope persists from the time that a session is established until session termination. The session context is shared between all requests that occur in the same HTTP session. This is useful when you wont to save data for a specific client for a particular session.
3. #javax.enterprise.context.ConversationScoped: The conversation scope persists as log as the bean lives. The scope provides 2 methods: Conversation.begin() and Conversation.end(). These methods should called explicitly, either to start or end the life of a bean.
4. #javax.enterprise.context.RequestScoped: The request scope is short-lived. It starts when an HTTP request is submitted and ends after the response is sent back to the client. If you place a managed bean into request scope, a new instance is created with each request. It is worth considering request scope if you are concerned about the cost of session scope storage.
5. #javax.faces.flow.FlowScoped: The Flow scope persists as long as the Flow lives. A flow may be defined as a contained set of pages (or views) that define a unit of work. Flow scoped been is active as long as user navigates with in the Flow.
6. #javax.faces.view.ViewScoped: A bean in view scope persists while the same JSF page is redisplayed. As soon as the user navigates to a different page, the bean goes out of scope.
The following legacy answer applies JSF version before 2.3
As of JSF 2.x there are 4 Bean Scopes:
#SessionScoped
#RequestScoped
#ApplicationScoped
#ViewScoped
Session Scope: The session scope persists from the time that a session is established until session termination. A session terminates
if the web application invokes the invalidate method on the
HttpSession object, or if it times out.
RequestScope: The request scope is short-lived. It starts when an HTTP request is submitted and ends after the response is sent back
to the client. If you place a managed bean into request scope, a new
instance is created with each request. It is worth considering request
scope if you are concerned about the cost of session scope storage.
ApplicationScope: The application scope persists for the entire duration of the web application. That scope is shared among all
requests and all sessions. You place managed beans into the
application scope if a single bean should be shared among all
instances of a web application. The bean is constructed when it is
first requested by any user of the application, and it stays alive
until the web application is removed from the application server.
ViewScope: View scope was added in JSF 2.0. A bean in view scope persists while the same JSF page is redisplayed. (The JSF
specification uses the term view for a JSF page.) As soon as the user
navigates to a different page, the bean goes out of scope.
Choose the scope you based on your requirement.
Source: Core Java Server Faces 3rd Edition by David Geary & Cay Horstmann [Page no. 51 - 54]

Spring-mvc 3.0 application session scope

While using left menu I am not redirecting to the other page but using href to link other pages. But while doing that my session scope which is limited to request no longer exists.
So here is my controller code:
Setting the session:
request.getSession(true).setAttribute("application", application);
Getting the session Object in other controller:
HttpSession session = request.getSession();
session.getAttribute("application"); //application null in href; redirect works fine
So is there any way I can use "application" session scope in Spring MVC 3. So that I can have the access to session through out my application.
I tried this code snippet in my application-servlet.xml
<!-- a HTTP Session-scoped bean exposed as a proxy -->
<bean id="applicationVO" class="com.nypd.viewobjects.ApplicationVO" scope="globalSession">
<!-- this next element effects the proxying of the surrounding bean -->
<aop:scoped-proxy/>
</bean>
I am injecting the object to set and retrieve the simple bean as below:
#Autowired private ApplicationVO applicationVO;
what I am I doing wrong here ?
I also tried #SessionAttribute on the controller
#SessionAttributes("applicationVO") but it seems the problem still exists.
I will deeply appreciate if anyone can provide me a small example with two controllers.
Read the reference for the defined bean scopes. Here they are:
So what you would usually do is define a bean and register it in scope session. Now you can inject it anywhere you need it. See the explanation here, but beware of this problem (singleton objects with non-singleton dependencies).
Or you can use the #SessionAttributes mechanism to store and retrieve arbitrary session data from your controllers. See the reference here.
Reference:
Bean Scopes > Session scope
Specifying attributes to store in a session with #SessionAttributes
#Session attribute does not store data in session scope. It stores data in conversation scope which is a scope greater than request but less than session. This scope is internally managed by spring for a conversation(which spans across several requests) and removed once the conversation is finished
To store your bean in session scope you will have to declare the requestContextListner in your spring-context.xml which would expose the request to the current thread

get <aop:scoped-proxy/> that is session scoped inside of a jsp

I have my user session stored as an <aop:scoped-proxy/> proxy. how would i go about accessing it on the jsp?
i am assuming that the bean is stored somewhere in the session, correct me if i am wrong.
i found an answer:
http://digitaljoel.nerd-herders.com/2010/11/01/accessing-spring-session-beans-in-jsp/
in short:
${sessionScope['scopedTarget.userSession'].firstName}
works like a charm
Check out this thread. The issue is that session scoped beans (or beans in general) must be injected into the classes that need them and there isn't an easy way to do that with JSP pages. In addition to the solution presented in the thread I linked, you could also inject the user session into your controllers and then add the object to your model. Alternatively, if you wanted to switch to a framework like Spring Security for your user session management, you could make use of their tag library to access the user session information from a JSP.

Spring Session Management

I'm using Spring for my web app. I have used several SimpleFormControllers. I've created a session in the first SimpleFormController for the login page using:
HttpSession session = request.getSession(true);
How can I protect other SimpleFormControllers using Sessions, i.e. so that other controllers won't load if the user is not loged in.
Thank you
You probably want to use Spring Security.
It's flexible and allows restrictions based on roles.
Without it, you will need to manually check in every controller whether the user logged in or not. Or you'll have to "reinvent" a security framework by adding filter to the webapp.
If you only want to protect the operation of getting the session, you need to write a filter that wraps the original request and overrides the getSession methods. There you can check for login data using the original request's getSession().
BTW, getSession() is equivalent to getSession(true)
To protect the Controller from access outside of the intended Session, you may want to compare the Scoping rules you need with this clearly written Guide.
How to get Session Object In Spring MVC
The author gives an example of creating a Controller annotated with #Scope("session")

Spring Webflow Best Practice

I have a java web application which uses spring webflow as framework. I have a problem with processing data on a plain flow xml. When the processing gets more complicated I find it hard to implement using the flow xml of the web flow. I was considering of using controllers to perform these operations. How do I do this. Have no Idea in using controllers in web flow. And from controllers can I jump to the flow xml too to continue processing?
An example of my problem is in submitting forms. Here's the scenario. I have a Users table and authorities table. I also have a User class representing the tablebec I used here Hibernate. In my register page I have the username, password and authority(not a field of the user class) fields. I bind this form to my User object using spring webflow binding. My problem is I can't bind the authority field because it doesn't exist in my User class. Do i need to create a bean representing my form? I need to add the username and password on Users table and authority in another table Authority. Where do I make the initializations for my User object and Authority object or where do I set the values from the registerFormBean to my POJOs? I think it is not a good approach or it will make my flow xml complicated
you can implement this by
jsf as presentation +webflow+mvc as controller
first you will create backing bean with username,password,authority
then let webflow to create the backing bean on session
then when submit the action will call #controller "Spring MVC"
and controller will call your business
and business call DAO which you implemented it as Hibernate
like this
<on-start>
<evaluate expression="youractionClass.createbackingBean()" result="conversationScope.yourbean" />
</on-start>
second solution
you can bind the view to model which contains username,password,authority
then when submit the action will call #controller "Spring MVC"
and controller will call your business
and business call DAO which you implemented it as Hibernate
<view-state id="registerForm" model="registerBean"

Categories