初學JavaWeb-利用Cookie儲存使用者基本資料__Java

來源:互聯網
上載者:User
前兩天把註冊給寫完了,昨天試著寫了寫登陸。這裡主要說一下登陸的 記住我 按鈕是如何?的

Cookie

  Cookie是用戶端技術,程式把每個使用者的資料以cookie的形式寫給使用者各自的瀏覽器。當使用者使用瀏覽器再去訪問伺服器中的web資源時,就會帶著各自的資料去。這樣,web資源處理的就是使用者各自的資料了。

 與之前說過的session不同,session是儲存在服務端的,而cookie是儲存在用戶端的。

首先先在前端通過Jquey來判斷,使用者是否勾選了 記住我 複選框,如果勾選了,那麼後台將使用者的帳號和密碼寫入cookie

 首先先建立Cookie,然後將使用者名稱和密碼寫入Cookie,設定號Cookie存在的時間長度,用response寫入Cookie

 後端代碼:

 

//添加緩衝    @RequestMapping("/add_cookie")    @ResponseBody    public String add_cookie(User user, HttpServletResponse response) throws UnsupportedEncodingException {        String username = user.getUserName();        String userPassword = user.getUserPassword();        //將使用者名稱存入cookie 並且設定cookie存在時間長度        Cookie cookie_username = new Cookie("username", URLEncoder.encode(username,"utf-8"));        cookie_username.setMaxAge(60*60*60);        response.addCookie(cookie_username);        //將密碼存入cookie 並且設定cookie存在時間長度        Cookie cookie_userPassword = new Cookie("userPassword",URLEncoder.encode(userPassword,"utf-8"));        cookie_userPassword.setMaxAge(60*60*60);        response.addCookie(cookie_userPassword);        return null;    }

HttpServletResponse對象介紹  

HttpServletResponse對象代表格服務器的響應。這個對象中封裝了向用戶端發送資料、發送回應標頭,發送響應狀態代碼的方法。查看HttpServletResponse的API,可以看到這些相關的方法


當使用者不想再讓自己的使用者名稱和密碼儲存在Cookie中,那麼將執行刪除緩衝的方法


代碼如下:

 

    //刪除緩衝    @RequestMapping("/del_cookie")    @ResponseBody    public String del_cookie(HttpServletRequest request,HttpServletResponse response){        Cookie[] cookies = request.getCookies();        if (cookies != null && cookies.length > 0) {            for (Cookie cookie : cookies) {                // 找到需要刪除的Cookie                if("username".equals(cookie.getName())){                    // 設定生存期為0                    cookie.setMaxAge(0);                    // 設回Response中生效                    response.addCookie(cookie);                }                if("userPassword".equals(cookie.getName())){                    // 設定生存期為0                    cookie.setMaxAge(0);                    // 設回Response中生效                    response.addCookie(cookie);                }            }        }        return null;    }


最後只用在使用者每次開啟首頁時候,重新導向中執行尋找Cookie的方法就可以實現了


 //判斷cookiees是否為空白        if(request.getCookies()!=null){            //將cookies值放入cookies數組            Cookie[] cookies = request.getCookies();            //判斷cookies數組            if (cookies != null && cookies.length > 0) {                //遍曆cookies數組                for (Cookie cookie : cookies) {                    if (cookie.getName().equals("username")) {                        model.addAttribute("username", URLDecoder.decode(cookie.getValue(), "utf-8"));                    }                    if (cookie.getName().equals("userPassword")){                        model.addAttribute("userPassword",URLDecoder.decode(cookie.getValue(),"utf-8"));                    }                }            }        }



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.