Email Subscriptions

Enter your email address:

Delivered by FeedBurner

Thursday, March 18, 2010

Applet Servlet Communication

following code can be used at applet side :

/*
*URL to servlet
*/

URL serverURL = new URL(servletPath);

URLConnection connection = serverURL.openConnection();
/*
* Connection will be used for both input and output
*/
connection.setDoInput(true);
connection.setDoOutput(true);

/*
* Disable caching
*/
connection.setUseCaches(false);

/*
*connection will be used for transfaring serialized java objects.
*/
connection.setRequestProperty("Content-Type", "application/octet-stream");

ObjectOutputStream outputStream = new ObjectOutputStream(connection.getOutputStream());

outputStream.writeObject(request);
outputStream.flush();
outputStream.close();

ObjectInputStream inputStream = new ObjectInputStream(connection.getInputStream());

response = (Map)inputStream.readObject();
inputStream.close();


Here response is the response Map returned from servlet.

following code can be used in your servlet to send serializable objects to applet.

write this code in doXXX method of servlet.

ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());
Map responseMap = new HashMap();
/*
put watever objects you wants to return to applet.
This objects must be serializable.

responseMap.put("a", "a");

*/
out.writeObject(responseMap);
out.flush();
out.close();
response.setStatus(HttpServletResponse.SC_NO_CONTENT);