request.get/setAttribute() 与 this.getServletContext().get/setAttribute()
当您从请求调用 get/setAttribute() 和从 getServletContext()
调用它们时,它们之间有什么区别。 我注意到您需要
RequestDispatcher rd = request.getRequestDispatcher("/view.jsp");
rd.forward(request, response);
使请求正常工作,但您只需要导航到应用程序中的另一个 jsp 或 servlet 即可使用 getServletContext().getAttribute() 。
但我不明白后面发生了什么。
what's the difference between get/setAttribute()
when you call them from request and from getServletContext()
.
I noticed that you need
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()
.
But i don't understand what is going on behind.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
request.setAttribute()
在请求范围内设置属性,因此仅在同一请求/响应周期内可用。servletContext.setAttribute()
在应用程序范围内设置一个属性,因此在所有其他请求/会话之间共享。当涉及特定于请求的数据时,您不希望这样做,否则访问者 Y 将能够看到访问者 X 的数据。如果您希望某些属性在
response.sendRedirect()
那么请求范围不合适,因为重定向基本上指示客户端(网络浏览器)创建一个全新的 HTTP 请求。您需要通过session.setAttribute()
将数据放入会话范围内,而不是放在应用程序范围内(如果没有,最好在后续请求中删除它)需要在整个会话期间保持不变)。另请参阅:
The
request.setAttribute()
sets an attribute in the request scope and is thus only available within the same request/response cycle. TheservletContext.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.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 bysession.setAttribute()
rather than in the application scope (and preferably remove it in the subsequent request if it doen't need to be persistent during the whole session).See also:
servlet 上下文有一个全局“应用程序”命名空间,该命名空间在应用程序的整个部署过程中得到维护。
该请求具有每个请求的命名空间,该命名空间在单个请求的生命周期内维护。
因此,使用 servletContext.setAttribute() 来存储需要全局范围的内容,并在不同请求之间共享(因此必须是线程安全的),并使用 request.setAttribute() 来存储仅与当前请求相关的内容(通常不需要担心线程安全,因为请求通常由单个线程提供)。
The servlet context has a global "application" namespace which is maintained throughout the deployment of the application.
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 中的属性位于应用程序范围内。在服务器应用程序中,就像tomcat一样,当服务器启动时,每个Web应用程序在内存中只会有一个ServletContext实例。该实例将持续到服务器停止为止。因此,存储在该实例中的属性可以在 Web 应用程序的整个生命周期中使用。但是request实例会在客户端发起http请求时创建,服务器发送响应消息后,该实例将不再存在。所以请求中的属性只能在请求的生命周期内使用。
我刚刚学习java web。以上如有错误还望批评指正。
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.
I was just learning java web. If I have some wrong above, I'd like to have a criticize.