欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

servlet cookie 操作

程序员文章站 2024-03-20 13:55:16
...
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