可以使用范围为“request”的对象吗?请求处理完成后是否可以访问?
我问这个问题是因为在我的工作实践评估中提出了一个关于 JSP 中具有“请求”范围的对象的问题。这是问题,请告诉我哪个是正确答案:
以下关于范围的陈述哪一项是正确的 JSP 中的请求?
• 具有请求范围的对象可从处理请求范围的页面访问 创建它们的请求相同。
• 处理请求后,应释放对该对象的所有引用;特别是,如果请求转发到同一运行时的资源,该对象仍然可达。
• 对具有请求范围的对象的引用存储在请求对象中
• 以上全部。
答案是“以上全部”,对吗?然而,模拟测试给出的答案却显示这只是第一个。但是,如果您将请求转发或包含在其他 JSP 或 servlet 中,那么此范围的对象仍然可以访问,这不是也是事实吗?而且,具有“请求”范围的对象存储在“请求对象”(即 ServletRequest 或其派生类之一)中,这难道不是真的吗?
我还缺少其他东西吗,比如在请求处理完成后的一段时间内,请求范围的对象仍然可以访问?或者说,这里有什么错误吗?
I ask this, because on the practice assessment for my work asks a question regarding objects in JSP that have a scope of "request". Here's the question, tell me which one is the right answer:
Which of the following statements is true regarding the scope of a
request in JSP?• Objects with request scope are accessible from pages processing the
same request where they were created.• All references to the object shall be released after the request is processed; in particular, if the request is forwarded to a resource in the same runtime, the object is still reachable.
• References to objects with request scope are stored in the request object
• All of the above.
The answer is "All of the above" right? However, the answer given on the practice test says it's just the first one. But, isn't it also true that the if you forward or include the request in some other JSP or servlet, then objects of this scope are still accessible? And, isn't it also true that objects with a scope of "request" are stored in the "request object" (ie. the ServletRequest or one of its derived classes)?
Is there something else that I am missing, like maybe the objects of request scope are still accessible for some time after the request has finished being processed? Or, is there a mistake here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为练习测试的答案是错误的。诸如 bean 之类的对象存储在请求中,可以使用 HttpRequest 对象的 getAttribute 方法进行访问。因此,对象会一直存在,直到请求结束。
来源:The HttpRequest/ServletRequest javadoc,以及 David Parsons 的《使用 XML 和 Java 进行动态 Web 应用程序开发》第 10 章,其中有一个使用请求范围的 bean 和转发的示例。
I think that the practice test answer is wrong. Objects such as beans are stored in the request and can be accessed with the HttpRequest object's getAttribute method. Therefore the objects live on until the end of the request.
Source: The HttpRequest/ServletRequest javadoc, and Dynamic Web Application Development using XML and Java by David Parsons, Chapter 10, which has an example that uses a request-scoped bean and forwards.