开发者

request.getSession() vs getThreadLocalRequest().getSession()

What is the difference between

request.getSession() 

and

getThreadLocalRequest().getSession()

The application I am maintaining appears to use the first f开发者_开发问答or straight Servletsand the second for anything implemented via GWT-RPC which itself extends servlet.


They both return the same thing. GWT simply stores the request sent in from the servlet into a thread local so that you don't need to pass it around in every method call and still have a separate request for each invocation.


getThreadLocalRequest() is convenience method to get the HttpServletRequest.

Both request.getSession() and getThreadLocalRequest().getSession() returns the same HttpSession the difference is the way you obtain the HttpServletRequest.


The main reason is that you use your own RPC methods in your GWT Servlet which do not get the HTTPRequest as a parameter - in contrast to the standard Servlet methods doGet(...), ..., doXYZ(...). Thus, the only way to access the HTTPRequest is the provided getThreadLocalRequest() of GWT's RemoteServiceServlet which you should normally extend.


The difference is scope. Specifically, request variable is only available directly from the doGet(..), doPost(..), etc. methods scopes (inside the methods). Once the thread you are in exits the method and enters your biz method doSomething(), etc., your code has no access to the request variable anymore (scope changed), but getThreadLocal..() allows you to gain the access regardless of the method you are in, given of course you are in the same thread as the doGet(), etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜