request.getSession() 与 getThreadLocalRequest().getSession()
和
request.getSession()
。
getThreadLocalRequest().getSession()
我正在维护的应用程序似乎将第一个用于直接 Servlet,而第二个用于通过 GWT-RPC 实现的任何内容,GWT-RPC 本身扩展了 servlet
What is the difference between
request.getSession()
and
getThreadLocalRequest().getSession()
The application I am maintaining appears to use the first for straight Servletsand the second for anything implemented via GWT-RPC which itself extends servlet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
他们都返回相同的东西。 GWT 只是将从 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() 是获取 HttpServletRequest 的便捷方法。
request.getSession() 和 getThreadLocalRequest().getSession() 都返回相同的 HttpSession,区别在于获取 HttpServletRequest 的方式。
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.
主要原因是您在 GWT Servlet 中使用自己的 RPC 方法,这些方法没有获取
HTTPRequest
作为参数 - 与标准 Servlet 方法doGet(...),...,<代码>doXYZ(...)。因此,访问
HTTPRequest
的唯一方法是 GWT 的RemoteServiceServlet
提供的getThreadLocalRequest()
,您通常应该扩展它。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 methodsdoGet(...)
, ...,doXYZ(...)
. Thus, the only way to access theHTTPRequest
is the providedgetThreadLocalRequest()
of GWT'sRemoteServiceServlet
which you should normally extend.区别在于范围。具体来说,请求变量只能直接从
doGet(..)
、doPost(..)
等方法范围(在方法内部)获取。一旦您所在的线程退出该方法并进入您的 biz 方法doSomething()
等,您的代码将无法再访问 request 变量(范围已更改),但是getThreadLocal.. ()
允许您获得访问权限,无论您使用哪种方法,当然前提是您与doGet()
位于同一线程中,等等。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 methoddoSomething()
, etc., your code has no access to the request variable anymore (scope changed), butgetThreadLocal..()
allows you to gain the access regardless of the method you are in, given of course you are in the same thread as thedoGet()
, etc.