java - sendRedirect() 不起作用

标签 java jsp servlets

我在这个论坛上寻找解决方案,但没有找到适合我的问题的解决方案。

我有一个非常简单的代码

一个jsp页面

<html>
<body>
<jsp:include page="/servletName"/>
</body>
</html>

和一个servlet

@WebServlet("/servletName")
public class reindirizzaController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public reindirizzaController() {
        super();        
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      String redirectURL = "http://www.google.it";
      response.sendRedirect(redirectURL);       
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

}

没有重定向。我被困在 jsp 页面中,没有收到任何错误。我还尝试在响应后添加 return;

最佳答案

因为您是通过Include 调用servlet,所以它不会让您重定向。它只是忽略。

来自 include() 的文档

includes the content of a resource (servlet, JSP page, HTML file) in the response. In essence, this method enables programmatic server-side includes.

The ServletResponse object has its path elements and parameters remain unchanged from the caller's. The included servlet cannot change the response status code or set headers; any attempt to make a change is ignored.

关于java - sendRedirect() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25140571/

相关文章:

java - 如何将隐藏字段数组发送到 servlet?

jsp - tomcat - 它们之前运行的类仍在内存中加载

java - 高级搜索java代码

jquery - 如何在 JAVA 中创建具有链接、图像、列表等高级功能的评论框

java - 加载 *.so 文件到 Websphere

javascript - 如何关闭带有链接或按钮等的html页面?

java - 让 Tomcat 使用静态上下文重新加载 Web 应用程序

java - 为什么post请求会出现禁止错误?

java - 尽管有 web.ignoring().antMatchers(),为什么我的过滤器仍在该端点上被调用?

java - 从 Glassfish 服务器上的 FoxPro 数据更新 sql 数据库