Making resource request from portlet using Flex application

I built this sample portlet in which i am making a resource request to the portlet. My portlet is very simple the doView() method returns HTML that includes a Flex application and then the flex application makes HTTP call to the portlet.

The interesting part was since the portlets are not URL addressable i cant hard code the resource URL inside my flex application so what i did is i am generating the resource URLs on the HTML and then making use of Flex to JavaScript communication capability to first get the Resource URL and then the FLex application was making HTTP call using action script. This is how my demo portlet looks like, you can download the sample code from here

  1. Flex source code

  2. FlexPortlet.war - Portlet Source code

I followed these steps to build the demo application

  • First create a file like this, as you can see in the doView() method of the portlet i am passing control to the flex.jsp and in the serveResource() method i am returning "Returning response from FlexPortlet.serveResource" message.

    package com.webspherenotes.flex;

    import javax.portlet.ActionRequest;
    import javax.portlet.ActionResponse;
    import javax.portlet.GenericPortlet;
    import javax.portlet.PortletException;
    import javax.portlet.RenderRequest;
    import javax.portlet.RenderResponse;
    import javax.portlet.ResourceRequest;
    import javax.portlet.ResourceResponse;

    public class FlexPortlet extends GenericPortlet {
    protected void doView(RenderRequest request, RenderResponse response)
    throws PortletException, IOException {
    getPortletContext().getRequestDispatcher("/flex.jsp").include(request, response);
    System.out.println("Exiting FlexPortlet.doView()");
    public void serveResource(ResourceRequest request, ResourceResponse response)
    throws PortletException, IOException {
    System.out.println("Entering FlexPortlet.serveResource()");
    response.getWriter().println("Returning response from FlexPortlet.serveResource");
    System.out.println("Exiting FlexPortlet.serveResource()");

  • This how the flex.jsp code looks like

    <%@taglib uri="" prefix="portlet-client-model"%>
    <%@taglib uri="" prefix="portletx"%>
    <portletx:defineObjects />
    <script type="text/javascript">
    function getResourceURL(){
    console.log("Returning resourceURL");
    return "<%=renderResponse.createResourceURL().toString()%>";
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
    id="HelloWorld" width="100%" height="100%"
    <param name="movie" value='<%= renderResponse.encodeURL(renderRequest.getContextPath() + "/HelloWorld.swf") %>' />
    <param name="quality" value="high" />
    <param name="bgcolor" value="#869ca7" />
    <param name="allowScriptAccess" value="sameDomain" />
    <embed src="<%= renderResponse.encodeURL(renderRequest.getContextPath() + "/HelloFlex.swf") %>" quality="high" bgcolor="#869ca7"
    width="100%" height="100%" name="HelloWorld" align="middle" play="true" loop="false" quality="high" allowScriptAccess="sameDomain"
    type="application/x-shockwave-flash" pluginspage="">

    The JSP code is pretty simple it has a getResourceURL() javascript method that will be called by Flex application and it is including HelloFlex.swf in the markup.

  • The biggest chunk of business logic is in the HelloFlex.mxml, this is how it looks like, please note that i am not Flex expert and i believe a Flex expert might be able to build much better code here. In my case both application layout and business logic is in the same file

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx=""
    width="100%" height="100%" >
    import flash.external.*;
    import mx.controls.Alert;
    import mx.rpc.http.HTTPService;

    //Event handler for handling button click
    public function getPortletResponse():void
    var javaScriptMethodName:String = "getResourceURL";
    if (ExternalInterface.available) {
    // Make call to the getResourceUrl javaScript function
    var portletUrl:String =;
    } else
    displayText.text = "Error sending data!";
    //This method is used for making a URL call
    private var service:HTTPService
    public function useHttpService(url:String):void {
    service = new HTTPService();
    service.url = url;
    service.resultFormat = HTTPService.RESULT_FORMAT_TEXT;
    service.method = "GET";
    service.addEventListener("result", httpResult);
    service.addEventListener("fault", httpFault);
    //This callback gets called in case if the http response is returned
    public function httpResult(event:ResultEvent):void {
    var result:Object = event.result;
    displayText.text = event.result.toString();
    //This call back gets called in case of error in http request
    public function httpFault(event:FaultEvent):void {
    var faultstring:String = event.fault.faultString;
    displayText.text = event.fault.faultDetail; + event.fault.faultDetail + event.fault.rootCause);
    <mx:Panel id="pnlMain" x="333" y="10" width="514" height="144" layout="absolute" title="Serve Resource Example">
    <mx:Button label="Make Resource request" y= "75" id="resourceUrl" click="getPortletResponse()" x="10"/>
    <mx:TextArea x="10" y="10" width="493" height="61" id="displayText"/>

    My application layout is pretty simple it has a button and a Text Area, when we click on button the control gets passed to getPortletResponse() method and that method will take the output generated by the portlet and write it in the text area.

    The getPortletResponse() method is calling the getResourceURL() javascript function by using ExternalInterface; like this, the getResourceUrl() method will return the resourceUrl generated by the RenderResponse.createResourceURL() method and then it is passing the control to useHttpService() method.

    The useHttpService() method is making HTTP "GET" call to the resourceUrl and then it is attaching the httpResult() function as a callback function for the ajax request the httpFault() method gets called in case of error in HTTP call.


srjwebsolutions said...

We are leading responsive website designing and development company in Noida.
We are offering mobile friendly responsive website designing, website development, e-commerce website, seo service and sem services in Noida.

Responsive Website Designing Company in Noida
Website Designing Company in Noida
SEO Services in Noida
SMO Services in Noida

Vikas Chaudhary said...

Battery Mantra is Authorized exide car battery dealer in Noida and Greater Noida. We are providing our service in Indirapuram, Delhi, Ashok Nagar.

Exide Battery Dealer in Noida
Battery Dealer in Noida
Authorized Battery Dealer in Noida
Car Battery Dealer in Noida
Car Battery Dealer
Exide Battery Dealer

EG MEDI said... is online medical store pharmacy in laxmi nagar Delhi. You can Order prescription/OTC medicines online.
Cash on Delivery available. Free Home Delivery

Online Pharmacy in Delhi
Buy Online medicine in Delhi
Online Pharmacy in laxmi nagar
Buy Online medicine in laxmi nagar
Onine Medical Store in Delhi
Online Medical store in laxmi nagar
Online medicine store in delhi
online medicine store in laxmi nagar
Purchase Medicine Online
Online Pharmacy India
Online Medical Store