加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

flex的与web cookie、session通信的方式

发布时间:2020-12-15 05:17:34 所属栏目:百科 来源:网络整理
导读:? 由于现在大多数所谓flex项目都是嵌入在web中的, 所以, 经常需要与web的session与cookie通信。我们是将swf嵌入到一个iframek中, 由swf去读 cookie以保证有效性。 ? ? ? Flex通过javascript读写cookie 方式如下 最近遇到个问题,开发web项目的时候,可能f
?

由于现在大多数所谓flex项目都是嵌入在web中的, 所以, 经常需要与web的session与cookie通信。我们是将swf嵌入到一个iframek中, 由swf去读 cookie以保证有效性。

?

?

?

Flex通过javascript读写cookie 方式如下
最近遇到个问题,开发web项目的时候,可能flex只用来实现项目的部分模块。当flex需要在客户端写入/读取一些状态信息的时候,我们会想到用cookie。flex是不支持cookie的,只有SharedObject这个本地对象。所以解决的办法就有两个:

1.flex通过调用js来实现对cookie的操作;
2.js通过flex实现对SharedObject的操作;
这两种方法的基础就是实现flex和javascript的交互,自己试着写了个小例子,实现了第一种方法,直接上代码:

Flex_Js_Cookie.js:

?
view plaincopy to clipboardprint?
01.function SetCookie(name,value)??
02.{??
03.??? document.cookie = name+"="+escape(value);??
04.};??
05.function GetCookie(name)??
06.{??
07.??? var arr = document.cookie.match(new RegExp("(^|)"+name+"=([^;]*)(;|$)"));??
08.??? alert(arr.length);??
09.??? if(arr != null)??
10.??? {??
11.??????? return unescape(arr[2]);??
12.??? }??
13.}?
function SetCookie(name,value)
{
?document.cookie = name+"="+escape(value);
};
function GetCookie(name)
{
?var arr = document.cookie.match(new RegExp("(^|)"+name+"=([^;]*)(;|$)"));
?alert(arr.length);
?if(arr != null)
?{
??return unescape(arr[2]);
?}
}

Flex_Js_Cookie.html:

view plaincopy to clipboardprint?
01.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">?
02.<html xmlns="http://www.w3.org/1999/xhtml">?
03.<head>?
04.<head>?
05.<title></title>?
06.<script src="swfobject.js" type="text/javascript"></script>?
07.<script src="Flex_Js_Cookie.js" type="text/javascript"></script>?
08.<script language=javascript>?
09.var flashvars = {};??
10.??????? var params = {??
11.??????????? menu: "false",??
12.??????????? scale: "noScale",??
13.??????????? allowFullscreen: "true",??
14.??????????? allowScriptAccess: "always",??
15.??????????? bgcolor: "#FFFFFF"??
16.??????? };??
17.??????? var attributes = {id:"swfplayer"};??
18.??????? swfobject.embedSWF("Flex_Js_Cookie.swf","swfplayer","500","350","9.0.0","expressInstall.swf",flashvars,params,attributes);??
19.</script>?
20.</head>?
21.<body>?
22.<div id="swfplayer"></div>?
23.</body>?
24.</html>?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<head>
<title></title>
<script src="swfobject.js" type="text/javascript"></script>
<script src="Flex_Js_Cookie.js" type="text/javascript"></script>
<script language=javascript>
var flashvars = {};
??var params = {
???menu: "false",
???scale: "noScale",
???allowFullscreen: "true",
???allowScriptAccess: "always",
???bgcolor: "#FFFFFF"
??};
??var attributes = {id:"swfplayer"};
??swfobject.embedSWF("Flex_Js_Cookie.swf",attributes);
</script>
</head>
<body>
<div id="swfplayer"></div>
</body>
</html>
?

Flex_Js_Cookie.mxml:

view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="utf-8"?>?
02.<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">?
03.??? <mx:Script>?
04.??????? <![CDATA[?
05.??????????? import flash.external.*;?
06.??????????? import mx.controls.Alert;?
07.?????????????
08.??????????? public function GetCookie():void?
09.??????????? {?
10.??????????????? var jsFunction:String = "GetCookie";?
11.??????????????? var key:String = txt_key.text;?
12.??????????????? if(ExternalInterface.available)?
13.??????????????? {?
14.??????????????????? var value:String = ExternalInterface.call(jsFunction,key);?
15.??????????????????? txt_value.text = value;?
16.??????????????? }?
17.??????????? }?
18.?????????????
19.??????????? public function SetCookie():void?
20.??????????? {?
21.??????????????? var jsFunction:String = "SetCookie";?
22.??????????????? var key:String = txt_key_set.text;?
23.??????????????? var value:String = txt_value_set.text;?
24.??????????????? if(ExternalInterface.available)?
25.??????????????? {?
26.??????????????????? ExternalInterface.call(jsFunction,key,value);?
27.??????????????? }?
28.??????????? }?
29.??????? ]]>?
30.??? </mx:Script>?
31.??? <mx:Label x="51" y="39" text="cookie名:" fontSize="12"/>?
32.??? <mx:Label x="51" y="86" text="cookie值:" fontSize="12"/>?
33.??? <mx:TextInput x="122" y="39" id="txt_key"/>?
34.??? <mx:Button x="301" y="84" label="确定" fontSize="12" click="GetCookie()"/>?
35.??? <mx:TextInput x="122" y="86" id="txt_value" enabled="false"/>?
36.??? <mx:Label x="51" y="183" text="cookie名:" fontSize="12"/>?
37.??? <mx:Label x="51" y="226" text="cookie值:" fontSize="12"/>?
38.??? <mx:TextInput x="122" y="183" id="txt_key_set"/>?
39.??? <mx:TextInput x="122" y="226" id="txt_value_set"/>?
40.??? <mx:Button x="301" y="226" label="确定" fontSize="12" click="SetCookie()"/>?
41.??? <mx:Label x="51" y="155" text="设置cookie" fontSize="12" color="#C42A2A"/>?
42.??? <mx:Label x="51" y="11" text="取得cookie" fontSize="12" color="#C42A2A"/>?
43.??????
44.</mx:Application>?
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
?<mx:Script>
??<![CDATA[
???import flash.external.*;
???import mx.controls.Alert;
???
???public function GetCookie():void
???{
????var jsFunction:String = "GetCookie";
????var key:String = txt_key.text;
????if(ExternalInterface.available)
????{
?????var value:String = ExternalInterface.call(jsFunction,key);
?????txt_value.text = value;
????}
???}
???
???public function SetCookie():void
???{
????var jsFunction:String = "SetCookie";
????var key:String = txt_key_set.text;
????var value:String = txt_value_set.text;
????if(ExternalInterface.available)
????{
?????ExternalInterface.call(jsFunction,value);
????}
???}
??]]>
?</mx:Script>
?<mx:Label x="51" y="39" text="cookie名:" fontSize="12"/>
?<mx:Label x="51" y="86" text="cookie值:" fontSize="12"/>
?<mx:TextInput x="122" y="39" id="txt_key"/>
?<mx:Button x="301" y="84" label="确定" fontSize="12" click="GetCookie()"/>
?<mx:TextInput x="122" y="86" id="txt_value" enabled="false"/>
?<mx:Label x="51" y="183" text="cookie名:" fontSize="12"/>
?<mx:Label x="51" y="226" text="cookie值:" fontSize="12"/>
?<mx:TextInput x="122" y="183" id="txt_key_set"/>
?<mx:TextInput x="122" y="226" id="txt_value_set"/>
?<mx:Button x="301" y="226" label="确定" fontSize="12" click="SetCookie()"/>
?<mx:Label x="51" y="155" text="设置cookie" fontSize="12" color="#C42A2A"/>
?<mx:Label x="51" y="11" text="取得cookie" fontSize="12" color="#C42A2A"/>
?
</mx:Application>

?

?

?

同时,我们还使用了blazeDS,使用了remoteObject与java进行通信。 所以经blazeDS后,在java端对session及cookie 操作如下

?

?public HttpServletRequest request;
?public HttpServletResponse response;
?public FlexSession flexSession;
?public HttpSession session;

?

?

request = FlexContext.getHttpRequest(); ??????? session = request.getSession(); ??? ?session.setAttribute("userId",uid); ??session.setAttribute("title",role); ?? ??response = FlexContext.getHttpResponse(); ??addCookie(response,"userId",String.valueOf(uid)); ??addCookie(response,"title",role);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读