记得在一篇博文中看到描述threadLocal的一句话:
ThreadLocal除了适用于多线程保证每条线程都有自己的变量副本外,还适用于在线程上下文中共享某些变量值。
这两种说法是有区别的。前者强调的是,使用ThreadLocal对副本做保护,避免同步、加锁,降低效率;后者强调的是,某个变量线程上下文中,A处用到、B处用到、C处用到,先在入口处set一个值,后使用ThreadLocal的get方法直接在需要用到的地方拿这个值。
项目中,最近理由cookie存值,使用到了threadLocal这个字段,自己就想去研究下,原理这里跟后者强调的一样,上代码:
1.web.xml里边配置过滤器,拦截请求,做处理
InterceptorFilter com.fx.anniversary.core.filter.InterceptorFilter InterceptorFilter public class WebContextFilter extends oncePerRequestFilter { public WebContextFilter() { } protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { if (request == null || response == null) { return; } else { WebContextHolder.setRequest(request); WebContextHolder.setResponse(response); filterChain.doFilter(request, response); return; } } }
3.然后我们将写好的拦截器配置到web.xml中:
Archetype Created Web Application contextConfigLocation encodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceEncoding true encodingFilter @SuppressWarnings({ "rawtypes", "unchecked" }) public class WebContextHolder { public WebContextHolder() { } public static String getRequestIp() { if (getRequest() == null) return null; HttpServletRequest request = getRequest(); String ip = request.getHeader("X-Forwarded-For"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) ip = request.getHeader("Proxy-Client-IP"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) ip = request.getHeader("WL-Proxy-Client-IP"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) ip = request.getHeader("HTTP_CLIENT_IP"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) ip = request.getHeader("HTTP_X_FORWARDED_FOR"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) ip = request.getRemoteAddr(); } else if (ip.length() > 15) { String ips[] = ip.split(","); int index = 0; do { if (index >= ips.length) break; String strIp = ips[index]; if (!"unknown".equalsIgnoreCase(strIp)) { ip = strIp; break; } index++; } while (true); } return ip; } public static HttpServletRequest getRequest() { if (requestLocal == null) return null; else return (HttpServletRequest) requestLocal.get(); } public static String getContextPath() { if (getRequest() == null) return null; else return (new StringBuilder()).append(getRequest().getContextPath()).append("/").toString(); } public static String getCurrRequestURI() { if (getRequest() == null) return null; else return (new StringBuilder()).append(getRequest().getRequestURI().replace(getRequest().getContextPath(), "")) .append("/").toString(); } public static HttpServletResponse getResponse() { if (responseLocal == null) return null; else return (HttpServletResponse) responseLocal.get(); } public static HttpSession getSession() { if (requestLocal == null) return null; if (requestLocal.get() == null) return null; else return ((HttpServletRequest) requestLocal.get()).getSession(); } public static UserDTO getLoginUserSession(Class loginUserClass) { if (getSession() == null) return null; Object obj = getSession().getAttribute(CURRENT_USER); if (obj == null) return null; else return (UserDTO) obj; } public static UserDTO getLoginUserSession() { return getLoginUserSession(UserDTO.class); } public static void createLoginUserSession(UserDTO loginUser) { if (loginUser != null) getSession().setAttribute(CURRENT_USER, loginUser); } public static void destroyLoginUserSession() { if (getLoginUserSession() != null) { getSession().removeAttribute(CURRENT_USER); getSession().invalidate(); } } public static void setRequest(HttpServletRequest request) { if (requestLocal == null) requestLocal = new ThreadLocal(); requestLocal.set(request); } public static void setResponse(HttpServletResponse response) { if (responseLocal == null) responseLocal = new ThreadLocal(); responseLocal.set(response); } 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的用法说明就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。



