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...