我們?cè)谝粋€(gè)網(wǎng)站注冊(cè)賬號(hào)以后會(huì)有提示下次是否自動(dòng)登錄,如果我們選擇下次自動(dòng)登錄,那么服務(wù)器端就會(huì)把你的賬號(hào)和密碼保存在Cookie中,并返回給客戶端。下次在登錄的時(shí)候Http協(xié)議會(huì)帶上你的Cookie中的內(nèi)容去驗(yàn)證信息,因?yàn)榉?wù)器中已經(jīng)存儲(chǔ)你的信息,下次登錄就會(huì)通過(guò)驗(yàn)證,并直接進(jìn)入你的主頁(yè)。

    下面是登錄的代碼:

public class LoginServlet extends HttpServlet {
    
    
public void doGet(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException {
        
this.doPost(request, response);
    }

  
    
public void doPost(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException {
        String username
=request.getParameter("username");
        String password
=request.getParameter("password");
        String savetime
=request.getParameter("saveTime");
        
if(CheckLogin.login(username, password)){
            
if(null!=savetime){
                
int saveTime=Integer.parseInt(savetime);//這里接受的表單值為天來(lái)計(jì)算的
                int seconds=saveTime*24*60*60;
                Cookie cookie 
= new Cookie("user", username+"=="+password);
                cookie.setMaxAge(seconds);
                response.addCookie(cookie);
            }

            request.setAttribute(
"username",username);
            request.getRequestDispatcher(
"/main126.jsp").forward(request,response);
        }

        
else{
            request.getRequestDispatcher(
"/failure.jsp").forward(request,response);
        }

    }

}

    驗(yàn)證代碼:
public class IndexFilter implements Filter {   
  
    
public void destroy() {
        
    }
   
  
    
public void doFilter(ServletRequest arg0, ServletResponse arg1,
            FilterChain arg2) 
throws IOException, ServletException {
        
        System.out.println(
"every request pass here haha");
        HttpServletRequest request 
= (HttpServletRequest) arg0;
        HttpServletResponse response 
= (HttpServletResponse) arg1;
        Cookie[] cookies 
= request.getCookies();
        String[] cooks 
= null;
        String username 
= null;
        String password 
= null;
        
if (cookies != null{
            
for (Cookie coo : cookies) {
                String aa 
= coo.getValue();
                cooks 
= aa.split("==");
                
if (cooks.length == 2){
                    username 
= cooks[0];
                    password 
= cooks[1];
                }

            }

        }

        System.out.println(
"cookie username | " + username);
        System.out.println(
"cookie password | " + password);
        
        
if (CheckLogin.login(username, password)){
            System.err.println(
"check successfully cookie data ");
            request.getSession().setAttribute(
"username",username);
            request.getRequestDispatcher(
"/main126.jsp").forward(request, response);
        }

        
else{
            arg2.doFilter(request,response );
        }

    }
   
  
    
public void init(FilterConfig arg0) throws ServletException {   
        
// TODO Auto-generated method stub   
  
    }
   
  
}
 

public class CheckLogin {

     
public static boolean login(String username, String password){
         
if ("admin".equals(username) && "123456".equals(password)) {
             
return true;
         }

         
else{
             
return false;
         }
  
     }

}


    我們看看沒有登錄的結(jié)果:


    我們?cè)倏纯匆呀?jīng)登錄的結(jié)果: