重定向后访问请求范围内的自定义对象的 HashMap
我有一个自定义对象的 HashMap
,使用 RequestDispatcher
传递到 JSP,并且我能够使用 JSTL 访问该对象及其属性。
但是,如果使用 response.sendRedirect()
发送参数,则代码会失败。
我不确定原因是什么以及如何使其发挥作用?
I have a HashMap
of custom objects being passed to a JSP using RequestDispatcher
and I am able to access the object and its properties using JSTL.
However the code fails in case the parameter is sent using response.sendRedirect()
.
I am not sure what the reason is and how to make it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
response.sendRedirect()
基本上指示客户端(网络浏览器)在给定 URL 上发送新请求。您还会看到浏览器地址栏中的更改反映了这一点。新请求当然不包含先前(或任何其他)请求的属性。否则就会破坏“请求范围”的整个概念。
要预处理 GET 请求,您需要在 Servlet 的
doGet()
方法中执行此操作,然后重定向到该 Servlet 的 URL。例如
,
请注意,此问题与请求范围内的自定义对象的哈希映射没有任何关系。
另请参阅:
The
response.sendRedirect()
basically instructs the client (the webbrowser) to send a new request on the given URL. You'll also see this being reflected by a change in the browser address bar.A new request does of course not contain the attribtues of the previous (or any other) request. That would otherwise have broken the whole concept of "request scope".
To preprocess a GET request, you need to do the job in
doGet()
method of a servlet and then redirect to the URL of that servlet instead.E.g.
and
Note that this problem is in no way related to having a hashmap of custom objects in the request scope.
See also:
您无法在response.sendRedirect 中共享请求属性,因为它会创建新请求。
但是,如果您想要该 HashMap,在 response.sendRedirect 中,您可以将其放入会话中
,并可以在 servlet 和 JSP 之间共享。这适用于 RequestDispatcher 和 sendRedirect。
You can not share a request attribute in response.sendRedirect as it creates a new request.
But, if you want that HashMap, in response.sendRedirect, you can put that in session like
and can share between the servlet and JSP. This works in both RequestDispatcher and sendRedirect.