Java快速開(kāi)發(fā)平臺(tái)

          www.fastunit.com

            BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            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 閱讀(9398) 評(píng)論(5)  編輯  收藏 所屬分類: Java

          Feedback

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

          我決定把這個(gè)收藏起來(lái)  回復(fù)  更多評(píng)論
            

          # re: Java操作Cookie 2008-06-02 15:09 懶人
          @草包書(shū)生
          沒(méi)有用,還煩  回復(fù)  更多評(píng)論
            

          # re: Java操作Cookie 2008-07-17 10:23 larry_qr
          草包,這咋能沒(méi)有用呢?為什么?別這么不負(fù)責(zé)任的亂說(shuō)  回復(fù)  更多評(píng)論
            

          # re: Java操作Cookie 2009-01-20 13:55 wangs130
          very good!  回復(fù)  更多評(píng)論
            

          # re: Java操作Cookie 2014-08-13 12:15 雪萊
          已收藏  回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 拉萨市| 乐昌市| 长春市| 波密县| 临安市| 武乡县| 文山县| 万山特区| 庆云县| 通河县| 民勤县| 泗水县| 宁晋县| 枣阳市| 六盘水市| 五大连池市| 栖霞市| 筠连县| 乌拉特后旗| 资兴市| 六盘水市| 望奎县| 禹城市| 闸北区| 图们市| 全椒县| 朝阳县| 台北县| 大厂| 县级市| 福建省| 武宣县| 绥滨县| 哈密市| 华安县| 澄城县| 黑山县| 花垣县| 高阳县| 嘉定区| 正宁县|