Thursday, May 7, 2009

Accessing HTTP Request/Header parameter in Flex

I hope this blog will help flex newbie to understand the ways to pass/access http request/header parameter in flex application.

To access http request/header, following approaches can be used -

  1. Load you flex application in a jsp page and pass the http request/header details through flashvars to flex application. I will discuss this further with code and limitation.
  2. Write a httpservlet and call the servlet from flex application using httpservice and get the response as e4x xml for required parameters.
  3. Instantiate servlet using flex httpservice and store the request/header parameter in it and access them using remoting service via java objects.

These approaches can be used if siteminder token other http request/header information need to be passed to flex application.

Approaches

A. Passing variable from JSP to Flex, variable can be user defined, system or http request/header variable.

Step 1. Change the html in which flex is embedded to jsp. Nothing required just save it from index.html to index.jsp its done.

Step 2. Define variable in index.jsp in this case we are trying to pass user-agent value to flex app.
<% String parameterA = (String)request.getHeader("user-agent");%>

Step3. Search "if (hasRequestedVersion)" in index.jsp , it will look something like this
else if (hasRequestedVersion) {
// if we've detected an acceptable version
// embed the Flash Content SWF when all tests are passed
AC_FL_RunContent(
"src", "AccessParameter",
"width", "100%",
"height", "100%",
"align", "middle",
"id", "AccessParameter",
"quality", "high",
"bgcolor", "#869ca7",
"name", "AccessParameter",
"allowScriptAccess","sameDomain",
"type", "application/x-shockwave-flash",
"pluginspage", "http://www.adobe.com/go/getflashplayer"
);


Add flashVars line for parameterA like this
"FlashVars",'parameterA=<%parameterA%>',

incase you want to pass html and pass value in it.
"FlashVars",'parameterA=test123',

Step4. Now we need to access the parameterA value in flex application. It can be either accessed directly at application.mxml file or in actionscript sub classes

To access parameterA values in application mxml, write a method accessJspVariable call it in completionComplete tag of application mxml or in case you have init(), call this method from init() method.

function accessJspVariable():void{
var parameterA=Application.application.parameters.parameterA
Alert.show("Variable passed by JSP :"+parameterA);
}

Incase you want to access this variable direclty in some actionscript class. First
import mx.core.application;

var parameterA=Application.application.parameters.parameterA;

Source code is upload. HTML file loaded instead of JSP as JSP would require server to run it.

2 comments:

  1. Hi there,
    I'm a bit confused by your example. The AC_FL_RunContent function that you listed above is the one that gets run to install the latest version of flash player (see how the "src" is "playerProductInstall"?) I thought that it might redirect you back to the correct html/jsp page if the flash version if correct, but when I tried it out it didn't work for me (I got a blank page).

    Also, you aren't accessing the parameterA correctly in Flex, when I try this "trace(Application.application.parameterA);" I get an error saying that there is no property "parameterA" defined on the application. The correct way to reference the parameter is "Application.application.parameters.parameterA". Your example might work because you defined your variable to also be called "parameterA", and so in fact the line "var parameterA:String = Application.application.parameterA;" does nothing because you are assigning parameterA to itself (assuming you write that code in your mxml application)!

    Cheers,
    Chris

    ReplyDelete
  2. Thanks Chris for letting me my typo error of parameter in Var parameterA=Application.application.parameters.parameterA

    Code is uploaded now, You can try that.

    ReplyDelete