Java-Spring-获取Request,Response对象

转载自:https://www.cnblogs.com/bjlhx/p/6639542.html

第一种、参数

@RequestMapping("/test")
@ResponseBody
public void saveTest(HttpServletRequest req, HttpServletResponse resp){  
}

第二种、注解

@Autowired  
private  HttpServletRequest request;  

第三种、上下文获取

1、在web.xml配置监听器

<listener>
      <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

2、程序使用

HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
HttpServletResponse resp = ((ServletWebRequest)RequestContextHolder.getRequestAttributes()).getResponse();
posted @ 2018-03-27 15:38  花儿为何那样红  阅读(989)  评论(0编辑  收藏  举报