重定向和转发

2019-07-05  本文已影响0人  账号已被注销

重定向:

// 使用response.sendRedirect重定向
public void hello(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.sendRedirect("/");
    }
// 使用redirect:xxx重定向
public String hello(HttpServletRequest request, HttpServletResponse response) throws IOException {
        return "redirect:http://www.hao123.com";
    }
public RedirectView hello(HttpServletRequest request, HttpServletResponse response) throws IOException {
        RedirectView redirectView = new RedirectView();
        redirectView.setUrl("http://www.qq.com");
        return redirectView;
    }

转发:

//使用RequestDispatcher进行转发
public void hello(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.getRequestDispatcher("index").forward(request,response);
    }
上一篇下一篇

猜你喜欢

热点阅读