java获取当前request_Java 获取Request,Response对象方法

第一种、参数

@RequestMapping("/test")

@ResponseBodypublic void saveTest(HttpServletRequest req, HttpServletResponse resp){

}

第二种、注解

@Autowired  private  HttpServletRequest request;

第三种、上下文获取

1、在web.xml配置监听器

org.springframework.web.context.request.RequestContextListener

2、程序使用(比较重要,能够凭空获取)

HttpServletRequest request= ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();

HttpServletResponse response  = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();

HttpServletResponse response = ((ServletWebRequest) RequestContextHolder.getRequestAttributes()).getResponse();

参考:https://blog.csdn.net/wzy18210825916/article/details/89173846?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1

有补充

你可能感兴趣的:(java获取当前request)