I know it is possible to pass objects between a
servlet and an
applet, but thus far I have been only partially successful. I can get the object in question to the applet from the servlet (by way of the doGet method), but I can't seem to be able to pass the object back to the servlet from the applet.
I have alread read numerous tutorials/articles on the subject, and I feel that I'm doing everything correctly. Still, it doesn't work.
For the sake of clarity, I'll post both my applet-side and servlet-side code (just the networking part, not all of it).
here is my applet-side code:
Servlet side:
public class Test_Servlet1 extends HttpServlet
{
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
System.out.println( "doPost " );
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
InputStream iii = req.getInputStream();
ObjectInputStream oo = new ObjectInputStream(iii);
System.out.println("From JApplet1 " + oo );
oo.close();
}
}
Client side:
public class JApplet1 extends JApplet
{
public void init()
{
getContentPane().setLayout(null);
setSize(426,266);
Frame aa = new Frame();
try
{
URL url = new URL("http://localhost:8080/Test/Test_Servlet1");
URLConnection uc =url.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setUseCaches(false);
uc.setDefaultUseCaches (false);
uc.setRequestProperty("Content-Type", "application/octet-stream");
ObjectOutputStream rs = new ObjectOutputStream (uc.getOutputStream());
Serializable str_obj = "zzzzzzz";
rs.writeObject(str_obj); // wished to sent this object to servlet
rs.flush();
rs.close();
InputStream in = url.openStream();
ObjectInputStream rs2 = new ObjectInputStream(in); //failed in this statement
String xx = (String) rs2.readObject();
System.out.println(xx );
rs.close();
} catch (Exception e) { System.out.println(e );}
}
}