1. 程式人生 > 實用技巧 >cookie自動儲存賬戶密碼

cookie自動儲存賬戶密碼

登入判斷成功後,呼叫cookie方法

private void saveCookie(HttpServletRequest request, HttpServletResponse response){
        logger.debug("entry saveCookie");
        String saveFlag = request.getParameter("saveFlag");
        logger.debug("saveFlag= " + saveFlag);
        if (saveFlag != null && Constants.SAVE.equals(saveFlag))
        {
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            Cookie cookieUsername = new Cookie("username",username);
            Cookie cookiePassword = new Cookie("password", password);
            // maxAge defaults to -1,do not save.A positive value indicates that
            // the cookie will expire after that many seconds have passed
            // a week
            int expiry = 7 * 24 * 3600;
            cookieUsername.setMaxAge(expiry);
            cookiePassword.setMaxAge(expiry);
            // Save to the browser
            response.addCookie(cookieUsername);
            response.addCookie(cookiePassword);

            logger.debug("save cookies success");
        }
    }