ThreadLocal如何正确应用于请求和响应处理?
- 内容介绍
- 文章标签
- 相关推荐
本文共计806个文字,预计阅读时间需要4分钟。
在阅读某篇博文时,我发现了一句话关于ThreadLocal的描述:ThreadLocal除了适用于多线程保证每条线程都有自己变量的副本外,还适用于在线程上下文中共享某些变量的值。这两种说法是有区别的。
记得在一篇博文中看到描述threadLocal的一句话:
ThreadLocal除了适用于多线程保证每条线程都有自己的变量副本外,还适用于在线程上下文中共享某些变量值。
这两种说法是有区别的。前者强调的是,使用ThreadLocal对副本做保护,避免同步、加锁,降低效率;后者强调的是,某个变量线程上下文中,A处用到、B处用到、C处用到,先在入口处set一个值,后使用ThreadLocal的get方法直接在需要用到的地方拿这个值。
项目中,最近理由cookie存值,使用到了threadLocal这个字段,自己就想去研究下,原理这里跟后者强调的一样,上代码:
1.web.xml里边配置过滤器,拦截请求,做处理
<filter> <filter-name>InterceptorFilter</filter-name> <filter-class>com.fx.anniversary.core.filter.InterceptorFilter</filter-class> </filter> <filter-mapping> <filter-name>InterceptorFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2.赋值
public class InterceptorFilter implements Filter{ publicvoiddestroy(){ } publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)throwsIOException,ServletException{ HttpServletRequestjava.sun.com/xml/ns/javaee" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="java.sun.com/xml/ns/javaee java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value></param-value> </context-param> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- ******************************************************************* --> <!-- *************** webcontent信息加载到TheadLocal中 ******************** --> <!-- ******************************************************************* --> <filter> <filter-name>webContentFilter</filter-name> <filter-class>com.office.common.filter.WebContextFilter</filter-class> </filter> <filter-mapping> <filter-name>webContentFilter</filter-name> <url-pattern>/*</url-pattern><!-- /* --> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
4.编写一个同一个管理操作工具类:WebContextHolder
package com.office.common.context; import javax.servlet.localhost:8080/projectName */ public static String getProjectRequestRootPath() { HttpServletRequest request = WebContextHolder.getRequest(); StringBuffer sb = new StringBuffer(); sb.append(request.getScheme()). append("://"). append(request.getServerName()). append(":"). append(request.getServerPort()). append(request.getContextPath()). append("/"); return sb.toString(); } private static ThreadLocal requestLocal; private static ThreadLocal responseLocal; public static String CURRENT_USER = "CURRENT_USER"; }
5.使用展示:
//我们可以在任何地方使用这种方法取值
WebContextHolder.getRequest().getParameter("id");
以上这篇关于ThreadLocal对request和response的用法说明就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。
本文共计806个文字,预计阅读时间需要4分钟。
在阅读某篇博文时,我发现了一句话关于ThreadLocal的描述:ThreadLocal除了适用于多线程保证每条线程都有自己变量的副本外,还适用于在线程上下文中共享某些变量的值。这两种说法是有区别的。
记得在一篇博文中看到描述threadLocal的一句话:
ThreadLocal除了适用于多线程保证每条线程都有自己的变量副本外,还适用于在线程上下文中共享某些变量值。
这两种说法是有区别的。前者强调的是,使用ThreadLocal对副本做保护,避免同步、加锁,降低效率;后者强调的是,某个变量线程上下文中,A处用到、B处用到、C处用到,先在入口处set一个值,后使用ThreadLocal的get方法直接在需要用到的地方拿这个值。
项目中,最近理由cookie存值,使用到了threadLocal这个字段,自己就想去研究下,原理这里跟后者强调的一样,上代码:
1.web.xml里边配置过滤器,拦截请求,做处理
<filter> <filter-name>InterceptorFilter</filter-name> <filter-class>com.fx.anniversary.core.filter.InterceptorFilter</filter-class> </filter> <filter-mapping> <filter-name>InterceptorFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2.赋值
public class InterceptorFilter implements Filter{ publicvoiddestroy(){ } publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)throwsIOException,ServletException{ HttpServletRequestjava.sun.com/xml/ns/javaee" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="java.sun.com/xml/ns/javaee java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value></param-value> </context-param> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- ******************************************************************* --> <!-- *************** webcontent信息加载到TheadLocal中 ******************** --> <!-- ******************************************************************* --> <filter> <filter-name>webContentFilter</filter-name> <filter-class>com.office.common.filter.WebContextFilter</filter-class> </filter> <filter-mapping> <filter-name>webContentFilter</filter-name> <url-pattern>/*</url-pattern><!-- /* --> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
4.编写一个同一个管理操作工具类:WebContextHolder
package com.office.common.context; import javax.servlet.localhost:8080/projectName */ public static String getProjectRequestRootPath() { HttpServletRequest request = WebContextHolder.getRequest(); StringBuffer sb = new StringBuffer(); sb.append(request.getScheme()). append("://"). append(request.getServerName()). append(":"). append(request.getServerPort()). append(request.getContextPath()). append("/"); return sb.toString(); } private static ThreadLocal requestLocal; private static ThreadLocal responseLocal; public static String CURRENT_USER = "CURRENT_USER"; }
5.使用展示:
//我们可以在任何地方使用这种方法取值
WebContextHolder.getRequest().getParameter("id");
以上这篇关于ThreadLocal对request和response的用法说明就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

