(7) Cookie对象的使用

核心代码:
cookie.jsp

  
  <%
    Cookie thisCookie = null;
    boolean cookieFound = false;
    Cookie[] cookies = request.getCookies();
    if(cookies!=null){
        for(int i=0;i < cookies.length;i++){
            thisCookie = cookies[i];
            if(thisCookie.getName().equals("CookieCount")){
                cookieFound = true;
                break;
            }
        }
    }
    out.println("

Cookie 计数器

"); if(cookieFound){ int cookieCount = Integer.parseInt(thisCookie.getValue()); cookieCount++; out.println("这是1分钟内第"+cookieCount+"次访问本页"); thisCookie.setValue(String.valueOf(cookieCount)); thisCookie.setMaxAge(60*1); response.addCookie(thisCookie); } if(cookieFound==false){ out.println("你在近1分钟内没有访问过此页,现在是第一次访问"); thisCookie = new Cookie("CookieCount","1"); thisCookie.setMaxAge(60*1); response.addCookie(thisCookie); } %>

cookieIndex.jsp

  
    <%  
        String strUser=null;
        String strPass=null;
        boolean cookieFound = false;
        
        //获取Cookie信息,并将值传入文本框
        Cookie[] cookies = request.getCookies();
        if(cookies!=null){
            for(int i=0;i
    
  
用户名:
密码:
记住密码:

你可能感兴趣的:((7) Cookie对象的使用)