aspose file tools*
The moose likes Servlets and the fly likes To pass object from Applet to Servlet and vice versa Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "To pass object from Applet to Servlet and vice versa" Watch "To pass object from Applet to Servlet and vice versa" New topic
Author

To pass object from Applet to Servlet and vice versa

fai fai
Greenhorn

Joined: Dec 18, 2002
Posts: 4
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 );}

}
}
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5

You should not be trying to open the url twice, you should use uc.getInputStream() to get the input stream that corresponds to the response to the request you send to the servlet.
However, you also have to watch out for the fact that the stream gets closed when you execut the rs.close() call. Just flush the object output stream, don't close it.
Bill
fai fai
Greenhorn

Joined: Dec 18, 2002
Posts: 4
William
Thank your very much, You are right
many thks
regards
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: To pass object from Applet to Servlet and vice versa