JSP隨記:儲(chǔ)存Cookie 和獲取Cookie

一、儲(chǔ)存Cookie:

1、從表單中獲取數(shù)據(jù): request.getParameter("username")

2、創(chuàng)建Cookie對(duì)象: Cookie usernameCookie = new Cookie("username",username);

3.儲(chǔ)存Cookie對(duì)象: response.addCookie(usernameCookie);

<%    
    request.setCharacterEncoding("utf-8");  //設(shè)置編碼    
    //首先判斷用戶是否選擇了記住登錄狀態(tài)    
    String[] isUseCookie = request.getParameterValues("isUseCookie");    
    if (isUseCookie !=null && isUseCookie.length > 0){
        //把用戶名和密碼保存在Cookie對(duì)象里面
        String username = URLEncoder .encode(request.getParameter("username"),"utf-8"); 
       //使用URLEncoder 解決無法在Cookie當(dāng)中保存中文字符串問題
        String password = URLEncoder .encode(request.getParameter("password"),"utf-8");
        Cookie usernameCookie = new Cookie("username",username);
        Cookie passwordCookie = new Cookie("password",password); 
       usernameCookie.setMaxAge(864000); 
       passwordCookie.setMaxAge(864000);
        response.addCookie(usernameCookie); 
       response.addCookie(passwordCookie);
    }else {
        Cookie[] cookies = request.getCookies(); 
       if (cookies!=null && cookies.length>0){
            for (Cookie c:cookies){
                if (c.getName().equals("username") || c.getName().equals("password")){ 
                   c.setMaxAge(0);  //設(shè)置Cookie失效
                    response.addCookie(c); //重新保存 
               } 
           }
        }
    }
%>

二、 獲取Cookie 信息

1.獲取Cookie數(shù)據(jù): request.getCookies();

2.遍歷獲取到的Cookie數(shù)組,取到當(dāng)中的值: c.getValue()

<%
    request.setCharacterEncoding("utf-8"); 
   String username = "";
    String password = "";
    //獲取Cookie數(shù)據(jù)    Cookie[] cookies = request.getCookies();
    //判斷Cookie數(shù)據(jù)是否存在
    if (cookies!=null && cookies.length>0){
        for (Cookie c:cookies){
            //獲取用戶名
            if (c.getName().equals("username")){
                username = URLDecoder.decode(c.getValue(),"utf-8");
            }
            //獲取密碼
            if (c.getName().equals("password")){
                password = URLDecoder.decode(c.getValue(),"utf-8");
            }
        }
    }
%>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,659評(píng)論 19 139
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類相關(guān)的語(yǔ)法,內(nèi)部類的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線程的語(yǔ)...
    子非魚_t_閱讀 34,766評(píng)論 18 399
  • 1、不安全的隨機(jī)數(shù)生成,在CSRF TOKEN生成、password reset token生成等,會(huì)造成toke...
    nightmare丿閱讀 3,998評(píng)論 0 1
  • 今天帶女兒在店里面玩,附近有幾家做生意的小孩在一起玩。最大的一個(gè)小男孩,上一年級(jí)胖胖的,他們家是開花店的。...
    泅海閱讀 212評(píng)論 0 0
  • 最近,某衛(wèi)視熱播了一檔軍營(yíng)真人秀,雖然不知道是不是有劇本臺(tái)稿的演出,但是每每看到軍營(yíng)中那種堅(jiān)韌、毅力、熱血、流汗,...
    貓貓cat閱讀 487評(píng)論 0 0

友情鏈接更多精彩內(nèi)容