如何在 servlet 中更改用户浏览器中的 url 而无需客户端重定向
我想从一个页面转发到另一页面,但我希望更改 url。假设用户在这里http://mywebsite/register,当他完成注册过程时,我希望在他的地址栏中显示这个< a href="http://mywebsite/home" rel="nofollow">http://mywebsite/home
是否可以不使用 sendRedirect ,我的意思是仅服务器端转发?或者有其他方法解决这个问题吗?
I want to forward from one page to another but with the same I want url to be changed. Suppose user is here http://mywebsite/register and when he completes his registration process then I want this in his address bar http://mywebsite/home
Is it possible without using sendRedirect , I mean by the way server side forwarding only? or any other way around to this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以让 HTML 表单直接提交到该 URL。
但这没有意义。在验证失败的情况下重新显示带有验证消息的同一表单时,您还会遇到问题。如果您打算保留原始 URL,并且需要在会话范围而不是请求范围中存储消息,则需要重定向回原始页面,因为重定向基本上会创建一个全新的请求。每当最终用户在提交表单后按 F5 时,如果没有重定向,您也会遇到“双重提交”问题。
只需让 servlet 将成功的 POST 请求重定向到所需的 URL 即可。这是规范的方法。更重要的是,这是一个推荐的“设计模式”:POST-Redirect-GET 模式。
You could just let the HTML form submit to that URL directly.
But this makes no sense. You'll also run into problems when redisplaying the same form with validation messages in case of validation failure. You'd need to redirect back to the original page if you intend to keep the original URL and you'd need to fiddle with storing messages in the session scope instead of the request scope because a redirect basically creates a brand new request. You'll without a redirect also run in "double submit" problem whenever the enduser presses F5 after submitting the form.
Just let the servlet redirect the successful POST request to the desired URL. That's the canonical approach. Even more, this is a recommend "design pattern": the POST-Redirect-GET pattern.
AFAIK 没有办法绕过重定向,因为浏览器必须在某个时候更新 url。如果您在加载转发到的页面后更新 url,则会发出刷新,并且页面将再次加载(这可能会导致无限循环)。
在这种情况下为什么不想使用重定向呢?
AFAIK there's no way around a redirect since the browser has to update the url at some point. And if you'd update the url after the forwarded to page has been loaded it would issue a refresh and the page would be loaded again (which might result in an endless loop).
Why don't you want to use a redirect in that case?