1.最简单的方式(注解法)
@Autowired
private HttpServletRequest request;
2.最麻烦的方法
a. 在web.xml中配置一个监听
org.springframework.web.context.request.RequestContextListener
b.之后在程序里可以用
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
3.最直接的方法
public String hello(HttpServletRequest request,HttpServletResponse response)
springMVC获取request很简单,直接在方法入参中使用HttpServletRequest request参数就可以了
从容器RequestContextHolder中获取,spring中很多都是通过各种容器获取,如果用springsecurity的话,登录信息会放置到安全容器中