Cookie常用属性

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

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

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

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

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

Integer.MAX_VALUE表示永远有效

boolean secure:该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.page import="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);

        }

    }

%>

 

<!DOCTYPE HTML 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>

            <script type="text/javascript">

                document.write(document.cookie);

            </script>

        </fieldset>

        <fieldset>

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

            <form action="setCookie.jsp" method="post">

                <table>

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

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

</td></tr>

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

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

</td></tr>

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

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

</td></tr>

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

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

</td></tr>

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

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

</td></tr>

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

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

</td></tr>

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

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

</td></tr>

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

<input type="submit" value="提交"/>

<input type="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属性分别为两个域名,输出到客户端。

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值