jsp - 如何在Servlet中发送重定向到JSP页面

标签 jsp redirect servlets

当我在 servlet 中完成处理并且结果有效时,我需要将响应重定向到另一个 JSP 页面,例如 Web 内容文件夹中的 welcome.jsp。我该怎么做?

例如:

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

         // Some processing code here ...

         // How do I redirect to another JSP here when I'm ready?

    } catch (Exception e) {
        throw new ServletException(e);
    }
}

最佳答案

看看 HttpServletResponse#sendRedirect(String location)方法。

将其用作:

response.sendRedirect(request.getContextPath() + "/welcome.jsp")

或者,查看 HttpServletResponse#setHeader(String name, String value)方法。

通过添加位置 header 来设置重定向:

response.setHeader("Location", request.getContextPath() + "/welcome.jsp");

关于jsp - 如何在Servlet中发送重定向到JSP页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57625972/

相关文章:

java - 有没有办法让 AIR 1.5 应用程序从 stdin 读取并写入 stdout/stderr?

java - Servlet Loop 函数调用直到 while 循环的结果

java - 无法获取从jsp到servlet的传递值

java - 数组在浏览器中给出 null 作为输出

redirect - nginx 从客户端浏览器缓存中清除 301 永久

ASP.NET customErrors with mode=remoteOnly 和 global.asax 处理异常

servlets - "Refused to display document because display forbidden by X-Frame-Options."编辑标题

java - 如何在servlet中获取html请求URL

java - 如何在 HTML.form 中添加 id 到操作

jquery 星级评定在表中不起作用