java request.get/setAttribute() 与 this.getServletContext().get/setAttribute()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5232846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
request.get/setAttribute() vs this.getServletContext().get/setAttribute()
提问by Evgeni Dimitrov
what's the difference between get/setAttribute()
when you call them from request and from getServletContext()
.
I noticed that you need
get/setAttribute()
当您从 request 和 from 调用它们时有什么区别getServletContext()
?我注意到你需要
RequestDispatcher rd = request.getRequestDispatcher("/view.jsp");
rd.forward(request, response);
for the request to work, but you just need to navigate to another jsp or servlet in the application to use getServletContext().getAttribute()
.
使请求生效,但您只需要导航到应用程序中的另一个 jsp 或 servlet 即可使用getServletContext().getAttribute()
.
But i don't understand what is going on behind.
但我不明白后面发生了什么。
回答by BalusC
The request.setAttribute()
sets an attribute in the request scope and is thus only available within the same request/response cycle. The servletContext.setAttribute()
sets an attribute in the application scope and is thus shared among all other requests/sessions. You don't want to do this when it concerns request-specific data, otherwise visitor Y would be able to see data of visitor X.
在request.setAttribute()
设置在请求范围的属性,并且因此仅在同一请求/响应循环内可用。在servletContext.setAttribute()
设置在应用范围的属性和所有其他请求/会话之间因此被共享。当涉及特定于请求的数据时,您不想这样做,否则访问者 Y 将能够看到访问者 X 的数据。
If you want some attribute to survive a redirect by response.sendRedirect()
then the request scope is not suitable since a redirect basically instructs the client (webbrowser) to create a brand new HTTP request. You need to put the data in the session scope by session.setAttribute()
rather than in the application scope (and preferably removeit in the subsequent request if it doen't need to be persistent during the whole session).
如果您希望某些属性在重定向时继续存在,response.sendRedirect()
则请求范围不适合,因为重定向基本上指示客户端(网络浏览器)创建全新的 HTTP 请求。你需要把数据在session范围内session.setAttribute()
,而不是在应用范围(最好取下的后续请求,如果它doen't需要是在整个会议期间持续)。
See also:
也可以看看:
回答by araqnid
The servlet context has a global "application" namespace which is maintained throughout the deployment of the application.
servlet 上下文有一个全局的“应用程序”命名空间,它在应用程序的整个部署过程中都得到维护。
The request has a per-request namespace which is maintained for the lifetime of a single request.
该请求具有每个请求的命名空间,该命名空间在单个请求的生命周期内维护。
So use servletContext.setAttribute() to store things that need to be global in scope, and shared between different requests (and therefore must be threadsafe), and request.setAttribute() to store things relating only to the current request (usually no need to worry about thread safety since a request is usually served by a single thread).
所以使用 servletContext.setAttribute() 来存储需要全局范围内的东西,并在不同的请求之间共享(因此必须是线程安全的),而 request.setAttribute() 来存储仅与当前请求相关的东西(通常不需要担心线程安全,因为请求通常由单个线程提供)。
回答by leo
First, you need to know some scopes. There are session scope, request scope and application scope, page scope. Attribute in different scope will be store in different length of time.
首先,您需要了解一些范围。有会话范围、请求范围和应用范围、页面范围。不同作用域的属性会存储不同的时间长度。
Just like your example, attribute stored in ServletContext is in application scope. In the server application just like tomcat, when the server start, there will be only one instance of ServletContext in memory for each web application. The instance will last untill the server stopped. So the attribute stored in this instance can be use all the life time of the web application. But the instance of request will be build when a http request from client, after server send the response message, it will not exist any longer. So attribute in request can only use in the life of the request.
就像您的示例一样,存储在 ServletContext 中的属性在应用程序范围内。在像tomcat这样的服务器应用中,当服务器启动时,每个web应用的内存中只有一个ServletContext实例。该实例将持续到服务器停止。因此,存储在此实例中的属性可以在 Web 应用程序的整个生命周期中使用。但是请求的实例会在客户端发出http请求时建立,服务器发送响应消息后,它就不再存在了。所以请求中的属性只能在请求的生命周期中使用。
I was just learning java web. If I have some wrong above, I'd like to have a criticize.
我刚刚在学习java web。以上如有不对之处,望批评指正。