jsp 重定向无效

jsp Redirect not working

我正在使用以下 servlet 代码发送针对特定 URL 的响应。

public class CasRedirectServlet extends HttpServlet{
    public void service(HttpServletRequest req, HttpServletResponse res)
            throws IOException, ServletException{
        System.out.println("CasRedirectServlet Called == " + TbuyHelper.getDashboardLocation());
        RequestDispatcher rd = req
                .getRequestDispatcher("/test.jsp");
        rd.include(req, res);
    }

test.jsp

代码
<%@ page import="tbuy.tbuyHelp"%>

<% response.sendRedirect("banana/index.html#/dashboard/file/tbuy.json"); %>

现在,我看到显示的是空白 test.jsp..

给定 URL 的 response.sendredirect 无效。

根据 RequestDispatcher.include 方法的文档

The included servlet cannot change the response status code or set headers; any attempt to make a change is ignored.

response.sendRedirect 通过发送 302 状态和位置 header 来工作。所以你在 test.jsp 中的重定向被忽略了

编辑: 看看 forward 方法,它可能比 include 更合适。