CookieUtil

package com.wxj.common;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
 * Cookie 工具类
 */
public class CookieUtil {
  /*
   * 得到cookie值,不编码
   */
	
	public static String getCookieValue(HttpServletRequest request,String cookieName){
		return getCookieValue(request,cookieName,false);
	}
	
	
	/*
	 * 得到cookie值
	 */
	public static String getCookieValue(HttpServletRequest request,String cookieName,boolean isDecoder){
		Cookie[] cookieList = request.getCookies();
		if(cookieList == null || cookieName == null){//如果cookie里为空或者cookieName为空,返回空
			return null;
		}
		String retValue = null;
		try {
		
			
			for(int i=0;i<cookieList.length; i++){
			   if(cookieList[i].getName().equals(cookieName)){
				   if(isDecoder){
					   retValue = URLDecoder.decode(cookieList[i].getValue(),"UTF-8");
				   }else{
					   retValue = cookieList[i].getValue();
				   }
				   break;
			   }
		   }
		
		
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return retValue;
	}
	
	/*
	 * 得到cookie值
	 */
	
	public static String getCookieValue(HttpServletRequest request, String cookieName,String encodeString){
		
        Cookie[] cookieList  = request.getCookies();
        if(cookieList == null || cookieName == null){
        	return null;
        }
		
        String retValue = "";
        try{
        	for(int i=0;i<cookieList.length;i++){
        		if(cookieList[i].getName().equals(cookieName)){
        			retValue = URLDecoder.decode(cookieList[i].getValue(), encodeString);
        			break;
        		}
        	}	
        	
        }catch(UnsupportedEncodingException e){
        	e.printStackTrace();
        }	
		return retValue;
	}
	/*
	 * 设置cookie值   不设置生效时间默认浏览器关闭即失效,也不编码
	 */
	public static void setCookie(HttpServletRequest request,HttpServletResponse response,String cookieName,String cookieValue){
		setCookie(request,response,cookieName,cookieValue,-1);
	}

	/*
	 * 设置cookie值  指定时间生效,不 编码
	 */
	public static void setCookie(HttpServletRequest request,HttpServletResponse response,String cookieName,String cookieValue,int cookieMaxage){
		 setCookie(request,response,cookieName,cookieValue,cookieMaxage,false);
	}
	
	
	/*
	 * 设置cookie值  不设置cookie生效时间,但编码
	 */
    public static void setCookie(HttpServletRequest request,HttpServletResponse response,String cookieName,String cookieValue,boolean isEncode){
		setCookie(request,response,cookieName,cookieValue,-1,isEncode);
	}
   
    /**
     * 设置Cookie的值 在指定时间内生效, 编码参数
     */
    public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,String cookieValue, int cookieMaxage, boolean isEncode) {
        doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, isEncode);
    }
    
    /*
	 * 设置cookie值  在指定时间内生效,编码参数(指定编码参数)
	 */
    public static void setCookie(HttpServletRequest request,HttpServletResponse response,String cookieName,String cookieValue,int cookieMaxage,String encodeString){
		doSetCookie(request,response,cookieName,cookieValue,cookieMaxage,encodeString);
	}
    
    /*
     * 删除cookie 
     */
    public static void deleteCookie(HttpServletRequest request,HttpServletResponse response,String cookieName){
    	doSetCookie(request,response,cookieName,"",-1,false);
    }
    
    
    /*
     * 设置cookie值。并使其在指定时间内有效
     * 
     * cookieMaxage cookie生效的最大秒数
     */
    private static final void doSetCookie(HttpServletRequest request,HttpServletResponse response,String cookieName,String cookieValue,int cookieMaxage,boolean isEncode){
    	
    	try {
			if(cookieValue == null){
				cookieValue = "";
			}else if(isEncode){
				cookieValue = URLDecoder.decode(cookieValue,"UTF-8");
			}
			
			Cookie cookie = new Cookie(cookieName,cookieValue);
			
			if(cookieMaxage>0){
				cookie.setMaxAge(cookieMaxage);
			}
			if(null != request){//设置域名
				String domainName = getDomainName(request);
				System.out.println(domainName);
				if(!"localhost".equals(domainName)){
					cookie.setDomain(domainName);
				}
				
				cookie.setPath("/");
				response.addCookie(cookie);
			}
	
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	
    }
    
    
    private static void doSetCookie(HttpServletRequest request,HttpServletResponse response,String cookieName,String cookieValue,int cookieMaxage,String encodeString){
    	
           try {
			if(cookieValue == null){
				   cookieValue = "";
			   }else{
				   cookieValue = URLEncoder.encode(cookieValue,encodeString);
			   }
			
			Cookie cookie = new Cookie(cookieName,cookieValue);
			if (null !=request){
				String domainName = getDomainName(request);
				System.out.println(domainName);
				
				if(!"localhost".equals(domainName)){
					cookie.setDomain(domainName);
				}
			}
			
			cookie.setPath("/");  //设置cookie在同一应用服务器下共享
			response.addCookie(cookie);
	
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    /*
     * 获取域名
     */
    public static final String getDomainName(HttpServletRequest request){
    	String domainName = null;
    	
    	String serverName = request.getRequestURI().toString();
    	
    	if(serverName == null || serverName.equals("")){
    		domainName = "";
    	}else{
    		serverName = serverName.toLowerCase();
    		serverName = serverName.substring(7);//将http://去掉
    		final int end = serverName.indexOf("/");//获得第一个“/”的索引号
    		serverName = serverName.substring(0, end);//例获得是localhost:8080 substring前包后不包
    		
    		//判断是否为域名,或者localhost之类
    		final String[] domains = serverName.split("\\.");//以“.”进行分割
    		
    		int len = domains.length;
    		
    		if(len>3){
    			//www.xxx.com.cn
    			domainName = "." +domains[len-3]+"."+domains[len-2]+"."+domains[len-1];
    		}else if(len<= 3 && len >1){
    			//xxx.com or xxx.cn
    			domainName = "."+domains[len-2] + "."+domains[len -1];
    		}else{
    			//有可能是localhost:8080这种形式,数组长度为0,直接把localhost:8080赋值给domainName
    			domainName = serverName;
    		}
    	}
    	//domainName 的值是localhost:8080,符合不为null并且包含“:”
    	
    	if(domainName != null && domainName.indexOf(":")>0){
    		//按“:”分组,得到的数组是["localhost","8080"]
    		String[] ary = domainName.split("\\:");
    		domainName = ary[0];
    	}
    	//返回localhost
    	return domainName;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RabbitMQ中常见的交换机有四种:直连交换机(Direct Exchange)、主题交换机(Topic Exchange)、扇形交换机(Fanout Exchange)和头交换机(Headers Exchange)。 1. 直连交换机(Direct Exchange): 直连交换机是最简单的交换机类型。它根据消息的路由键(Routing Key)将消息发送给与之匹配的绑定队列。如果消息的路由键与绑定队列的路由键完全匹配,则消息将被发送到该队列。适用于一对一的消息传递。 2. 主题交换机(Topic Exchange): 主题交换机根据消息的路由键和绑定队列的模式进行匹配。路由键可以使用通配符进行匹配,支持两种通配符:*(匹配一个单词)和#(匹配零个或多个单词)。可以实现灵活的消息路由和过滤。 3. 扇形交换机(Fanout Exchange): 扇形交换机将收到的消息广播到所有绑定到它上面的队列。它忽略消息的路由键,直接将消息发送到所有绑定的队列。适用于一对多的消息传递,例如广播消息。 4. 头交换机(Headers Exchange): 头交换机根据消息的头部属性进行匹配,而不是路由键。消息头部包含一组键值对,可以根据键值对的匹配规则将消息发送给绑定队列。适用于复杂的消息匹配规则。 总结: - 直连交换机根据路由键完全匹配进行消息路由。 - 主题交换机根据路由键模式进行通配符匹配。 - 扇形交换机广播消息给所有绑定的队列。 - 头交换机根据消息头部属性进行匹配。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值