Response重定向

2019-11-01  本文已影响0人  JayMeWangGL

重定向

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

@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);
    }
}

结果展示

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

猜你喜欢

热点阅读