初步认识Flex如何处理Seesion

 

    在学习Flex如何处理Session问题,发现Seesion是在LCDS基础上实现。HTTP-based 通道连接servlet-based 端点远程对象服务,Session是一个JAVA servlet HTTP Session 对象。HTTP-based 通道连接NIO-based端点远程对象服务,Session是一个支持FlexSession APIHTTP Session,但与servlet HTTP Session对象脱离。

     Flex如何取得session,关键是LCDS为开发者提供FlexSession类。

编写一个JAVA 类:

package com.les.java3006;

 

import flex.messaging.FlexContext;

import flex.messaging.FlexSession;

import flex.messaging.client.FlexClient;

 

 

public class ConnectSession {

        

         public FlexSession _FlexSession;

         public FlexClient  _FlexClient; 

        

         public ConnectSession(){

                   _FlexSession=FlexContext.getFlexSession();//获取Session 对象

                   _FlexClient=FlexContext.getFlexClient();   //获取Client 对象

         }

        

    //获取FlexSessionID

         public String getFlexSession () throws Exception{

                   String s = new String();

                   s=_FlexSession.getId();

                   System.out.println("Flex Session:"+_FlexSession.getId());

                   return s;

         }

 

    //获取FlexClientID      

         public String getFlexClient () throws Exception{

                   String s = new String();

                   s=_FlexClient.getId();

                   System.out.println("Flex Client:"+_FlexClient.getId());

                   return s;

         }

        

    //设置Session

         public Boolean setSession (String _sessionName,Object _sessionKey) throws Exception{

                   try{

                            _FlexSession.setAttribute(_sessionName, _sessionKey);

                   }catch (Exception e) {

                            System.out.println("Set Session error!");

                            return false;

                   }

                   return true;

         }

    //根据命名获取Session

         public Object getSession (String _sessionName) throws Exception{

                   Object obj=new Object();

                   try{

                            obj=_FlexSession.getAttribute(_sessionName);

                   }catch (Exception e) {

                            System.out.println("Get Session error!");

                   }

                   return obj;

         }

}

 

配置remoting-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<service id="remoting-service"

    class="flex.messaging.services.RemotingService">

 

    <adapters>

        <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>

    </adapters>

 

    <default-channels>

        <channel ref="my-amf"/>

    </default-channels>

        

         <destination id="echo2005S">

             <channels>

             <channel ref="my-nio-amf"/>

        </channels>

        <properties>

             <source>com.les.java3006.ConnectSession</source>

                             <scope>request</scope>

        </properties>              

    </destination>

</service>

 

 

设置destinationscope时,关于设置值有一段在叙述远程对象访问文字:

Indicates whether the object is available in the request scope, the application scope, or the session scope.

 

Use the request scope when you configure a Remoting Service destination to access stateless objects. With the request scope, the server creates an instance of the Java class on each request. Use the request scope if you are storing the object in the application or session scope causes memory problems.

 

When you use the session scope, the server creates an instance of the Java object once on the server for the session. For example, multiple tabs in the same web browser share the same session. If you open a Flex application in one tab, any copy of that application running in another tab accesses the same Java object.

 

When you use the application scope, the server creates an instance of the Java object once on the server for the entire application.

 

Flex调用JAVA远程对象处理FlexSession,以下是一个测试代码Flex2004.mxml

<?xml version="1.0" encoding="utf-8"?>

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

                               xmlns:s="library://ns.adobe.com/flex/spark"

                               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"

                               creationComplete="application1_creationCompleteHandler(event)">

         <s:layout>

                   <s:BasicLayout/>

         </s:layout>

 

         <fx:Script>

                   <![CDATA[

                            import mx.events.FlexEvent;

                            import mx.rpc.AbstractOperation;

                            import mx.rpc.events.FaultEvent;

                            import mx.rpc.events.ResultEvent;

                            import mx.rpc.remoting.RemoteObject;

                           

                            private var ro:RemoteObject;

                            private var _operand:int;

                           

                            protected function application1_creationCompleteHandler(event:FlexEvent):void

                            {

                                     // 定义一个远程对象

                                     var op1:AbstractOperation;

                                     var op2:AbstractOperation;

                                     var op3:AbstractOperation;

                                     var op4:AbstractOperation;

                                     ro=new RemoteObject();

                                     ro.destination="echo2005S";

 

                                     //调用setSession方法

                                     op1=ro.getOperation("setSession");

                                     op1.addEventListener("result",setSessionIdResultHandler1);

                                     op1.addEventListener("fault",getSessionIdFaultHandler);

                                    

//调用getSession方法                                 

                                     op2=ro.getOperation("getSession");

                                     op2.addEventListener("result",getSessionIdResultHandler2);

                                     op2.addEventListener("fault",getSessionIdFaultHandler);

                                     ro.getSession("name");

 

                                     //调用getFlexSession方法

                                     op3=ro.getOperation("getFlexSession");

                                     op3.addEventListener("result",getSessionIdResultHandler3);

                                     op3.addEventListener("fault",getSessionIdFaultHandler);

                                     ro.getFlexSession();

                                    

                 //调用getFlexClient方法

                                     op4=ro.getOperation("getFlexClient");

                                     op4.addEventListener("result",getSessionIdResultHandler3);

                                     op4.addEventListener("fault",getSessionIdFaultHandler);

                                     ro.getFlexClient();

                            }

                           

                            protected function setSessionIdResultHandler1(event:ResultEvent):void{

                                     trace(event.result as Boolean);

                            }

                            //取得命名为“name”的Session值,值加一后再保存Session值。

                            protected function getSessionIdResultHandler2(event:ResultEvent):void{        

                                     if (event.result==null){

                                               ro.setSession("name",0);                                       

                                     }else{

                                               _operand=event.result as int;

                                               trace(_operand);

                                               _operand++;

                                               IDlabel.text="标签:"+_operand;

                                               ro.setSession("name",_operand);

                                     }                                   

                            }

                           

             //获取FlexClientFlexSession并前台显示。

                            protected function getSessionIdResultHandler3(event:ResultEvent):void{

                                     trace(event.result as String);

                                     var op:AbstractOperation;

                                     op=event.currentTarget as AbstractOperation;

                                     if (op.name=="getFlexSession") {

                                               _fsid.text=event.result as String;

                                     }else{

                                               _fcid.text=event.result as String;

                                     }

                            }

 

                            //按钮事件处理函数。

                            private function Plus():void{

                                     ro.getSession("name");

                            }

                           

                            protected function getSessionIdFaultHandler(event:FaultEvent):void{

                                     trace("error");

                            }

                   ]]>

         </fx:Script>

 

         <fx:Declarations>

                   <!-- 将非可视元素(例如服务、值对象)放在此处 -->

         </fx:Declarations>

         <s:Label x="75" y="45" text="标签:0" width="594" height="20" id="IDlabel"/>

         <s:Button x="75" y="193" label="Plus" id="_btn" click="Plus();"/>

         <s:Label x="75" y="79" text="FlexClient ID" width="79" height="19" verticalAlign="middle"/>

         <s:Label x="75" y="102" text="FlexSession ID" width="93" height="17" verticalAlign="middle"/>

         <s:Label x="179" y="79" text="标签" height="19" width="426" id="_fcid"/>

         <s:Label x="179" y="104" text="标签" height="19" width="426" id="_fsid"/>

</s:Application>

 

为更容易理解FlexSessionFlexClient的关系,俺将两个Flex2004.swf放于一个页面,并命名为Flex2005.mxml

 

<?xml version="1.0" encoding="utf-8"?>

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

                               xmlns:s="library://ns.adobe.com/flex/spark"

                               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

         <s:layout>

                   <s:BasicLayout/>

         </s:layout>

         <fx:Declarations>

                   <!-- 将非可视元素(例如服务、值对象)放在此处 -->

         </fx:Declarations>

         <mx:TabNavigator x="73" y="75" width="643" height="448">

                   <s:NavigatorContent label="选项卡1" width="100%" height="100%">

                            <mx:SWFLoader x="10" y="10" width="621" height="394" id="_swfload01" source="Flex2004.swf"/>

                   </s:NavigatorContent>

                   <s:NavigatorContent label="选项卡2" width="100%" height="100%">

                            <mx:SWFLoader x="10" y="10" width="622" height="394" id="_swfload02" source="Flex2004.swf"/>

                   </s:NavigatorContent>

         </mx:TabNavigator>

</s:Application>

 

Scope设置为request值的运行结果:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

分别在三个IE和多个Tab中运行Flex2005

请留意他们的标签值和FlexClient ID,FlexSession ID

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值