栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

关于ThreadLocal对request和response的用法说明

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

关于ThreadLocal对request和response的用法说明

记得在一篇博文中看到描述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的用法说明就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/132499.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号