您的位置:程序门 -> java -> j2ee / ejb / jms



使用cookie来纪录同一个客户端在一次会话中访问页面的次数


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


使用cookie来纪录同一个客户端在一次会话中访问页面的次数
发表于:2007-05-25 15:35:10 楼主
代码如下:
public   class   cookiecounter   extends   httpservlet   {
        private   static   final   string   content_type   =   "text/html;   charset=gbk ";
        private   int   pagecount=0;

        //initialize   global   variables
        public   void   init(servletconfig   config)   throws   servletexception   {
                super.init(config);
                string   s=getinitparameter( "initial ");
                if(s==null){
                        pagecount=0;
                }else{
                        pagecount=integer.parseint(s);
                }
        }

        //process   the   http   get   request
        public   void   doget(httpservletrequest   request,   httpservletresponse   response)   throws
                        servletexception,   ioexception   {
                response.setcontenttype(content_type);
                printwriter   out   =   response.getwriter();
                boolean   cookiefound=false;
                cookie   thiscookie=null;
                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;
                                }
                        }
                }
                if(cookiefound==false){
                        thiscookie=new   cookie( "cookiecount ", "1 ");
                        thiscookie.setmaxage(60*1);
                        response.addcookie(thiscookie);
                }
                out.println( " <html> ");
                out.println( " <head> <title> cookie   counter </title> </head> ");
                out.println( " <body   bgcolor=\ "#ffffff\ "> ");
                out.println( " <h1   align=center> cookie   counter </h1> ");
                pagecount++;
                out.println( " <p> 此页面已被访问过 "+pagecount+ "次 ");
                if(cookiefound){
                        int   cookiecount=integer.parseint(thiscookie.getvalue());
                        cookiecount++;
                        thiscookie.setvalue(string.valueof(cookiecount));
                        thiscookie.setmaxage(1);
                        response.addcookie(thiscookie);
                        out.println( " <p> 在最近10秒钟内,您访问此页面 "+thiscookie.getvalue()+ "次 ");
                }else{
                        out.println( " <p> 在最近10秒钟内您没有访问此页面,或者您的浏览器不支持cookie! ");
                }
                out.println( " </body> ");
                out.println( " </html> ");
                out.close();
        }

        //process   the   http   post   request
        public   void   dopost(httpservletrequest   request,   httpservletresponse   response)   throws
                        servletexception,   ioexception   {
                doget(request,   response);
        }

        //clean   up   resources
        public   void   destroy()   {
        }
}

请各路高手给在下解释一下代码关键部分的意思,不胜感谢
尤其是为什么要判断2次cookiefound
发表于:2007-05-25 15:45:481楼 得分:0
if(cookiefound==false){
是为了判断cookiefound为false的情况,即没有找到cookie的情况,没有找到这里进行了创建cookie的操作.(还可以写成这种形式if(!cookiefount))

if(cookiefound){
是为了判断cookiefound为true的情况,即找到cookie的情况,这里进行了修改值的操作



快速检索

最新资讯
热门点击