雪域幽狐 2011-12-20 18:47
JSONP的一个应用,可以跨站获取Cookie
在A站提供一个获取A站 Cookie 的连接
在B站就可以通过以上方式获取了
A站代码示例:
[code] Cookie[] cookies = request.getCookies();
StringBuffer sbCookie = new StringBuffer();
JSONObject json = new JSONObject();
sbCookie.append(request.getParameter("callback")).append("(");
response.reset();
OutputStreamWriter ow = new OutputStreamWriter(
response.getOutputStream());
for (Cookie cookie : cookies)
{
try
{
json.put(cookie.getName(), cookie.getValue());
}
catch (JSONException e)
{
e.printStackTrace();
}
}
sbCookie.append(json.toString()).append(")");
ow.write(sbCookie.toString());
ow.flush();[/code]
在B站,获取这个连接信息回调即可
KISSY提供了一个通用的获取JS方法
参考代码如下:
[code]
function a(data)
{
alert(data.UserName);
}
KISSY.getscript("http://172.16.16.91:8080/test/servlet/GetCookie?callback=a");[/code]
KISSY.getscript连接地址