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

后端 未结 8 733
情歌与酒
情歌与酒 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:27

    I'm not 100% sure what was the intention of the author of the code you'd once came across, but I guess the idea there is that ServletRequest instance is available from any method in the code without passing it as parameter or setting as instance variable. Usually ThreadLocal variable is static and there is a method exposed that allows to obtain the instance of ServletRequest in statically. For instance you could access ServletRequest in Struts FromBeans easily using this technique.

提交回复
热议问题