ThreadLocal to store ServletRequest and Response in servlet: what for?

后端 未结 8 740
情歌与酒
情歌与酒 2020-12-05 21:19

Once I have came across a pattern, where ServletRequest and response objects are put to servlet\'s local ThreadLocal variables. The servlet class h

8条回答
  •  隐瞒了意图╮
    2020-12-05 21:50

    This is really terrible. You should obtain the values you need from the HTTP request/session as soon as you possibly can. you can pass these values in method invocations or Transfer Objects. you should strive to write methods/classes technology-free. if your method/class obtains an http request from ThreadLocal it's a worthless class - it's no longer useful in any any non-http context.

    It's especially shocking to me to see people pull http requests from ThreadLocal in BOs (Business Objects) or DAOs. HTTP requests should never appear in any layer other than the presentation layer of the application.

提交回复
热议问题