servletクッキー操作

2167 ワード

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种クッキーは使いにくいですが、火狐は使いやすいです.
      http://blog.csdn.net/cuihaiyang/article/details/8106651