美文网首页
重定向和转发

重定向和转发

作者: 账号已被注销 | 来源:发表于2019-07-05 21:57 被阅读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);
        }
    

    相关文章

      网友评论

          本文标题:重定向和转发

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