ServletUtils.getRequest() returns null

573 views
Skip to first unread message

Andre

unread,
Nov 16, 2010, 3:24:25 AM11/16/10
to gwt-sl
Hi,

i'm using gwt-sl and now i need the HttpServletRequest object
the method ServletUtils.getRequest() returns null although it is
actually used in the dispatching thread

i use an extended version of org.gwtwidgets.server.spring.GWTHandler:
GWTSecuredHandler from http://code.google.com/p/gwt-incubator-lib/ for
spring security

so why do i get null instead of the valid HttpServletRequest

anybody an idea?

thx in advance
andre

George Georgovassilis

unread,
Nov 16, 2010, 3:48:30 AM11/16/10
to gwt-sl
Moin Andre,

I had a brief look at the GWTSecuredhandler but couldn't quite see
what it is doing because of some runtime transforming of the handler -
that is as far a static analysis can go :-( That left aside:

First thing to check that the handler is declared in a *-servlet.xml
equivalent and not in the application-context.xml

If that still doesn't help you can always populate the ServletUtils
with an interceptor [1]


[1] Spring 3.0 reference, chapter 15.4
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html

Andre

unread,
Nov 16, 2010, 5:36:27 AM11/16/10
to gwt-sl
Moin George,

thanks fpr fast reply

actually the handler is declared in a *-servlet.xml file (springrpc-
servlet.xml)
very strange thing, but like described in an issue of the gwt-
incubator project, ServletUtils.getRequest()
should work just fine, see comment #2 http://code.google.com/p/gwt-incubator-lib/issues/detail?id=4

looking at he source code of the gwt-incubator and gwt-sl i can not
see something wrong

but still null is returned :-( very curious...

George Georgovassilis

unread,
Nov 16, 2010, 5:51:11 AM11/16/10
to gwt...@googlegroups.com
Hello Andre,

Some background on how ServletUtils works:

It declares a thread local variable (meaning its value is unique to a
servlet request) in which the SL code stores the current
HttpServletRequest and HttpServletResponse. The GWTSecuredHandler
overrides the default SL behaviour and doesn't set the request and
response in ServletUtils.

May I suggest that you extend the GWTSecuredHandler (not tested) like this:

public class MyGWTSecuredHandler extends GWTSecuredHandler{

@Override
protected Object
getHandlerInternal(HttpServletRequestrequest)throwsException{

ServletUtils.setRequest(request);
return super.getHandlerInternal(request);

Julian Scheid

unread,
Nov 16, 2010, 6:08:47 AM11/16/10
to gwt...@googlegroups.com
On Tue, Nov 16, 2010 at 9:24 PM, Andre <a.re...@web.de> wrote:
> so why do i get null instead of the valid HttpServletRequest

I'm using the below code and it works fine for me. I don't recall if
this requires any additional configuration in web.xml or elsewhere but
I don't think so.

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;

HttpServletRequest request =
((ServletRequestAttributes)
RequestContextHolder.getRequestAttributes()).getRequest();

Andre

unread,
Nov 16, 2010, 9:15:51 AM11/16/10
to gwt-sl
@Julian: thanks a lot for the reply!
it works this way for me

@George: your suggestion to extend GWTSecuredHandler works, but IMHO
not really elegant...
after processing somewhere the request should be "cleaned" over
ServletUtils.setRequest(null);
what is about HttpServletResponse, if i need it later on?

the worst thing is, i can not understand, why the simple call
"ServletUtils.getRequest" is not working for me out of the box...

George Georgovassilis

unread,
Nov 16, 2010, 9:28:05 AM11/16/10
to gwt...@googlegroups.com
Hello Andre,

Julian's answer (using the RequestContextHolder) is definitely
preferable as it uses Spring's own functionality and does not tie your
service to the SL API - according to the javadocs you should be able to
get the response in addition to the request provided you use Spring 3.0
(some getters seem to be missing in 2.x). As I wrote earlier in this
thread, the reason lies with the GWTSecuredHandler which doesn't
populate ServletUtils.

Reply all
Reply to author
Forward
0 new messages