jsp p3p跨域设置cookie

网上现在有很多关于跨域设置cookie的文章,但大多数都是php的文章,网上仍然有很多关于jsp p3p的设置,但是必须要将浏览器的隐私设置成低才可以跨域设置cookie.查了n多文章发现他们都搞错了!(真郁闷死了,自己没有解决干什么要贴在自己的博客中?!害得我好惨!^@^)
下面是一个例子:
首先在hosts文件中设置(其中的192.168.73.1为您本机的ip,我写成127.0.0.1不行,可能是公司的域的设置问题)
192.168.73.1       www.a.com
192.168.73.1       www.b.com
[1]编写文件
b_setcookie.jsp

  1. <%@ page contentType= "text/html; charset=utf-8"  %>   
  2. <%    
  3.    response.addHeader( "Cache-Control""no-cache" );   
  4.    response.addHeader( "Expires""Thu, 01 Jan 1970 00:00:01 GMT" );    
  5.    String ssocookie= "www.sso12345678910.com" ;   
  6.  %>   
  7. <script src= "http://www.a.com/mp/test/a_setcookie.jsp?id=<%=ssocookie%>" >   
  8. </script>  
<%@ page contentType="text/html; charset=utf-8" %>
<% 
   response.addHeader("Cache-Control", "no-cache");
   response.addHeader("Expires", "Thu, 01 Jan 1970 00:00:01 GMT"); 
   String ssocookie="www.sso12345678910.com";
 %>
<script src="http://www.a.com/mp/test/a_setcookie.jsp?id=<%=ssocookie%>">
</script>


a_setcookie.jsp

  1. <%   
  2. response.setHeader( "P3P" , "CP=\"CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR\"" );   
  3. String domainId=request.getParameter( "id" );   
  4. Cookie _cookie= new  Cookie( "test" ,domainId);   
  5. _cookie.setMaxAge( 30 * 60 * 100 );    
  6. _cookie.setPath( "/" );   
  7. response.addCookie(_cookie);   
  8. %>  
<%
response.setHeader("P3P","CP=\"CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR\"");
String domainId=request.getParameter("id");
Cookie _cookie=new Cookie("test",domainId);
_cookie.setMaxAge(30*60*100); 
_cookie.setPath("/");
response.addCookie(_cookie);
%>


a_getcookie.jsp

  1. <%@ page contentType= "text/html; charset=utf-8"  %>   
  2. <%   
  3.    Cookie cookies[]=request.getCookies();  // 将适用目录下所有Cookie读入并存入cookies数组中   
  4.     
  5.    Cookie sCookie= null ;    
  6.    String sname= null ;   
  7.    String name= null ;   
  8.     if (cookies== null // 如果没有任何cookie   
  9.      out.print( "none any cookie" );   
  10.     else   
  11.    {   
  12.      out.print(cookies.length +  "<br>" );   
  13.       for ( int  i= 0 ;i<cookies.length; i++)  // 循环列出所有可用的Cookie   
  14.      {   
  15.        sCookie=cookies[i];   
  16.        sname=sCookie.getName();   
  17.        name = sCookie.getValue();   
  18.        out.println( "comment==>>>" +sCookie.getComment()+ "\n" );   
  19.        out.println( "getDomain==>>>" +sCookie.getDomain()+ "\n" );   
  20.        out.println( "getSecure==>>" +sCookie.getSecure()+ "\n" );   
  21.        out.println( "getVersion==>>" +sCookie.getVersion()+ "\n" );   
  22.        out.println( "cookiename==>>" +sname +  "->"  +  "cookievalue==>>>" +name +  "<br>" );   
  23.      }   
  24.    }    
  25. %>  
<%@ page contentType="text/html; charset=utf-8" %>
<%
   Cookie cookies[]=request.getCookies(); // 将适用目录下所有Cookie读入并存入cookies数组中
 
   Cookie sCookie=null; 
   String sname=null;
   String name=null;
   if(cookies==null) // 如果没有任何cookie
     out.print("none any cookie");
   else
   {
     out.print(cookies.length + "<br>");
     for(int i=0;i<cookies.length; i++) // 循环列出所有可用的Cookie
     {
       sCookie=cookies[i];
       sname=sCookie.getName();
       name = sCookie.getValue();
       out.println("comment==>>>"+sCookie.getComment()+"\n");
       out.println("getDomain==>>>"+sCookie.getDomain()+"\n");
       out.println("getSecure==>>"+sCookie.getSecure()+"\n");
       out.println("getVersion==>>"+sCookie.getVersion()+"\n");
       out.println("cookiename==>>"+sname + "->" + "cookievalue==>>>"+name + "<br>");
     }
   } 
%>


测试时依次请求
http://www.b.com/mp/test/b_setcookie.jsp
http://www.a.com/mp/test/a_getcookie.jsp
便可看到通过跨域设置的cookie的值!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值