Java快速開發平臺

          www.fastunit.com

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            23 Posts :: 0 Stories :: 273 Comments :: 0 Trackbacks
          import javax.servlet.http.Cookie;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          public class CookieUtil {

            
          public static Cookie getCookie(HttpServletRequest request, String name) {
              Cookie cookies[] 
          = request.getCookies();
              
          if (cookies == null || name == null || name.length() == 0) {
                
          return null;
              }
              
          for (int i = 0; i < cookies.length; i++) {
                
          if (name.equals(cookies[i].getName())
                    
          && request.getServerName().equals(cookies[i].getDomain())) {
                  
          return cookies[i];
                }
              }
              
          return null;
            }

            
          public static void deleteCookie(HttpServletRequest request,
                HttpServletResponse response, Cookie cookie) {
              
          if (cookie != null) {
                cookie.setPath(getPath(request));
                cookie.setValue(
          "");
                cookie.setMaxAge(
          0);
                response.addCookie(cookie);
              }
            }

            
          public static void setCookie(HttpServletRequest request,
                HttpServletResponse response, String name, String value) {
              setCookie(request, response, name, value, 
          0x278d00);
            }

            
          public static void setCookie(HttpServletRequest request,
                HttpServletResponse response, String name, String value, 
          int maxAge) {
              Cookie cookie 
          = new Cookie(name, value == null ? "" : value);
              cookie.setMaxAge(maxAge);
              cookie.setPath(getPath(request));
              response.addCookie(cookie);
            }

            
          private static String getPath(HttpServletRequest request) {
              String path 
          = request.getContextPath();
              
          return (path == null || path.length()==0? "/" : path;
            }

          }
          posted on 2008-03-25 17:19 FastUnit 閱讀(9391) 評論(5)  編輯  收藏 所屬分類: Java

          Feedback

          # 頂頂頂 2008-03-26 16:28 草包書生
          可以這樣,但現在好多PC的主人都不愿意保存cookie了呀。

          我決定把這個收藏起來  回復  更多評論
            

          # re: Java操作Cookie 2008-06-02 15:09 懶人
          @草包書生
          沒有用,還煩  回復  更多評論
            

          # re: Java操作Cookie 2008-07-17 10:23 larry_qr
          草包,這咋能沒有用呢?為什么?別這么不負責任的亂說  回復  更多評論
            

          # re: Java操作Cookie 2009-01-20 13:55 wangs130
          very good!  回復  更多評論
            

          # re: Java操作Cookie 2014-08-13 12:15 雪萊
          已收藏  回復  更多評論
            

          主站蜘蛛池模板: 海口市| 丰宁| 赤壁市| 高尔夫| 万荣县| 台州市| 黔西| 呈贡县| 肥东县| 西充县| 容城县| 攀枝花市| 新巴尔虎右旗| 福海县| 章丘市| 毕节市| 普洱| 泸定县| 清涧县| 施秉县| 富民县| 云梦县| 揭东县| 萍乡市| 闽清县| 无极县| 林芝县| 江陵县| 奉化市| 区。| 南宁市| 搜索| 资溪县| 乐山市| 双流县| 湖口县| 新蔡县| 峨眉山市| 关岭| 海原县| 庆阳市|