当前位置:编程学习 > JS >>

jsp中cookies的使用以及代码实例

下面是一个jsp中cookies的使用实例代码
  1. <%@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> 
补充:Web开发 , Jsp ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,