Flex(通过URLLoader)与后台jsp进行交互

初学flex,写个flex(通过URLLoader)与后台交互的例子,解决中午乱码的处理:登录flash页面

后台jsp接受到的flex传入的中文

FB里面显示从jsp里面传出的中文信息

这是flex.jsp的代码:

<% @ page contentType="text/html; charset=UTF-8"  %>
<%
    
String usr = java.net.URLDecoder.decode(request.getParameter("user"), "UTF-8");
    System.out.println(
"取得传入的参数:"+usr);
    
String psw = java.net.URLDecoder.decode(request.getParameter("psw"), "UTF-8");
    System.out.println(
"取得传入的参数:"+psw);
%>
<% = usr %> + <% = psw %>

 

这是login.mxml的代码:

 

<? xml version="1.0" encoding="utf-8" ?>
< mx:Application  xmlns:mx ="http://www.adobe.com/2006/mxml"  
    layout
="absolute"  fontSize ="12"  xmlns:local ="*" >
    
< mx:TraceTarget />
    
< mx:Style >
        .lab{
           fontWeight: "bold";
           /*color: #FF0000;*/
           fontSize: 15;
        }
    
</ mx:Style >
    
< mx:Script >
        
<![CDATA[
        //对提交给后台的参数进行UTF-8的编码处理
        private function httpEncoding(param:String):String{
            return encodeURIComponent(param);
        }
        private function doLogin():void {
            //trace("focusEnabled:"+loading.focusEnabled);
            //this.focusManager.setFocus(user);
            var url:String = "http://localhost:8600/flex.jsp";
            var params:URLVariables = new URLVariables();
            //这个user,psw就是传入后台的参数user,jsp就用 request.getParameter("user")来取
            params.user = httpEncoding(user.text);
            params.psw = psw.text;
            var loader:URLLoader = new URLLoader();
            this.configureEventListeners(loader);
            //可以不设置,因为默认是text
            loader.dataFormat = URLLoaderDataFormat.TEXT;
            var request:URLRequest = new URLRequest(url);
            request.data = params;
            try{
                loader.load(request);
            }catch(error:Error){
                trace(error.message);
            }
        }
        private function configureEventListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, completeHandler);
            dispatcher.addEventListener(Event.OPEN, openHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        }
       private function completeHandler(event:Event):void {
            var loader:URLLoader = URLLoader(event.target);
            trace("--complete..."+event.target.data);
            //var dataXML:XML = XML(event.target.data);
            //trace(dataXML.toXMLString());
            btn_btn.enabled=true;
        }

        private function openHandler(event:Event):void {
            trace("openHandler: " + event);
            //this.focusManager.setFocus(loading);
            btn_btn.enabled=false;
        }

        private function progressHandler(event:ProgressEvent):void {
            trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
        }

        private function securityErrorHandler(event:SecurityErrorEvent):void {
            trace("securityErrorHandler: " + event);
        }

        private function httpStatusHandler(event:HTTPStatusEvent):void {
            trace("httpStatusHandler: " + event);
        }

        private function ioErrorHandler(event:IOErrorEvent):void {
            trace("ioErrorHandler: " + event);
        }
        
]]>
    
</ mx:Script >
    
    
< mx:Panel  title ="欢迎登录WAP管理系统:"  width ="307"  height ="189"  layout ="absolute"  verticalAlign ="top"  horizontalCenter ="-10.5"  verticalCenter ="-9" >
        
< mx:Label  x ="32"  y ="25"  text ="登录口令:"  width ="59" />
        
< mx:TextInput  id ="user"  x ="99"  y ="23"  width ="147" />
        
< mx:Label  x ="32"  y ="53"  text ="登录密码:"  width ="59" />
        
< mx:TextInput  id ="psw"  x ="99"  y ="51"  displayAsPassword ="true"  width ="147" />
        
< mx:ControlBar  alpha ="1" >
            
< mx:Button  id ="btn_btn"  x ="58"  y ="92"  label ="确 定"  click ="this.doLogin();" />
            
< mx:Button  x ="162"  y ="92"  label ="取 消" />
            
< mx:Label  x ="0"  y ="129"  text ="Powered by Keren"  textAlign ="right" />
        
</ mx:ControlBar >
    
</ mx:Panel >
</ mx:Application >

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值