Java快速開發平臺

          www.fastunit.com

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            23 Posts :: 0 Stories :: 273 Comments :: 0 Trackbacks

          公告

          常用鏈接

          留言簿(23)

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          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 閱讀(9397) 評論(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 雪萊
          已收藏  回復  更多評論
            

          主站蜘蛛池模板: 深州市| 阜平县| 桑植县| 河津市| 寻甸| 通海县| 黄浦区| 阳江市| 金昌市| 大石桥市| 咸阳市| 星座| 萝北县| 安义县| 郧西县| 台前县| 安阳市| 濮阳市| 盐亭县| 富川| 沾益县| 大竹县| 新郑市| 牙克石市| 梅河口市| 鄂托克旗| 清徐县| 贵港市| 衡山县| 响水县| 青海省| 白朗县| 兰溪市| 自治县| 嵊州市| 宜城市| 双桥区| 甘孜县| 马边| 福海县| 铜山县|