<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    我的java歷程

    Cookie的使用

     

    Cookie的使用

     

    一、cookie的作用

    在我們平常寫的B/S程序中,會經常用到cookie,主要有以下一些作用:

    1、 記錄用戶名和密碼

    以及該用戶需要保存的一些信息,如購物購站,使用cookie,可以讓用戶自動登錄到站點等。

    2、 定制站點

    可以使用cookie來記錄用戶的偏好。

    3、 定向廣告

    可以使用cookie來記錄用記經常訪問的主題,并向他們顯示與這些主題相關的廣告。

     

    二、向客戶程序發送cookie

    1、 創建cookie對象

    Cookie c = new Cookie(“userId”,”landril”);

    2、 設置最大時效,默認該cookie是存儲在瀏覽器的內在中,用戶關閉瀏覽器則被刪除,下面的方法是將cookie存儲在硬盤上。

    c.setMaxAge(60*60*24);//一天,如果設置為0則是刪除該cookie

    3、 cookie放入到HTTP響應報頭,可以使用HttpServletResponseaddCookie方法,此方法不修改之前指定的Set-Cookie報頭,而是創建新的報頭。

    response.addCookie(c);

           注意:設置cookie的步驟為創建cookie對象,設置最大時效,將cookie放入響應報頭,即發送到客戶程序,記住一定要將cookie發送到客戶程序

     

    三、從客戶端讀取cookie

    1、 調用HttpServletRequestgetCookies得到一個Cookie對象的數組

    2、 對數組進行循環,調用cookiegetName方法,獲取具體的cookie的值

    Cookie[] cookies = request.getCookies();

    if(cookies != null){

    for(int i=0;i<cookies.length;i++){

            Cookie c = cookies[i];

            if(“userId”.equals(c.getName())){

            System.out.println(c.getValue());

    }

    }

    }

     

    四、cookie的常用方法

    1、 setComment()/getComment():指定或查找與該cookie相關的注釋

    2、 setDomain()/getDomain():設置或讀取該cookie適用的域

    3、 setMaxAge()/getMaxAge():操作cookie保留的時間,多長時間后過期

    4、 getName():讀取cookie的名稱

    5、 setPath()/getPath():設置或取得cookie適用的路徑

    cookie.setPath(“/”);指定服務器的所有頁面都應該收到該cookie

    6、 setSource()/getSource():指定cookie是否只能通過加密連接(SSL

    默認false,表示cookie適用所有連接

    7、 setValue()/getValue():指定或獲取cookie的值

     

    五、使用cookie

     

    1RepeatServlet.java

    public class RepeatServlet extends HttpServlet {

     

        public void doGet(HttpServletRequest request, HttpServletResponse response)

               throws ServletException, IOException {

            boolean newa = true;

            Cookie[] cookies = request.getCookies();

            if(cookies != null){

               for (int i = 0; i < cookies.length; i++) {

                   Cookie c = cookies[i];

                   if ((c.getName().equals("repeat")) && (c.getValue().equals("true"))) {

                      newa = false;

                       break;

                   }

               }

           }

          

            String title;

           if (newa) {

               Cookie rtn = new Cookie("repeat"," true");

               rtn.setMaxAge(60*60*24*365);

               response.addCookie(rtn);

               title = "First Welcome";

           } else {

               title = "Welcom Back";

           }

          

            response.setContentType("text/html");

            PrintWriter out = response.getWriter();

            out.println("<html><body><h3>");

            out.println(title);

            out.println("</h3></body></html>");

        }

     

        public void doPose(HttpServletRequest request, HttpServletResponse response)

               throws ServletException, IOException {

            doGet(request,response);

        }

    }

     

    2CookieUtil.java

    public class CookieUtil{

     

        public static String getValue(HttpServletRequest request,String cName,String value){

            Cookie[] cookies = request.getCookies();

            if(cookies != null){

               for (int i = 0; i < cookies.length; i++) {

                   Cookie cookie = cookies[i];

                   if (cName.equals(cookie.getName())) {

                       return cookie.getValue();

                   }

               }

           }

            return value;

        }

       

        public static Cookie getCookie(HttpServletRequest request,String cName){

            Cookie[] cookies = request.getCookies();

            if(cookies != null){

               for (int i = 0; i < cookies.length; i++) {

                   Cookie cookie = cookies[i];

                   if (cName.equals(cookie.getName())) {

                       return cookie;

                   }

               }

           }

            return null;

        }

    }

     

    3AccessCountServle.java 記錄訪問數

    public class AccessCountServlet extends HttpServlet {

     

        public void doGet(HttpServletRequest request, HttpServletResponse response)

               throws ServletException, IOException {

          

            String acount = CookieUtil.getValue(request, "acc", "1");

           int count = Integer.parseInt(acount);

            Cookie cookie = new Cookie("acc",String.valueOf(count+1));

            cookie.setMaxAge(60*60*24*365);

            response.addCookie(cookie);

          

            response.setContentType("text/html");

            PrintWriter out = response.getWriter();

            out.println("<html><body>Hello,the number is<h3>");

            out.println(count);

            out.println("</h3></body></html>");

        }

       

        public void doPost(HttpServletRequest request, HttpServletResponse response)

        throws ServletException, IOException {

            doGet(request,response);

        }

    }

    posted on 2007-04-10 12:37 landril 閱讀(12426) 評論(2)  編輯  收藏 所屬分類: Jsp&Servlet

    Feedback

    # re: Cookie的使用 2007-10-14 10:50 22

    留言怎么沒有COOKIES的,  回復  更多評論   

    # re: Cookie的使用 2013-09-09 13:58 帶飯

    你知道老習是誰不  回復  更多評論   

    主站蜘蛛池模板: 在线观看免费高清视频| 亚洲一区二区三区在线观看网站| a色毛片免费视频| 国产亚洲精AA在线观看SEE| 你是我的城池营垒免费看| 亚洲中文字幕无码不卡电影| 国产成人无码精品久久久久免费| 亚洲高清视频一视频二视频三| 无套内谢孕妇毛片免费看看| 亚洲av午夜精品一区二区三区 | 99久久99久久精品免费看蜜桃| 亚洲视频在线不卡| 亚洲成年人免费网站| 亚洲国产av美女网站| 青青视频观看免费99| 亚洲国产综合精品中文第一| 成人性生免费视频| 亚洲经典千人经典日产| 国产国产成年年人免费看片| 国产午夜亚洲精品不卡电影| 亚洲第一区在线观看| GOGOGO免费观看国语| 国产A在亚洲线播放| 久久久久久久岛国免费播放| 亚洲酒色1314狠狠做| 国产成人福利免费视频| 亚洲综合无码无在线观看| 免费看a级黄色片| 美女视频黄频a免费| 国产亚洲成人在线播放va| 永久在线观看免费视频 | 曰批全过程免费视频网址| 亚洲国产成人久久精品app| 在线免费观看一级片| 曰批全过程免费视频免费看| 亚洲香蕉网久久综合影视| 日韩人妻无码精品久久免费一| 亚洲中文字幕无码av在线| 日本一道一区二区免费看 | 日韩中文字幕免费| 老司机免费午夜精品视频|