美文网首页
Response重定向

Response重定向

作者: JayMeWangGL | 来源:发表于2019-11-01 15:52 被阅读0次

    重定向

    使用步骤:
    1、设置状态码为302 setStatus()
    2、设置响应头location setHeader()

    • 更加直接的方式:response.sendRedirect("路径名");
    @WebServlet("/ResponseServlet")
    public class ResponseServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            System.out.println("demo01...");
            //重定向步骤:1.设置状态码为302 2.设置响应头location 为跳转页面路径
            response.setStatus(302);
            response.setHeader("location","/ResponseServlet2");
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request, response);
        }
    }
    
    @WebServlet("/ResponseServlet2")
    public class ResponseServlet2 extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            System.out.println("demo02...");
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request, response);
        }
    }
    

    结果展示

    浏览器访问
    会自动跳转到 控制台输出为
    浏览器抓包

    相关文章

      网友评论

          本文标题:Response重定向

          本文链接:https://www.haomeiwen.com/subject/hmkfbctx.html