This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

To pass object from Applet to Servlet and vice versa

 
fai fai
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William
Thank your very much, You are right
many thks
regards
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic