4

I want to send send HashMap object to the applet that requested it. A servlet has that HashMap object. Is there a way I can do this ?

Applet ------requests HashMap object---->Servlet listens to this request
                                                 |
                                                 |
                                     Servlet searches that HashMap Object
                                                 |
                                                 |
                                                \ /
<--Finally Send this to applet------------ Servlet gets the HashMap object                                                                

I have made a connection to the servlet and my servlet also has the HashMap object,but I don't know how to send it to the applet and I wonder if it can be sent !

Suhail Gupta
  • 19,563
  • 57
  • 170
  • 298

4 Answers4

3

How about serializing it and sending it in response ? Consider converting it to JSON or XML.

mrd081
  • 279
  • 1
  • 11
  • 1
    Using Java serialization is absolutely not recommended. It tight couples your service to a single platform and it breaks portability. Rather use a common transfer format so that it can easily be reused for other purposes. – BalusC Jul 25 '12 at 11:28
  • i think json or xml are some sort of serializing data with metadata. – mrd081 Jul 25 '12 at 17:26
  • Yes, but you explicitly mentioned it as an alternative ("Or") to converting it to JSON/XML. – BalusC Jul 25 '12 at 17:29
  • can you site some tutorial for this ? I haven't been able to do this till now. Or just detail your answer a bit – Suhail Gupta Aug 14 '12 at 17:49
  • sure @SuhailGupta . Try using google gson - http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/index.html and http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/ – mrd081 Aug 15 '12 at 05:41
2

You can open an URL connection to the servlet if the servlet is in the same server from where the applet was downloaded. The you can read the

URL site = new URL("your site")
URLConnection urlCon = site.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
                                urlCon.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) 
        System.out.println(inputLine);
    in.close();

Meanwhile in the servlet you write your data back to the client using the HttpServletResponse.

If you need something more "sofisticated", you can use axis as webservice stack in your applet, or include a lightweight REST lib like Jersey. But these solutions force you to use other server component instead a Servlet.

This post will help you:

They both use json-lib to parse/serialize objects from JSON format.

Hope this help.

Community
  • 1
  • 1
kothvandir
  • 1,771
  • 2
  • 15
  • 30
1

I'm going to make use of some external libraries in order to answer your question: Google Gson and Apache IO Utils.

So you already have the HashMap in your Servlet and want to send it to the Applet:

Map<String, String> myMap = new HashMap<String, String>();// or whatever
Gson gson = new GsonBuilder().create();
String jsonString = gson.toJson(myMap);
IOUtils.write(jsonString, resp.getOutputStream());// where 'resp' is your HttpServletResponse
IOUtils.closeQuietly(resp.getOutputStream());

And to receive it in your Applet:

String jsonString = IOUtils.toString(conn.getInputStream()); // where 'conn' is an HttpURLConnection
IOUtils.closeQuietly(connection.getInputStream());
Gson gson = new GsonBuilder().create();
// The TypeToken is needed when Generics are involved
Type typeOfHashMap = new TypeToken<Map<String, String>>() {}.getType();
Map<String, String> myMap = gson.fromJson(jsonString, typeOfHashMap);

And that's it. It's just a simple example but I hope you get something out of it.

Of course you could be doing it by hand instead of using external libraries, but this way is much easier.

  • I haven't understood how to implement this code.Also explain what actually is `resp.getOutputStream()`. What does it return ? The same for `connection.getInputStream()`. What I did now is [this](http://i48.tinypic.com/2chx6v7.jpg). The red mark is the call to the function of applet where I put the second snippet of yours. – Suhail Gupta Aug 15 '12 at 04:38
  • Since your Applet already calls the Servlet, you have an input and an output stream available to communicate between the two. What you have to do is use the aforementioned streams to send and receive the data. Remember: what you write to the **output** stream in the Servlet is received through the **input** stream in the Applet. I don't think you need to (or can) call the Applet from the Servlet. You have to include the second part of my answer in your Applet's code right after the connection to the servlet is made; and remove the `new PollForm().func()` in your servlet. –  Aug 15 '12 at 17:06
  • can you please explain this statement in the applet code : `Type typeOfHashMap = new TypeToken>() {}.getType();` What does it do ? – Suhail Gupta Aug 20 '12 at 10:59
  • It holds the Type of the HashMap so that you can pass it to Gson, which in turn uses it to convert the JSON string into the given Type. POJOs (without Generics) doesn't need that; you can just use `gson.fromJson(jsonString, MyPojo.class)`. –  Aug 20 '12 at 14:26
  • what you mean by type of hash map ? And how does it know that I have used HashMap ? Please explain this. I have been trying to understand this – Suhail Gupta Aug 20 '12 at 14:30
  • I don't think I can explain it better that the docs: https://sites.google.com/site/gson/gson-user-guide#TOC-Collections-Examples –  Aug 20 '12 at 14:33
0

You can simply serialize the object and write it directly to the HttpServletResponse, writing the byte stream.

On the Applet Side you have to read the byte stream and the deserialize using the stream. Be aware that the Applet sends a POST request otherwise you could have the 1024byte limit if you use GET request.

Hope this help you.

FrancescoAzzola
  • 2,536
  • 2
  • 12
  • 22