• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

To pass object from Applet to Servlet and vice versa

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 );}

}
}
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
William
Thank your very much, You are right
many thks
regards
 
Could you hold this puppy for a sec? I need to adjust this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic