如何使用JSP重定向请求并转发路径?
示例:
my-domain.com/abc to www.other-domain.com/abc
其中"abc"将是请求路径。还可以转发查询参数和片段标识符?
不能重定向片段标识符,因为它们没有传递到服务器。
要在JSP中做到这一点,您需要以下内容:
<%
response.sendRedirect("http://www.other-domain.com" +
request.getContextPath() +
request.getServletPath() +
(request.getQueryString() == null ?
"" :
"?" + request.getQueryString())
%>
使用HttpServlet响应:
response.sendRedirect(location)