servlet cookie 操作

class  
{
	public   void writeCookie( HttpResponse response ,String name ,String value ,String path  String time  ) 
	{
		if(value!=null) value = URLEncoder.encode(value,"UTF-8");
		Cookie c = new Cookie(name, value);
		c.setPath(path == null ? "/" : path);
		if(time!=null && !"".equals(time)){
			int cookieTime = Integer.parseInt(time)*60;
			c.setMaxAge(cookieTime);
		}
		if(domain!=null && !"".equals(domain)){
			c.setDomain(domain);
		}
		response.addCookie(c); 
	}

	public   void getCookie( HttpRequest request ,String cookiename     ) 
	{
			
			String value ="" ;
			Cookie cookies[] = null;
			Cookie sCookie = null;
			String sname = null;
		
			cookies = request.getCookies();
			if (cookies != null) {
				for (int i = 0; i < cookies.length; i++) {
					sCookie = cookies[i];
					sname = sCookie.getName();
					if(!sname.toLowerCase().equals(cookiename)){
						continue;
					}
					if(sname.toLowerCase().equals(strParam)){
						value = sCookie.getValue();
						value = URLDecoder.decode(value,"utf-8");
						break;
					}
		
				}
			}
			 return value;


	}

	public   void removeCookie( HttpRequest request ,String cookiename     ) 
	{
			
		//Cookie sCookie = null;
		String sname = null;
	
		Cookie cookies[] = request.getCookies();
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				//sCookie = cookies[i];
				sname = cookies[i].getName();
				if(sname!=null && sname.equals( strParam) ){
					cookies[i].setMaxAge(0);
					cookies[i].setValue("");
					response.addCookie(cookies[i] );
				}
	
			}
		}


	}
}




 

解决:IE种cookie不好用,但火狐是好用的

      http://blog.csdn.net/cuihaiyang/article/details/8106651

你可能感兴趣的:(java,servlet)