如何将 Servlet 重定向到给定的 URL
目前,我正在
request.getRequestDispatcher("thePage.html").forward(request, response);
我的 servlet 中向用户使用新页面。但 servlet 的 URL 保留在浏览器地址栏中。我希望在浏览器地址栏中显示目标页面的 URL,而不是初始 servlet URL。我该如何实现这个目标?
Currently, I am using
request.getRequestDispatcher("thePage.html").forward(request, response);
in my servlet to the user the new page. But the URL of the servlet stays in browser address bar. I want the URL of the target page to be shown in browser address bar, instead of the initial servlet URL. How do I accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以执行response.sendRedirect("thePage.html"),但该页面需要可以直接从互联网访问。特别是可以直接访问,不需要先去servlet。它还会产生额外的往返(而转发仅在同一请求响应周期内返回结果)。
根据您想要执行的操作,您可能还应该查看 Servlet 过滤器以及将任何名称(包括“thePage.html”和路径前缀)与 Servlet 关联的可能性。
You can do
response.sendRedirect("thePage.html")
, but then that page needs to be directly accessible from the Internet. In particular, it can be accessed directly without going to the servlet first. It will also incur an additional roundtrip (whereas a forward just returns the result within the same request-response cycle).Depending on what you are trying to do, you should probably also look at Servlet Filters and the possibility to associate any name (including "thePage.html" and path prefixes) to a Servlet.