Cookie常用属性

来源:互联网 发布:证书打印软件 编辑:程序博客网 时间:2024/03/28 22:17

String name:该Cookie的名称,一旦创建,名称便不可更改

Object value:该Cookie的值,如果值为Unicode字符,需要为字符编码。

如果值为二进制数据,则需要使用BASE64编码

int maxAge Cookie失效时间,单位秒。如果为正数,则CookiemaxAge秒之后失效。

如果为负数,该Cookie为临时Cookie,关闭浏览器即失效,浏览器也不会以任何形式保存Cookie.如果为0,表示删除Cookie。默认是-1

Integer.MAX_VALUE表示永远有效

booleansecure:该Cookie是否仅被使用安全协议传输。安全协议有HTTPS,SSL等,

在网络上传输数据之前先将数据加密。默认为false

secure属性并不能对Cookie内容加密,因而不能保证绝对的安全性。如果

需要高安全性,需要在程序中对Cookie内容加密,解密,以防泄密

String path:该Cookie使用路径。如果设置为”/sessionWeb/”,

只有contextPath”/sessionWeb”的程序可以访问该Cookie。如果设置为”/”,则本域名下contextPath都可以访问该Cookie.注意最后一个字符必须为/

String domain可以访问该Cookie的域名。如果设置为”.google.com”,则所有以”google.com”结尾的域名都可以访问该Cookie注意第一个字符必须为.

String commentCookie的用处说明。浏览器显示Cookie信息的时候显示该说明

int version:该Cookie使用的版本号0表示遵循NetscapeCookie规范,

1表示遵循W3CRFC2109规范

注意:浏览器提交Cookie时只会提交NAMEVALUE的值

       修改,删除Cookie时,新建的Cookievalue,maxAge之外的所有属性,例如

       name,path,domain等,都要与原Cookie完全一样。否则,浏览器将视为两个不同的Cookie

       不予覆盖,导致修改,删除失败。

 

例子:setCookie.jsp

<%@page language="java"import="java.util.*"pageEncoding="UTF-8"isErrorPage="true"%>

<jsp:directive.pageimport="java.net.URLEncoder"/>

 

<%!

   boolean isNull(String str){

       return str ==null||str.trim().length()==0;

   }

 %>

<%

   request.setCharacterEncoding("UTF-8");

   if("POST".equals(request.getMethod())){

       String name = request.getParameter("name");

       String value = request.getParameter("value");

       String maxAge = request.getParameter("maxAge");

       String domain = request.getParameter("domain");

       String path = request.getParameter("path");

       String comment = request.getParameter("comment");

       String secure = request.getParameter("secure");

       if(!isNull(name)){

           Cookie cookie = new Cookie(URLEncoder.encode

(name,"UTF-8"),URLEncoder.encode(value,"UTF-8"));

           if(!isNull(maxAge)){

               cookie.setMaxAge(Integer.parseInt(maxAge));

           }

           if(!isNull(domain)){

               cookie.setDomain(domain);

           }

           if(!isNull(path)){

               cookie.setPath(path);

           }

           if(!isNull(comment)){

               cookie.setComment(comment);

           }

           if(!isNull(secure)){

               cookie.setSecure("true".equalsIgnoreCase(secure));

           }

           response.addCookie(cookie);

       }

   }

%>

 

<!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

 <head>

   

   <title>COOKIE</title>

   <meta http-equiv="pragma"content="no-cache">

   <meta http-equiv="cache-control"content="no-cache">

   <meta http-equiv="expires"content="0">   

   <meta http-equiv="keywords"content="keyword1,keyword2,keyword3">

   <meta http-equiv="description"content="This is my page">

   <!--

   <link rel="stylesheet" type="text/css" href="styles.css">

   -->

 </head>

 

 <body>

   <div>

       <fieldset>

           <legend>当前有效的Cookie</legend>

           <scripttype="text/javascript">

               document.write(document.cookie);

           </script>

       </fieldset>

       <fieldset>

           <legend>设置新的Cookie</legend>

           <formaction="setCookie.jsp"method="post">

               <table>

                   <tr><td>name:</td><td>

<inputtype="text"name="name"style="width:200px;"/>

</td></tr>

                   <tr><td>value:</td><td>

<inputtype="text"name="value"style="width:200px;"/>

</td></tr>

                   <tr><td>maxAge:</td><td>

<inputtype="text"name="maxAge"style="width:200px;"/>

</td></tr>

                   <tr><td>domain:</td><td>

<inputtype="text"name="domain"style="width:200px;"/>

</td></tr>

                   <tr><td>path:</td><td>

<inputtype="text"name="path"style="width:200px;"/>

</td></tr>

                   <tr><td>comment:</td><td>

<inputtype="text"name="comment"style="width:200px;"/>

</td></tr>

                   <tr><td>secure:</td><td>

<inputtype="text"name="secure"style="width:200px;"/>

</td></tr>

                   <tr><td></td><td>

<inputtype="submit"value="提交"/>

<inputtype="button"value="刷新"onclick="window.location='setCookie.jsp'"/>

</td></tr>

               </table>

           </form>

       </fieldset>

   </div>

 </body>

</html>

JSESSIONIDtomcat自动生成的。

 

Cookie的不可跨域名性,这是由Cookie的隐私安全机制决定的。隐私安全机制能够禁止网站非法获取其他网站的Cookie

       正常情况下,同一个一级域名下的两个二级域名如www.helloweenvsfei.comimages.helloweenvsfei.com也不能交互使用Cookie,因为二者的域名并不严格相同。如果想所有helloweenvsfei.com名下的二级域名都可以使用该Cookie,需要设置Cookiedomain参数,例如:

       Cookie cookie = new Cookie(“time”,”20110510”);

       cookie.setDomain(“.helloweenvsfei.com”);

       cookie.setPath(“/”);

       cookie.setMaxAge(Integer.MAX_VALUE);

       response.addCookie(cookie);

可以修改本机C:\WINDOWS\system32\drivers\etc下的hosts文件来配置多个临时域名,然后使用setCookie.jsp程序来设置跨域名Cookie验证domain属性

注意:如果想要两个域名完全不同的网站共有Cookie,可以生产两个Cookiedomain属性分别为两个域名,输出到客户端。

 

0 0
原创粉丝点击