Home > RPC > HttpService says “I can invoke the servlet”

HttpService says “I can invoke the servlet”

Hi everybody, its my pleasure to come up with another interesting blog that is how to invoke a servlet from flex application using HttpService. Let me start with HttpService which is responsible for handling request and response to and from http services like servlet. Now we are bound to know how servlet works, dont get panic !!! its easy let me explain.

Servlet – a glance

Whenever the client requests resource from web server (Apache), the web server will fetch the resource and send it as a response to the client. But the web server will love to serve as long as the requested resource is static content. If the client request any dynamic content which will not be available until the request is made, the web server cant do it alone so that it need help from some helper application like servlet which is capable of generating just in time resource i.e. dynamic content.

Who is the manager of servlet ?

Now you must know the person who is managing servlet who is none other container. the role of container has to create an instance of servlet before any one requests the servlet and whenever the client requests the servlet the container will intercept and create new thread of the requested servlet and also it will handover the request parameters to the servlet class

Note :

One could not explain the concept of servlet and container in single post because its too vast, i hereby just given idea about servlet and how it works. If you want to know more there are plenty of forums available where you can know everything about these stuffs.

Now come back to our concept. To invoke a servlet from flex you have to create HttpService component, Servlet class and at last you have to map the URL to Servlet in deployment descriptor.

Before you do all these stuffs, set up your web application in Tomcat , create flex project and configure server location of your flex application. Please refer to my previous post if you dont know how to do it, click here

Create HttpService component

  • Create HttpService instance in your MXML application file, name the instance and define the event handler function name for result and fault property.
  • specify the url which will invoke the servlet class when user invokes the service.
  • Define the http method of your request e.g. GET, POST etc.
  • Specify the request parameters of your HttpService which will passed to the servlet and servlet will do some process with the data. It can be specified using request property of HttpService.
  • Define the event handler function for result and fault inside <mx:script> tag.
  • The response of the servlet will be handled by either result event handler function or fault event handler function depends on the result of servlet invocation.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">

 import mx.rpc.events.FaultEvent;
 import mx.rpc.events.ResultEvent;

 import mx.controls.*;

 public function resultHandler(event : ResultEvent):void

 public function faultHandler(event : FaultEvent):void


 <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="">

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

 <mx:Button label="Submit" click="service.send()" />


In the code you are seeing above, i just created HttpService instance and the request parameter are the users input of email address and password which will be passed to servlet once the user invokes servlet by clicking “Submit” button.

Create, compile & deploy servlet

  • Create a java class which should extend HttpServlet Class
  • Define doGet method if you have declared GET in the method property of HttpService instance else doPost.
  • Get the request parameters sent by the HttpService instance
  • Do some process with request parameters
  • Compile the servlet class
  • Finally copy your_servlet.class and paste this file in to {Tomcat_home_directory}\webapps\{Web_application_name}\WEB-INF\classes folder of your web application.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class MyServlet extends HttpServlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
String email = request.getParameter("email");
String password = request.getParameter("pass");
String result = "Hi iam servlet, I am here to conform your username & password, they are " + email + " & " + password + ". is it correct ?";
PrintWriter out = response.getWriter(); out.println(result);

In the servlet class above, i have implemented doGet() method since i have declared GET in the method property of HttpService instance. I am also getting the request parameters , construct a string which asks user whether the email address and password they have entered are same as it displays and finally it returns that string as response to the HttpService request.

Map URL to servlet

Actually the servlet will have three names. They are public URL, internal name and actual servlet path . The public URL and internal name wont be necessary to same as actual servlet path. Internal name will be chosen by the developer who deploys servlet and it will be internally used in the deployment descriptor. The public URL is the one which users knows and when they request the container will find the internal name of the requested servlet and using internal name the container will find the actual path of the servlet. here the example goes. Please add the following code in the web.xml before </webpp> which is present under

{Tomcat_home_directory}\webapps\{Web_application_name}\WEB-INF folder of your web application.



Tags & Definitions

<url-pattern> – defines the relative public URL

<servlet-name> – defines the internal name

<servlet-classs> – defines the fully qualified class name.

Yes we did it all, now restart your tomcat server, run your flex application, input email address, password, click Submit button and see the response of your servlet.

Download this application

Note : Download the application, deploy it in the webapps of your tomcat home directory. Copy the url below and paste in the address bar of your web application and run the application. http://localhost:8080/CallServlet/HttpRequest-debug/HttpRequest.html

  1. devendra
    November 5, 2009 at 9:36 am

    greate tutorial ….

  2. December 12, 2009 at 12:45 am

    Premium article, amazing looking blog, added it to my favorites!!

    • Mahesh
      December 17, 2009 at 1:35 pm

      Thank you very much nelson.

  3. Prem
    February 19, 2010 at 4:29 am

    Thanks a lot for your great tutorial…i followed your steps.There where no errors but when i open in browser i cant access the servlet.in your example what this path specify(http://localhost:8080/CallServlet/callService).What is this CallServlet and CallService?? can u help me in ths..thanks in advance 🙂

  4. rakhi
    March 1, 2010 at 6:36 am

    This is really a nice artical.
    But can you just let me know how can i call an URL of another application
    from flex.Means it will a single sign on sort of.
    please tell me how to do this?

    • Mahesh
      March 2, 2010 at 8:20 am

      @rakhi : I dint get you. Can you bit more clear ?

  5. Rizwan
    July 19, 2010 at 6:56 am

    This is a nice tutorial. It helped me in handling request

  6. http://mensengagementrings.ca
    March 20, 2012 at 1:35 am

    Very well written article. It will be beneficial to anyone who employess it, including me. Keep up the good work – looking forward to more posts.

  1. May 20, 2009 at 5:46 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: