javagwtservletsgwt-rpcgwt2

How to send hashmap with a file in GWT


I want to know or some code sample which will help me to send the file and the hashmap to the server.

to upload a file to GWt server i use formpanel and a HttpServlet. this is working fine.

i have a hashmap

    private static Map<String, List<Customproperties>> docClass =
 new HashMap<String, List<Customproperties>>();

which holds the property of document according to its classname.

I know how to do with RPC. but i want to do with servlet.As i have to upload a file which i have done with servlet. And every Hashmap is related to file .and this file with its property(in HashMap) will send to external repository.

Please help.


Solution

  • There are 2 ways to convert a hashmap to a string (and convert it back to a hashmap)

    1: Convert it using JSON library http://json-lib.sourceforge.net/ This will allow you to convert any java object to a JSON string so you can transfer it anywhere. And using the same library or another JSON library, can convert it back to a Java object.

    2: Convert it to an XML string using a library called XStream http://x-stream.github.io/ This will convert any Java object into a String represented as XML.

    I would recommend to convert your Objects to JSON strings because you are using GWT and it has a lot of support for JSON. And JSON is a good format for Webapps. Another advantage is that other languages can convert your JSON string into an object too.