-
Notifications
You must be signed in to change notification settings - Fork 38.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Injected HttpServletRequest object in session-scoped bean should transparently access current request [SPR-5135] #9808
Comments
Grant Gochnauer commented I printed the HttpServletRequest object in a bean that is called when requesting a page through a Spring controller and I see: So it appears the request is wired in properly. I tried doing a simple call to see if it would succeed. First page load, the call worked and printed out the values below valid? false Hitting refresh to load the page a 2nd time, I see the following: Caused by: java.lang.NullPointerException Line 66 of PersonalizedContentAspect is: "siteConfigContainer" is the |
Grant Gochnauer commented Also -- the title of this issue is a tad misleading I think. All I have to do is refresh the page so it really doesn't have to be concurrent. It's really just the 2nd time a request is made to the controller, it dies. |
Grant Gochnauer commented Another interesting thing -- if I open the site in Firefox, the first load works but then will consistently fail until I restart the EAR. However, once the site is failing in Firefox, I can load it in IE just fine. It's like my session gets corrupted somehow. |
Juergen Hoeller commented Well, isn't the problem here that you have a session-scoped bean that has the request stored in an instance variable? That request object is bound to be outdated when accessed outside of the original request. I would argue that the bean would have to be request-scoped (or a prototype) when storing the request in an instance variable... That means that you should be able to reproduce the problem by simply manually storing the HttpServletRequest reference in a manually managed session attribute. Juergen |
Grant Gochnauer commented Juergen -- Thanks for the feedback. I agree that accessing the request instance object in the session scoped bean outside of a request would cause this issue. The bean is only used/accessed when making requests to the web controller so as long as a request is being made, I should be able to access the HttpServletRequest object right? Maybe I am misunderstanding something. Thanks |
Juergen Hoeller commented Servlet containers usually create new HttpServletRequest objects per request. We are storing the servlet container's original HttpServletRequest object there in case of a Spring-injected bean. So the request object stored in your session-scoped bean will be outdated by the time that a subsequent request comes in... There is no transparent switch to the then-current new request object, I'm afraid. Juergen |
Grant Gochnauer commented Juergen -- That makes sense. I'm changing the bean to be request scoped and things do seem to be working better!!! Sorry for non-issue. I'm grateful for the clarification. -Grant |
Juergen Hoeller commented I'll consider this as enhancement request for Spring 3.0: Maybe we can do something about the kind of HttpServletRequest object that we inject there; possibly using a proxy that transparently delegates to the current request object. Juergen |
Juergen Hoeller commented As of Spring 3.0 RC1, we generally inject proxies for request and session objects now. This has the advantage of working in singleton beans and serializable beans as well, and to always obtain the current session handle - even in case of invalidation between requests or between multiple calls within the same request. Juergen |
Grant Gochnauer commented Thanks Juergen -- this is fantastic. |
Grant Gochnauer opened SPR-5135 and commented
http://forum.springframework.org/showthread.php?t=59618
I've run into a situation where accessing anything from a HttpServletRequest object in my session scoped bean throws an exception. This happens for example if I try to set an attribute on the request object or get a cookie with WebUtils.
I've found that the problem is easily reproducible when refreshing a page that uses a session scoped bean heavily. If I refresh the page every few seconds it seems to work for a while but eventually I'll still run into a problem where the HttpServletRequest object is "dead". The problem is bad because the only way for the application to recover is to restart the EAR. Requesting the page again just results in the same exception over and over.
The exception I am seeing is:
Caused by: java.lang.NullPointerException
at com.ibm.ws.webcontainer.srt.SRTServletRequest$SRTServletRequestHelper.access$1500(SRTServletRequest.java:2232)
at com.ibm.ws.webcontainer.srt.SRTServletRequest.parseParameters(SRTServletRequest.java:1420)
at com.ibm.ws.webcontainer.srt.SRTServletRequest.getParameter(SRTServletRequest.java:1090)
at javax.servlet.ServletRequestWrapper.getParameter(ServletRequestWrapper.java:203)
at org.springframework.security.wrapper.SavedRequestAwareWrapper.getParameter(SavedRequestAwareWrapper.java:261)
I've also tried removing Spring Security to reduce the variables in this problem but that had no effect.
My session scoped bean makes heavy use of:
@Autowired
private HttpServletRequest request;
Affects: 2.5.5
The text was updated successfully, but these errors were encountered: