Java中如何获取HttpServletRequest、HttpServletResponse呢?
下文笔者讲述Spring开发中获取HttpServletRequest、HttpServletResponse的方法及示例分享,如下所示
获取HttpServletRequest、HttpServletResponse的几种方式
1.可封装为静态方法
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); HttpServletRequest request = servletRequestAttributes.getRequest(); HttpServletResponse response = servletRequestAttributes.getResponse();
2.controller中获取request和response的方法
@GetMapping(value = "") public String doSomething(HttpServletRequest request,HttpServletResponse response) { //... }
3.直接注入
@Autowired private HttpServletRequest request; @Autowired private HttpServletResponse response;
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。