Flex and Servlets

TheSpook

Limp Gawd
Joined
Feb 12, 2004
Messages
379
I'd like to have Flex talk with a Java servlet.

Currently, my servlet can receive data from my Flex application via an HTTPService.

The servlet can send data back to Flex as a byte stream:
Code:
OutputStream out = response.getOutputStream();
byte[] returnVal = "some response message".getBytes();		
out.write(returnVal, 0, returnVal.length);

I'm told Flex/Java communication can handle Arrays, though (link).

How would I write a Java array back to my Flex application?

Thanks in advance :).
 
http://www.spicefactory.org/pimento/

or any other AMF framework for java. I'm not sure if you can pass Arrays natively.

Thanks! I'm looking at stuff like this now (also BlazeDS and some others).

I think for now I will settle with a JSON (http://www.json.org/) implementation. My array will consist of (many, many) strings, integers, and floats, so JSON will work fine.


This being one of my first forays into web stuff, let me tell you: there are too many technologies! Holy crap. I spend all of my time getting Technology A to play with Technology B. It is the worst.
 
I'd like to have Flex talk with a Java servlet.

Currently, my servlet can receive data from my Flex application via an HTTPService.

The servlet can send data back to Flex as a byte stream:
Code:
OutputStream out = response.getOutputStream();
byte[] returnVal = "some response message".getBytes();		
out.write(returnVal, 0, returnVal.length);

An easier way to send back a text response is to use a PrintWriter, via the javax.servlet.ServletResponse.getWriter() method. Using this method also prevents accidentally encoding your response one way while specifying another in the Content-Type header.

As for your question, I don't really know Flex so I'm not sure if it has a way to pass arrays back naively, but as an alternative you can use the PrintWriter returned from getWriter() method to write an XML or JSON response constructed by you in the Servlet which could be parsed on the other side.

As you linked above json.org has java bindings for JSON, and if you decide to go the xml route I recommend using JDOM
 
I would highly suggest using BlazeDS, you can send most common things back and forth without a problem, they havea mapping sheet somewhere, google amf3 java deserialization should get you to it, basically it translates things with no fuss, basically ArrayCollection (AS3) -> List
Object -> Map
so if you have a object with a lot of stuff in it you can pass the object and whatever is in that doc gets converted just fine, you save the ac3 object directly to a database too and back and send to flex with everything preserved.

amf is also secure i believe over https.
 
i've done some flex-apps (well, i didnt do the flex apps, but the java back-end) with java servlets to provide data, we always used XML as the format for communicating (from java to flex), it has more overhead then json, but is better human-readable (to me anyway), and supposibly easier to parse in flex..

I also did some json servlets for some advanced javascript trickery, and boy do i hate json..
 
Back
Top