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

设置Cookie

程序员文章站 2022-03-03 12:40:06
...
	public static void setCookie(javax.servlet.http.HttpServletResponse response,String name,String value){
		if (null==name || "".equals(name)) 
			return;
		if (null==value ) 
			value="null";

		Cookie cookie=new Cookie(name, value);
		cookie.setMaxAge(300*60);
		cookie.setDomain("url");
		cookie.setPath("/");
		response.addCookie(cookie);
	}

  删除Cookie

		 Cookie  cookie   =new   Cookie("loginId",   null);   
         cookie.setMaxAge(0);   //设置为0就过期了。即删除。
		 cookie.setDomain("url");
		 cookie.setPath("/");
         response.addCookie(cookie);

  获取Cookie

	public static String getHeadCookie(
			javax.servlet.http.HttpServletRequest request, String cookie_name) {
		String cookie_value = "";
		Cookie cookies[] = request.getCookies();
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				System.out.println("cookies[i].getValue = "
						+ cookies[i].getName());
				if (cookies[i].getName().equals(cookie_name)) {
					cookie_value = cookies[i].getValue();

				}
			}
		}
		return cookie_value;
	}
 

 

 

相关标签: Servlet