Home > RPC > URL loader frowns “when HttpService does, why cant I ?”

URL loader frowns “when HttpService does, why cant I ?”

URL loader allows the users to handle requests and response to and from HttpServices same as what flex HttpService allows the users to do. So whatever the task can be done by HttpService, can also do by URL loader but the way they do will be different. We do have MXML HttpService tag which will invoke HttpServices like servlet in a much easier way than URL loader. Because we dont have MXML URL loader tag since its part of Flash player API so we can create an URL loader instance and call servlet using ActionScript alone.

To invoke a servlet using load method of URLLoader instance, the load method will expect a single parameter which is none other than URLRequest instance. It defines the URL in which it will request the servlet when URLLoader instance invokes the load method. As well as the event handler function for URLLoader complete event should be registered before you invoke the servlet because as you know well this event handler function will be executed once the request has been fulfilled.


var loader : URLLoader = new URLLoader();
var request : URLRequest = new URLRequest("http://localhost:8080/CallServlet/callService");
loader.addEventListener(Event.COMPLETE,resultHandler);
loader.load(request);

When we make a request to the servlet, we should also send request parameters which will be expected by the servlet when it receives the request. In URL loader we do have class named URLVariables through which we will create the request parameters as name/ value pair and finally we will set the data property of the URLRequest instance to URLVariables instance.


var param :URLVariables = new URLVariables();
param.email = email.text;
param.pass = pass.text;
request.data = param;

Thats it. Now replace the URLLoader instance code, instead of HttpService instance code in our previous example & run the application.


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="createLoader()">

 <mx:Script>
 <!&#91;CDATA&#91;
 import mx.rpc.events.FaultEvent;
 import mx.rpc.events.ResultEvent;
 import mx.controls.*;

 var loader : URLLoader;
 var request : URLRequest;

 public function createLoader():void
 {
 loader = new URLLoader();
 request = new URLRequest("http://localhost:8080/CallServlet/callService");
 loader.addEventListener(Event.COMPLETE,resultHandler);
 }
 public function callLoader():void
 {    
 var param :URLVariables = new URLVariables();
 param.email = email.text;
 param.pass = pass.text;
 request.data = param;
 loader.load(request);
 }
 public function resultHandler(event : Event):void
 {
 Alert.show(loader.data);
 }
 public function faultHandler(event : Event):void
 {
 Alert.show("error");
 }
 &#93;&#93;>
 </mx:Script>

 <!--<mx:HTTPService id="service" url="http://localhost:8080/CallServlet/callService" result="resultHandler(event)" fault="faultHandler(event)" method="GET" showBusyCursor="true" resultFormat="text">
 <mx:request xmlns="">
 <email>{email.text}</email>
<pass>{pass.text}</pass>
 </mx:request>
 </mx:HTTPService> -->

 <mx:Panel paddingLeft="20" paddingRight="20" paddingTop="20" paddingBottom="20">
 <mx:FormItem label="E-mail">
 <mx:TextInput id="email" />
 </mx:FormItem>
 <mx:FormItem label="Password">
 <mx:TextInput id="pass" displayAsPassword="true" />
 </mx:FormItem>
 <mx:Button label="Submit" click="callLoader()" />
 </mx:Panel>

</mx:Application>

Download previous example here

Note :

This post is continuity of my previous post if you could not follow this instructions. Please refer my previous post click here. Still you could not follow, drop your feedback i will try to refine the content as much as possible.

  1. June 25, 2009 at 5:47 pm

    The URLLoader seems to have a problem with XMLNS (namespace) tags returned in the data. Have you ever run into this?

    • July 1, 2009 at 2:47 pm

      So far i have not came across this issue, if i come to know i will let you know

  2. chandrasekhar
    February 24, 2011 at 8:22 am

    hi , first of all i appriciate you for this work. Thank you very much ….. It was helpfull to me.
    Plzz give one example about flex&servlet communication witth servlet response going to other flex(app) with session tracking .

  3. May 11, 2013 at 12:17 am

    Hi it’s me, I am also visiting this web page daily, this web site is truly fastidious and the users are actually sharing good thoughts.

  1. No trackbacks yet.

Leave a reply to chandrasekhar Cancel reply