RequestContextHolder获取request和response

2019年03月16日 15:18:15 whp404 阅读数:21更多

个人分类: Spring
 

首先需要在web.xml中配置监听器,这个监听器源码如下,实现了ServletRequestListener,则每个请求进来都会被监听

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
// package org.springframework.web.context.request; import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;
import org.springframework.context.i18n.LocaleContextHolder; public class RequestContextListener implements ServletRequestListener {
private static final String REQUEST_ATTRIBUTES_ATTRIBUTE = RequestContextListener.class.getName() + ".REQUEST_ATTRIBUTES"; public RequestContextListener() {
} public void requestInitialized(ServletRequestEvent requestEvent) {
if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) {
throw new IllegalArgumentException("Request is not an HttpServletRequest: " + requestEvent.getServletRequest());
} else {
HttpServletRequest request = (HttpServletRequest)requestEvent.getServletRequest();
ServletRequestAttributes attributes = new ServletRequestAttributes(request);
request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes);
LocaleContextHolder.setLocale(request.getLocale());
RequestContextHolder.setRequestAttributes(attributes);//将request包装的ServletRequestAttributes放入RequestContextHolder
}
} public void requestDestroyed(ServletRequestEvent requestEvent) {
ServletRequestAttributes attributes = (ServletRequestAttributes)requestEvent.getServletRequest().getAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE);
ServletRequestAttributes threadAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
if (threadAttributes != null) {
if (attributes == null) {
attributes = threadAttributes;
} LocaleContextHolder.resetLocaleContext();
RequestContextHolder.resetRequestAttributes(); //将ServletRequestAttributes等资源清除掉
} if (attributes != null) {
attributes.requestCompleted();
} }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

web.xml中配置如下

<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener> spring中获取request 和 response代码 HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); HttpServletResponse resp = ((ServletWebRequest)RequestContextHolder.getRequestAttributes()).g

RequestContextHolder获取request和response的更多相关文章

  1. Spring MVC 中RequestContextHolder获取request和response

    1.最简单方式:处理方法入参 例如: @RequestMapping("/test") @ResponseBody public void saveTest(HttpServlet ...

  2. springMVC中获取request和response对象的几种方式(RequestContextHolder)

    springMVC中获取request和response对象的几种方式 1.最简单方式:参数 2.加入监听器,然后在代码里面获取 原文链接:https://blog.csdn.net/weixin_4 ...

  3. SpringMvc4中获取request、response对象的方法

    springMVC4中获取request和response对象有以下两种简单易用的方法: 1.在control层获取 在control层中获取HttpServletRequest和HttpServle ...

  4. spring MVC中获取request和response:

    spring MVC中获取request和response: HttpServletRequest request = ((ServletRequestAttributes) RequestConte ...

  5. Spring 手动获取request和response

    //获取responseHttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getReque ...

  6. springmvc在使用@ModelAttribute注解获取Request和Response会产生线程并发不安全问题(转)

    springmvc在获取Request和Response有很多方式:具体请看:https://www.cnblogs.com/wade-luffy/p/8867144.html 产生线程问题的代码如下 ...

  7. 在springMVC的controller中获取request,response对象的一个方法

    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttr ...

  8. struts2中获取request、response,与android客户端进行交互(文件传递给客户端)

    用struts2作为服务器框架,与android客户端进行交互需要得到request.response对象. struts2中获取request.response有两种方法. 第一种:利用Servle ...

  9. Struts2,springMVC获取request和response

    springMVC获取request和response1:在BaseController中加入: protected HttpServletRequest request;   protected H ...

随机推荐

  1. php产品细节图多图上传示例代码 无刷新

    前台文件代码 upload.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  2. 开发第一个VUE插件

    背景 项目中用到element-ui,里面用到了弹出组件,但是效果不太满意,于是自己就想写一个简单的弹出组件.目前已经发布到npm:可以通过npm i dialog-wxy -s 进行下载使用页面调用 ...

  3. U盘安装linux(CentOS Kali ubuntu)无法挂载_实测

    最新的Kali linux官方下载地址: https://www.kali.org/downloads/ 刻录软件官网:http://rufus.ie/ 1.打开. 2.插入U盘. 3.自动下载插件. ...

  4. Spring对单例的底层实现,单例注册表

    Spring框架对单例的支持是采用单例注册表的方式进行实现的,源码如下: public abstract class AbstractBeanFactory implements Configurab ...

  5. SpringBoot+Rocketmq

    @PostConstruct:用于在依赖关系注入完成之后需要执行的方法上,以执行任何初始化.此方法必须在将类放入服务之前调用. @PreDestroy:在开发中我们如果要在关闭spring容器后释放一 ...

  6. 转 弹性反向传播(RProp)和均方根反向传播(RMSProp)

    from http://blog.csdn.net/tsq292978891/article/details/78619384 都是一种权值更新算法,类似于SGD算法,其中,RMSProp是RProp ...

  7. Leaflet使用vector tiles 标注label设置

    JS //简单的标注 var marker = L.marker([ 31.2, 114.5 ], { // icon:myIcon }).addTo(map) // 设置label .bindToo ...

  8. CF1260F

    题目大意 一棵树,每个节点的权为L[i]~R[i],一棵树的贡献为\(\sum\limits_{h_{i} = h_{j}, 1 \le i < j \le n}{dis(i,j)}\),其中\ ...

  9. chrome浏览器调试js,结果Sources里面找不到js文件解决办法

        页面出现问题,就debug,这是前端开发工程师最常见的做法,但是有时候,我们打开开发者工具,在sources查找js文件,却发现怎么也找不到,无法设置断点.但是文件在network选项卡里确实 ...

  10. 2018百度之星初赛A轮 度度熊学队列

    注意:刚开始用数组存deque<int> qa[MAX]会爆内存 需要改用map<int, deque<int> > qa优化 不明觉厉 #include<b ...