Sending data from a servlet to an applet: how to implement this?

I want to send the send HashMap object to the applet that requested it. The servlet has this 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 made a connection to the servlet, and my servlet also has a HashMap object, but I donโ€™t know how to send it to the applet, and I wonder if it can be sent!

+4
source share
4 answers

I am going to use some external libraries to answer your question: Google Gson and Apache IO Utils .

So, you already have a HashMap in your servlet and you 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 get 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); 

What is it. This is just a simple example, but I hope you get some of this.

Of course, you can do it manually instead of using external libraries, but this method is much simpler.

+1
source

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

+3
source

You can open the URL connection to the servlet if the servlet is located on the same server from which the applet was loaded. You can read

 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 are returning data back to the client using the HttpServletResponse.

If you need something more โ€œsophisticated,โ€ you can use the axis as the webservice stack in your applet, or include a lightweight REST library such as Jersey. But these solutions force you to use a different server component instead of Servlet.

This post will help you:

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

I hope for this help.

+2
source

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

On the applet side, you have to read the byte stream and deserialize it with the stream. Keep in mind that the Applet sends a POST request, otherwise you may have a limit of 1024 bytes if you use a GET request.

Hope this helps you.

0
source

All Articles