1. 程式人生 > >Java 獲取Request,Response物件方法

Java 獲取Request,Response物件方法

第一種、引數

@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 = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();