java Source

            BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            14 Posts :: 24 Stories :: 8 Comments :: 0 Trackbacks
          /*
           * CookieSupport.java
           * Copyright (C) 2007-3-19  <JustinLei@gmail.com>
           *
           *        This program is free software; you can redistribute it and/or modify
           *        it under the terms of the GNU General Public License as published by
           *      the Free Software Foundation; either version 2 of the License, or
           *     (at your option) any later version.
           *
           *       This program is distributed in the hope that it will be useful,
           *      but WITHOUT ANY WARRANTY; without even the implied warranty of
           *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
           *        GNU General Public License for more details.
           *
           
          */
          package org.lambdasoft.web.support;

          import java.util.HashMap;
          import java.util.Map;
          import java.util.Set;

          import javax.servlet.http.Cookie;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          import org.lambdasoft.utils.StringUtils;

          /**
           * 
          @author TangLei <justinlei@gmail.com>
           * @date 2008-12-17
           
          */
          public class CookieSupport {
              
          private CookieSupport() {
              }

              
          /**
               * 寫cookies
               * 
               * 
          @param response
               * 
          @param cookieParams
               * 
          @param maxAge
               
          */
              
          public static final void writeCookies(HttpServletResponse response,
                      Map
          <String, String> cookieParams, int maxAge) {
                  
          if (cookieParams == null || cookieParams.size() == 0)
                      
          return;
                  Set
          <String> keySet = cookieParams.keySet();
                  
          for (String key : keySet) {
                      Cookie cookie 
          = new Cookie(key, cookieParams.get(key));
                      cookie.setMaxAge(maxAge);
                      response.addCookie(cookie);
                  }
              }
              
              
          /**
               * 刪除所有的cookies
               * 
          @param request
               * 
          @param response
               
          */
              
          public static final void removeAllCookies(HttpServletRequest request,HttpServletResponse response) {
                  Cookie[] cookies 
          = request.getCookies();
                  
          if(cookies == null || cookies.length == 0)
                      
          return;
                  Map
          <String, String> cookiesMap = new HashMap<String, String>();
                  
          for (Cookie cookie : cookies) {
                      cookiesMap.put(cookie.getName(),
          "");
                  }
                  writeCookies(response, cookiesMap, 
          0);
              }
              
              
          /**
               * 讀取cookies
               * 
               * 
          @param request
               * 
          @param cookieName
               * 
          @return
               
          */
              
          public static final Cookie[] readCookies(HttpServletRequest request,String cookieName) {
                  Cookie[] cookies 
          = request.getCookies();
                  
          if(cookies == null || cookies.length == 0)
                      
          return null;
                  
          if(StringUtils.isEmpty(cookieName))
                      
          return cookies;
                  
          for (int i = 0; i < cookies.length; i++) {
                      
          if(cookies[i].getName().equals(cookieName))
                          
          return new Cookie[] {cookies[i]};
                  }
                  
          return null;
              }
          }
          posted on 2009-12-18 16:34 JustinLei 閱讀(1418) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 沂水县| 延津县| 兴海县| 莲花县| 大丰市| 汪清县| 岑溪市| 图木舒克市| 威远县| 和田县| 长汀县| 大新县| 略阳县| 潼南县| 崇左市| 临澧县| 武陟县| 泽州县| 北川| 嘉禾县| 临沭县| 临猗县| 黄大仙区| 绥德县| 进贤县| 金昌市| 平和县| 会泽县| 民县| 陵川县| 霍山县| 嵩明县| 基隆市| 安阳市| 新龙县| 奇台县| 英超| 五台县| 晋中市| 东乌珠穆沁旗| 南溪县|