如何将Servlet重定向到给定的URL [英] How to redirect Servlet to given URL

查看:92
本文介绍了如何将Servlet重定向到给定的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在使用

request.getRequestDispatcher("thePage.html").forward(request, response);

在我的servlet中向用户提供新页面.但是,该servlet的URL保留在浏览器地址栏中.我希望目标页面的URL显示在浏览器地址栏中,而不是初始Servlet URL.我该怎么做?

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?

推荐答案

您可以执行response.sendRedirect("thePage.html"),但是随后需要从Internet直接访问该页面.特别是,可以直接访问它,而无需先进入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).

根据您要尝试执行的操作,您可能还应该查看Servlet过滤器以及将任何名称(包括"thePage.html"和路径前缀)与Servlet关联的可能性.

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.

这篇关于如何将Servlet重定向到给定的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆