Chinaunix首页 | 论坛 | 博客
  • 博客访问: 742409
  • 博文数量: 225
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2722
  • 用 户 组: 普通用户
  • 注册时间: 2013-02-03 17:32
文章分类

全部博文(225)

文章存档

2019年(7)

2018年(16)

2017年(1)

2016年(26)

2015年(41)

2014年(15)

2013年(119)

我的朋友

分类: Java

2013-08-08 16:00:49


  1. public static void addCookie(HttpServletResponse response,String domain,String name,String value,int maxAge){
  2.         try {
  3.             //解决中文报Control character in cookie value, consider BASE64 encoding your value错。
  4.             value=URLEncoder.encode(value,"UTF-8");
  5.         } catch (UnsupportedEncodingException e) {
  6.             // TODO Auto-generated catch block
  7.             e.printStackTrace();
  8.         }
  9.         Cookie cookie = new Cookie(name,value);
  10.      cookie.setPath("/");
  11.      if(!domain.equals("")&&domain!=null){
  12.          cookie.setDomain(domain);
  13.      }else {
  14.             cookie.setDomain("."+SystemConfig.getInstance().getDnsName());
  15.         }
  16.      if(maxAge>0) cookie.setMaxAge(maxAge);
  17.      response.addCookie(cookie);
  18.     }
  19.     public static String getCookieValue(String cook, HttpServletRequest request) {
  20.         Cookie[] cookies = request.getCookies();
  21.         try {
  22.             for (Cookie one : cookies) {
  23.                 if (one.getName() != null && one.getName().equals(cook))
  24.                     return URLDecoder.decode(one.getValue(),"UTF-8");
  25.             }
  26.         } catch (Exception e) {
  27.             e.printStackTrace();
  28.         }
  29.         return null;
  30.     }
  31.     public static void clearCookiebyName(String cook, HttpServletRequest request,HttpServletResponse response){
  32.         Cookie[] cookies = request.getCookies();    
  33.         if (cookies != null) {
  34.             for (Cookie one : cookies) {            
  35.                 try {
  36.                     if (one.getName() != null && one.getName().equals(cook)){
  37.                         one.setMaxAge(0);
  38.                         one.setDomain("." + SystemConfig.getInstance().getDnsName());
  39.                         response.addCookie(one);
  40.                     }                    
  41.                 } catch (Exception e) {

  42.                     BitTools.debugPrintln("清楚Cookies发生异常!");
  43.                 }
  44.             }
  45.         }
  46.         
  47.     }
  48.     public static void clearAllCookie(HttpServletRequest request,HttpServletResponse response){
  49.         Cookie[] cookies = request.getCookies();
  50.         Cookie cookie = null;
  51.         if (cookies != null) {
  52.             for (int i = 0; i < cookies.length; i++) {                
  53.                 try {
  54.                     cookie = cookies[i];
  55.                     cookie.setMaxAge(0);
  56.                     cookies[i].setDomain("." + SystemConfig.getInstance().getDnsName());
  57.                     response.addCookie(cookie);
  58.                 } catch (Exception e) {

  59.                     BitTools.debugPrintln("清空Cookies发生异常!");
  60.                 }
  61.             }
  62.         }
  63.     }

阅读(1104) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~