Search code examples
javajspapache-flexservletsflash-builder4.5

Retrieve hashmap values from java servlet to flex


I am working on "migration from JSP to FLEX, and java as back-end. I am novice in java and JSP.

I am stuck at getting values from a java servlet where it takes httprequest and there is a function called forward(request,response) which responds as JSP page with requried values in it.
Now I need to change that and get only data from that servlet and use that in flex.

Problem:
case1: When using httpservice it takes result as string, but unable to get as object.
case2: If I use RemoteObject , it needs method in java servlet to get return value, which is not present in existing servlet.

Can I get any suggestions on this problem.

Thank You


Solution

  • case1: you don't use forward anymore, you set the type of data you want to return, for example:

    response.setContentType("application/json");
    

    ... and returning data in servlet is done by writing data to a stream like:

    PrintWriter out = response.getWriter();
    out.print(object);
    

    but it's a while ago since I did this, so there may be some small problem you will face...

    case2: if you are using servlet, it's correct to use HTTPService, RemoteObject will not work, it is used differently....


    Now, if I can, I would suggest diferent thing to use than servlets - to obtain data from a java server to Flex - I love to use GraniteDS.

    I will just state some benefits I see, in case you are interested:

    It is easy to setup: - in java, you will just add a graniteDS library, two config files (granite+services-config xmls) add a granite servlet config to web.xml - in flex there is also granite library and services-config.xml

    When set up, using it is also flowleslly easy - you have a class with a method (or simple bean or ejb) in Java which just return an object of any type! And that's it in Java

    In flex, in this case you use RemoteObject which you just call that remote java method and in result handler you get your dataGranite will take care of serializing+transfer+deserializing and just give you the Object (either just dynamic {} or even exact class type)