Groups | Blog | Home
all groups > macromedia flash flash remoting > november 2003 >

macromedia flash flash remoting : Accessing Java Servlets


suddu dude
11/19/2003 10:03:04 AM

I have developed a application using java Servlets and jsp. Now i want the same to be done using Flash without changing the java Servlets. How can i call the Servlets from the Flash movie. Can some body help me

Please give me an example.....


Sudarshan Devadiga



renee
12/4/2003 2:06:22 AM
it can be done!!!

but I understand the frustration with finding tutorials on it.

this is the way that I do it - using Flash Remoting mostly, but I think you'll get the idea.

this uses Flash remoting (which has to be separately installed) and JRun, another macromedia product. I'm not sure how different it would be for different platforms, but maybe this will help you get some ideas.

in your Actionscript code: (AS 1.0) - little different with AS 2.0 (let me know if you need that)
IN FLASH:
------------------------------------------------------------------------------------------------------------------------
NetServices.setDefaultGatewayURL("http://localhost/flashservices/gateway");
gatewayConnection = NetServices.createGatewayConnection();
_global.servletService = gatewayConnection.getService("CMSFlash", this);
------------------------------------------------------------------------------------------------------------------------

NOTE: 'CMSFlash' is the name of my jrun application that I have defined under the default server - you
would put your own application name there, applications by which your servlets are defined
IN FLASH:
------------------------------------------------------------------------------------------------------------------------
_global.servletService = gatewayConnection.getService("YourAppName", this);
------------------------------------------------------------------------------------------------------------------------
then you can make a call when you want to access a given servlet:
IN FLASH:
------------------------------------------------------------------------------------------------------------------------
_global.servletService.LoadFlashData(param1, param2, param3,....paramN);
------------------------------------------------------------------------------------------------------------------------
where 'LoadDataServlet' is the name of the servlet you are accessing
Of course, you can call the servlet without parameters too... just empty parenthesis
Flash then puts all of the parameters into an array and sets it into an attribute in the request object
They call it: "FLASH.PARAMS"

IN JAVA:
------------------------------------------------------------------------------------------------------------------------
ArrayList paramsFromFlash = (ArrayList)request.getAttribute("FLASH.PARAMS");
// then you do with those params whatever you want....
------------------------------------------------------------------------------------------------------------------------

when you are ready to go back to Flash
put your data in the request object with any of the datatypes known to Flash (let me know if you wanna talk how to do complex data) - set them in the "FLASH.RESULT" attribute in the request object

IN JAVA:
------------------------------------------------------------------------------------------------------------------------
ArrayList results = new ArrayList();
results.add("success");
results.add(dataResultSet);
request.setAttribute("FLASH.RESULT", results);
------------------------------------------------------------------------------------------------------------------------
I like to send back a success/failure type attribute for my own needs... but I think you get the idea.


Ok... back on the Flash end
I'm kinda going through this based on the timeline of how events occur - I think its easier to understand, maybe its just the way I think. whatever.

To capture the results from your Servlet, set up the flash remoting result method.

IN FLASH:
------------------------------------------------------------------------------------------------------------------------
function LoadFlashData_Result (data) {
// do with the data what you want...
// to show how the data is just like the ArrayList I sent, I'll write this
if (data[0] == "success") {
myData = data[1];
}
}

// YOU ALSO need to make a _Status function... another thing that captures errors
// like, not being able to find the servlet...or something - easy enough
function LoadFlashData_Status(statusData) {
trace (statusData);
}
------------------------------------------------------------------------------------------------------------------------

Note: whatever name you use to call the servlet is the same name that you define in web.xml in your Jrun\default\applicationName...\WEB-INF\web.xml

So, a node in the web.xml might look like this:
------------------------------------------------------------------------------------------------------------------------
<servlet>
<servlet-name>LoadFlashData</servlet-name>
<servlet-class>LoadFlashDataServlet</servlet-class>
<display-name>LoadFlashData</display-name>
<description />
</servlet>
<servlet-mapping>
<url-pattern>/LoadFlashData</url-pattern>
<servlet-name>LoadFlashData</servlet-name>
</servlet-mapping>
------------------------------------------------------------------------------------------------------------------------
so, NOTE that I use "LoadFlashData()" and not "LoadFlashDataServlet" which is the ACTUAL class name of the servlet. Does that make sense?

then in Actionscript... to recieve the calls, you use the EXACT SAME NAME (LoadFlashData) with a _Result and a _Status as part of the name of the function, and it will call those functions when it has come back from the server.

I know all this assumes you are using Flash Remoting and JRun.
But, I wish so much someone had explained it to me like this when I was starting out. Took me forever to get here..

I sincerely hope this helps.
let me know if you have any questions

Renee


AddThis Social Bookmark Button