http://coenraets.com/apps/cv.jsp?descriptor=cookie/source.xml
Cookieapp.jsp 文件
<%@ taglib uri="FlexTagLib" prefix="mm"%>
<SCRIPT LANGUAGE="JavaScript">
<!--
function setCookie(userId) {
document.cookie = "userId="+escape(userId);
}
function getCookie() {
var cookieStr = document.cookie;
var index = cookieStr.indexOf("userId=");
if (index == -1) return null;
index = cookieStr.indexOf("=", index) + 1;
var endstr = cookieStr.indexOf(";", index);
if (endstr == -1) endstr = cookieStr.length;
window.document.flexApp.SetVariable("userIdJS", unescape(cookieStr.substring(index, endstr)));
}
-->
</SCRIPT>
<mm:mxml name="flexApp" source="cookie.mxml"/>
cookie.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" width="300" height="400">
<mx:Script>
function setCookie() {
srv.send({action: 'set', userId: userId.text});
}
function getCookie() {
srv.send({action: 'get'});
}
function setCookieJS() {
getURL("javascript:setCookie('"+userId.text+"')");
}
function getCookieJS() {
getURL("javascript:getCookie()");
}
// callback function invoked by the getCookie() JavaScript function in cookieapp.jsp
function set userIdJS(userIdJS:String) {
userId.text=userIdJS;
}
</mx:Script>
<mx:HTTPService id="srv" url="cookie.jsp" useProxy="false" method="POST"
result="userId.text=srv.result.userId"/>
<mx:Label text="UserId:"/>
<mx:TextInput id="userId"/>
<mx:Button label="Set Cookie Value Using JSP" width="220" click="setCookie()"/>
<mx:Button label="Set Cookie Value Using JavaScript" click="setCookieJS()"/>
<mx:Button label="Get Cookie Value Using JSP" width="220" click="getCookie()"/>
<mx:Button label="Get Cookie Value Using JavaScript" width="220" click="getCookieJS()"/>
</mx:Application>
cookie.jsp
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" width="300" height="400">
<mx:Script>
function setCookie() {
srv.send({action: 'set', userId: userId.text});
}
function getCookie() {
srv.send({action: 'get'});
}
function setCookieJS() {
getURL("javascript:setCookie('"+userId.text+"')");
}
function getCookieJS() {
getURL("javascript:getCookie()");
}
// callback function invoked by the getCookie() JavaScript function in cookieapp.jsp
function set userIdJS(userIdJS:String) {
userId.text=userIdJS;
}
</mx:Script>
<mx:HTTPService id="srv" url="cookie.jsp" useProxy="false" method="POST"
result="userId.text=srv.result.userId"/>
<mx:Label text="UserId:"/>
<mx:TextInput id="userId"/>
<mx:Button label="Set Cookie Value Using JSP" width="220" click="setCookie()"/>
<mx:Button label="Set Cookie Value Using JavaScript" click="setCookieJS()"/>
<mx:Button label="Get Cookie Value Using JSP" width="220" click="getCookie()"/>
<mx:Button label="Get Cookie Value Using JavaScript" width="220" click="getCookieJS()"/>
</mx:Application>