您好,欢迎访问代理记账网站
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

Spring的Filter、服务器间跳转和客户端跳转通过示例得到的相关总结

先look look演示代码:

package com.test;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "myFilter1", urlPatterns = "/*")
public class TestFilter implements Filter {


    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        System.out.println(((HttpServletRequest)servletRequest).getRequestURI() + " start");
        filterChain.doFilter(servletRequest, servletResponse);
//        ((HttpServletResponse)servletResponse).getWriter().print("111");
        System.out.println("返回到Filter,是否已提交:" + servletResponse.isCommitted());
        System.out.println(((HttpServletRequest)servletRequest).getRequestURI() + " end");
    }
}
package com.test;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("test")
public class TestController {


    @RequestMapping("/a")
    public void a(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.getRequestDispatcher("/test/b").forward(req, resp);
    }

    @RequestMapping("/b")
    public String b(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("服务器间跳转后,是否已提交:" + resp.isCommitted());
        return "b";
    }

    @RequestMapping("/c")
    public void c(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.sendRedirect("/test/d");
    }

    @RequestMapping("/d")
    public String d(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("客户端跳转后,是否已提交:" + resp.isCommitted());
        return "d";
    }

    @RequestMapping("/e")
    public String e(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        return "e";
    }

    @RequestMapping("/f")
    public void f(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.sendRedirect("/test/g");
    }

    @RequestMapping("/g")
    public void g(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.sendRedirect("/test/h");
    }

    @RequestMapping("/h")
    public String h(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        return "h";
    }

    @RequestMapping("/i")
    public void i(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.getRequestDispatcher("/test/j").forward(req, resp);
    }

    @RequestMapping("/j")
    public void j(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.getRequestDispatcher("/test/k").forward(req, resp);
    }

    @RequestMapping("/k")
    public String k(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        return "k";
    }
}

访问/test/a,打印如下:

访问/test/c,打印如下:

访问/test/e,打印如下:

访问/test/f,打印如下:

访问/test/i,打印如下:

得到如下总结:

Spring的请求处理方法可服务器间跳转和客户端可跳转3次以上
Spring的请求处理方法返回到Filter后response已commit,不能再修改响应头和响应体
Spring的请求处理方法无论服务器间跳转多少次,都只以第一次请求的URL来回经过一次Filter
Spring的请求处理方法每次客户端跳转,都会来回经过一次Filter
Spring的请求处理方法无论服务端跳转还是客户端跳转,跳转后response都未commit


分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进