iLeichun

当前位置: 首页 > Jsp

jsp的cookies

分类:Jsp   来源:网络   时间:2010-09-05 00:16:08

用习惯了php的方式,jsp的很是不适应。

记一个小小的练习上来,别忘了!

 

XML/HTML代码
<%@page contentType="text/html" pageEncoding="GBK"%>  
<%@page import="javax.servlet.http.Cookie,java.util.*"%>  
  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"   
   "http://www.w3.org/TR/html4/loose.dtd">  
  
<html>  
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=GBK">  
        <title>JSP Page</title>  
    </head>  
    <body>  
        <%   
            //String CookiesName="username";   
            //Cookie cookie_username=new Cookie("username",CookiesName);   
            //response.addCookie(cookie_username);   
            int click=0;   
  
            Cookie[] cookies=request.getCookies();   
            Cookie cookie_reponse=null;   
            List list=Arrays.asList(cookies);   
            Iterator it=list.iterator();   
            while(it.hasNext()){   
                Cookie temp=(Cookie)it.next();   
                if(temp.getName().equals("clicktimes")){   
                    click=Integer.parseInt(temp.getValue());   
                    cookie_reponse=temp;   
                    break;   
                }   
            }   
            //取得了click的值   
  
            //输出   
            out.println("第 "+click+" 次刷新");   
  
            //更新   
            clickclick=click+1;   
            if(cookie_reponse==null){   
                //空的   
                cookie_reponse=new Cookie("clicktimes", String.valueOf(click));   
            }else{   
                cookie_reponse.setValue(String.valueOf(click));   
            }   
            response.addCookie(cookie_reponse);   
            response.setContentType("text/html");   
            response.flushBuffer();   
        %>  
    </body>  
</html>  
 

 
 

更多