P3p script SRC solves Cross-Domain Cookie Access problems Java Ultimate Solution

Source: Internet
Author: User

Recently, I was working on a project where my website is www.a.com, but I had to get the cookie from www. B .com for a long time. I worked with my colleagues. It's done.
I used Java to add the following words to the www.a.com/a.vmfile:

<SCRIPT type = "text/JavaScript" src = "http://www. B .com/common

/Cart/forpersate. do? Act = getsidanduserid "> </SCRIPT>


The link in SRC can be a page or an action in B. I use an action. In the action of B .com, my code is as follows:
Public actionforward getsidanduserid (actionmapping mapping, actionform form,
Httpservletrequest request, httpservletresponse response ){
String cartsid = This. getcartsid (request, response );
Usersession = This. getusersession (request, response );
Cartservicedelegate delegate = new cartservicedelegate ();
Int basketsize = delegate. getbasketsize (usersession, cartsid );
Stringbuffer buffer = new stringbuffer ();
Buffer. append ("ibacketitemsnumber =" + basketsize + ";");
Printwriter out;
Response. setheader ("p3p", "cp =/" non DSP cor Cura ADMA Deva taia psaa psda ivaa ivda ConA hisa TELA otpa our unra ind uni com nav int DEM

CNT pre loc /"");
Response. setheader ("Pragma", "No-Cache ");
Response. setheader ("cache-control", "No-Cache ");
Try {
Out = response. getwriter ();
Out. Print (buffer. tostring ());
} Catch (ioexception e ){
Log. Error ("", e );
}
Return NULL;
}

In this way, you can get the desired result.

In the. VM file, if you view the source code, you will see

<SCRIPT type = "text/JavaScript" src = "http://www. B .com/common

/Cart/forpersate. do? Act = getsidanduserid "> ibacketitemsnumber = 1; </SCRIPT>

Then a. VM can write the following code to obtain the data:

<SCRIPT type = "text/JavaScript"> document. Write ('& nbsp;' + ibacketitemsnumber + '& nbsp;'); </SCRIPT>

In this way, you can get the desired data.

Appendix:

The reason why I add the following code to action is:
Response. setheader ("p3p ", "cp =/" non DSP cor Cura ADMA Deva taia psaa psda ivaa ivda ConA hisa TELA otpa our unra ind uni com nav int dem cnt pre loc /"");

In IE7 (IE6 and firfox are normal, only IE7 and later versions have this problem), because the browser's privacy level is "medium" by default in IE7 ", it will prevent the storage of third-party cookies that can be used to contact you without your explicit consent. Therefore, the cross-origin cookie retrieval function cannot be implemented unless you manually set the browser's privacy level to "low" by default in IE7 ". The above line of code can solve this problem, where the p3p technology is used.
I am writing this article, hoping to help you...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.