How to communicate between applet-servlet communication ?

Showing Answers 1 - 1 of 1 Answers

ram

  • May 19th, 2006
 

the following code shows you how to communicate to servlet from within an applet

// Connection to servlet

URL urlServlet = new URL(getCodeBase(), "ServletName");

URLConnection con = urlServlet.openConnection();

con.setDoInput(true);

con.setDoOutput(true);

con.setUseCaches(false);

con.setRequestProperty("Content-Type", "application/x-java-serialized-object");

// get input data for sending it to servlet

String input = "input string";

// send data to the servlet

OutputStream outstream = con.getOutputStream();

ObjectOutputStream oos = new ObjectOutputStream(outstream);

oos.writeObject(input);

oos.flush();

oos.close();

// receive result from servlet

InputStream instr = con.getInputStream();

ObjectInputStream inputFromServlet = new ObjectInputStream(instr);

String result = (String) inputFromServlet.readObject();

inputFromServlet.close();

instr.close();

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions