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

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

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

    yxhxj2006

    常用鏈接

    統計

    最新評論

    Java中Cookie的使用

    1、Cookie的來歷與作用

    Cookie是WEB服務器通過瀏覽器保存在WWW用戶端硬盤上的一個文本文件,這個文本文件中包含了文本信息。
    文本信息的內容以“名/值”對(key/value)的形式進行存儲。
    可以讓WEB開發者通過程序讀寫這個文本文件。
    XP中保存Cookie的目錄是“C://Documents and Settings\用戶名\Cookies”
    Cookie的作用
    解決瀏覽器用戶與Web服務器之間無狀態通信。
    2Cookie編程

    //創建對象
    Date date = new Date() ;
    Cookie c = new Cookie("lastVisited",date.toString()) ;
    //設定有效時間  以s為單位
    c.setMaxAge(60) ;
    //設置Cookie路徑和域名
    c.setPath("/") ;
    c.setDomain(".zl.org") ;  //域名要以“.”開頭
    //發送Cookie文件
    response.addCookie(c) ;
    //讀取Cookie
    Cookie cookies[] = request.getCookies() ;
    Cookie c1 = null ;
    if(cookies != null){
        for(int i=0;i<cookies.length;i++){
           c1 = cookies[i] ;
           out.println("cookie name : " + c1.getName() + "   ") ;
           out.println("cookie value :" + c1.getValue() + "<br>");
        }
    }
    //修改Cookie
    Cookie cookies[] = request.getCookies() ;
    Cookie c = null ;
    for(int i=0;i<cookies.length;i++){
       c = cookies[i] ;
       if(c.getName().equals("lastVisited")){
          c.setValue("2010-04-3-28") ;
          c.setMaxAge(60*60*12) ;
          response.addCookie(c) ;     //修改后,要更新到瀏覽器中    
       }
    }

    //刪除Cookie,(將Cookie的有效時間設為0)
    Cookie cookies[] = request.getCookies() ;
    Cookie c = null ;
    for(int i=0;i<cookies.length;i++){
       c = cookies[i] ;
       if(c.getName().equals("lastVisited")){
          c.setMaxAge(0);
          response.addCookie(c) ;
       }
    }
     
    4、使用Cookie的注意事項

    ·Cookie的大小和數量是有限制的。
    ·Cookie在個人硬盤上所保存的文本信息是以明文格式進行保存的,沒有任何的加密措施。
    ·瀏覽器用戶可以設定不使用Cookie。
    5、實例:Servlet中的Cookie編程

     cookieInput.html頁面
     
    SetCookie.java
     
    GetCookie.java
     
     
    cookieInput.html頁面中的參數提交到SetCookie.java中,由SetCookie.java保存在瀏覽器的Cookie中,在SerCookie.java中鏈接到GetCookie.java從而讀取剛剛保存的Cookie。

    SetCookie.java :
    public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
     
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            String username = request.getParameter("username") ;
            //保存Cookie
            if(username !=""){
                Cookie c1 = new Cookie("username",username) ;
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd") ;
                Cookie c2 = new Cookie("lastVisited",sdf.format(new java.util.Date())) ;
                c1.setMaxAge(60*60*60*12*30) ;
                c2.setMaxAge(60*60*60*12*30) ;
                response.addCookie(c1) ;
                response.addCookie(c2) ;
                out.println("Cookie保存成功!");
                out.println("<br><br>") ;
                out.println("<a href=GetCookie02>讀取Cookie</a>") ;
            }else{
                response.sendRedirect("../cookieInput.html") ;
            }
           
           
            out.flush();
            out.close();
        }
     
    GetCookie.java:
    public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
     
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
            out.println("<HTML>");
            out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
            out.println("  <BODY>");
           
            //讀取Cookie
            Cookie cookies[] = request.getCookies() ;
            Cookie c = null ;
            if(cookies != null){
                for(int i=0;i<cookies.length;i++){
                    c = cookies[i] ;
                    if(c.getName().equals("username")){
                        out.println("用戶名: "+c.getValue());
                        out.println("<br>");
                    }
                    if(c.getName().equals("lastVisited")){
                        out.println("上次登錄的時間: "+c.getValue());
                    }
                }
            }else{
                out.println("No cookie !");
            }
           
            out.println("  </BODY>");
            out.println("</HTML>");
            out.flush();
            out.close();
        }

    posted on 2013-11-25 22:15 奮斗成就男人 閱讀(172) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲精品色播一区二区 | eeuss影院ss奇兵免费com| 免费av欧美国产在钱| 91嫩草亚洲精品| 丁香花在线观看免费观看| 国产精品高清视亚洲精品| 西西大胆无码视频免费| 激情亚洲一区国产精品| 大陆一级毛片免费视频观看| 亚洲国产精品无码观看久久| 亚洲av麻豆aⅴ无码电影| 一级有奶水毛片免费看| 亚洲av无码av制服另类专区| 91福利视频免费观看| 亚洲激情视频图片| 国产中文字幕免费观看| 国产大片免费天天看| 久久亚洲中文字幕精品有坂深雪 | 久久久久久av无码免费看大片| 亚洲精品乱码久久久久久久久久久久| 免费在线看黄网站| 亚洲美女人黄网成人女| 青苹果乐园免费高清在线| 色天使色婷婷在线影院亚洲| 中文字幕在亚洲第一在线| 午夜影院免费观看| 亚洲一卡一卡二新区无人区| 亚洲欧洲自拍拍偷精品 美利坚| 成全视频免费观看在线看| 精品国产成人亚洲午夜福利| 男人的天堂亚洲一区二区三区 | 亚洲精品国精品久久99热| 免费视频成人手机在线观看网址| 亚洲国产成a人v在线| 青青草国产免费久久久下载| 久久久精品国产亚洲成人满18免费网站| 亚洲国产二区三区久久| 四虎免费大片aⅴ入口| a毛片免费全部播放完整成| 亚洲香蕉在线观看| 国产成人综合亚洲亚洲国产第一页|