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:
public void writeDB()
{
ObjectOutputStream outputToServlet = null;
try
{
URL skillsDBservlet = new URL("http://cpatton:8100/servlet/ServTest");
URLConnection servletConnection = skillsDBservlet.openConnection();
servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);
servletConnection.setUseCaches (false);
servletConnection.setDefaultUseCaches (false);
servletConnection.setRequestProperty("Content-Type", "application/octet-stream");
outputToServlet = new ObjectOutputStream(servletConnection.getOutputStream());
outputToServlet.writeObject(theObject);
outputToServlet.flush();
outputToServlet.close();
}
catch(Exception e)
{
//if there's an exception, send it to the main text window.
subPanel1.removeAll();
subPanel1.repaint();
textBox.setText(e.toString());
subPanel1.add(textBox);
subPanel1.setVisible(false);
subPanel1.setVisible(true);
System.out.println(e);
}
}
here is my servlet-side code:
public void doPost(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException
{
if(DEBUG)System.out.println("in doPost(..)");
ObjectInputStream inputFromApplet = null;
Object tempObj = null;
try {
// get an input stream from the applet
inputFromApplet = new ObjectInputStream(req.getInputStream());
// read the serialized object from applet
tempObj = inputFromApplet.readObject();
inputFromApplet.close();
}catch (Exception e){
System.out.println(e);
}
setObject(tempObj);
}
Thanks
- Chris
PS love the site