JSP 转发与重定向和浏览器 URL
我对 JSP/Servlet 设置以及浏览器 URL 中显示的内容有疑问。 page1.jsp 通过带有“SAVE.do”操作的表单提交到 servlet。 Servlet 希望在保存时将成功消息传递回 page1.jsp。 将消息放入请求中来做到这一点
request.setAttribute("message", "Save Successful");
我通过使用I then call
request.getRequestDispatcher("page1.jsp").forward(req,resp);
,但是,浏览器将显示 http:// /localhost:8080/SAVE.do 而不是 http://localhost:8080/page1.jsp
当我更改转发时使用重定向
response.sendRedirect("page1.jsp");
然后该属性就会丢失。
我想我正在寻找正确的方法来执行此操作,以便在再次加载 page1.jsp 时可以取回该属性,并在浏览器中显示正确的 URL。
I have an issue with a JSP/Servlet set-up and what's getting displayed in the browser URL. page1.jsp submits to the servlet by a form with an action of "SAVE.do". The servlet wants to pass a success message back to page1.jsp on the save. I do this by placing the message in the request using the
request.setAttribute("message", "Save Successful");
I then call
request.getRequestDispatcher("page1.jsp").forward(req,resp);
However, the browser will display the URL of http://localhost:8080/SAVE.do instead of http://localhost:8080/page1.jsp
When I change the forward to a redirect using
response.sendRedirect("page1.jsp");
Then the attribute is lost.
I guess I'm looking for the right way to do this, so that I can get the attribute back when page1.jsp loads again, WITH the right URL displaying in the browser.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正确的 URL 是浏览器提交的 URL。请求首先由 servlet 处理,然后由 JSP 处理,这一事实与浏览器无关。顺便说一句,JSP 很可能位于受保护的文件夹中(如
/WEB-INF
),因为浏览器从不直接向此 JSP 发送请求,而是通过调度程序 servlet 的 URL 发送请求。重定向与转发完全不同。转发意味着:我使用另一个服务器组件来完成我的请求的处理。重定向的意思是:我已经处理完请求,我会要求浏览器去访问另一个URL,从而发出一个新的请求。这个新 URL 可以是完全外部的 URL(例如 google.com)。
在您的情况下,您可能需要应用 post-redirect-get 模式,这样“成功”页面的刷新就不会触发表单的重新提交。但是,如果您想显示动态选择的消息,则必须将其保存到会话中并在第二个请求中检索它。不过,如果您想正确应用 MVC 模式,您的第二个请求也应该通过 servlet。
请注意,大多数 MVC 框架都支持 post-redirect-get 模式。
The right URL is the one the browser submitted to. The fact that the request is first handled by a servlet and then by a JSP is irrelevant to the browser. BTW, the JSP could very well be in a protected folder (like
/WEB-INF
), since the browser never sends a request to this JSP directly, but does through the URL of the dispatcher servlet.A redirect is a totally different thing than a forward. Forward means : I use another server component to finish the handling of my request. Redirect means : I have finished handling the request, and I'll ask the browser to go visit another URL, and thus make a new request. This new URL could be a totally external URL (google.com for example).
In your situation, you might want to apply the post-redirect-get pattern, so that a refresh of the "success" page doesn't trigger the re-submission of the form. But if you want to display a dynamically chosen message, you'll have to save it into the session and retrieve it in the second request. Your second request should go through a servlet as well, though, if you want to apply the MVC pattern correctly.
Note that most MVC frameworks have suppport for the post-redirect-get pattern.