aspose file tools*
The moose likes Servlets and the fly likes Passing objects between a servlet and an applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Passing objects between a servlet and an applet" Watch "Passing objects between a servlet and an applet" New topic
Author

Passing objects between a servlet and an applet

Chris Patton
Greenhorn

Joined: Jun 12, 2000
Posts: 7
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
Todd M Bush
Greenhorn

Joined: Apr 26, 2000
Posts: 22
The only thing I see is that theObject must be serialized to be passed. Sun has a good paper on this called Java Servlets and Serialization with RMI, just search on it. Here is snippets of code that I know work....
server side....
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException {

ObjectInputStream in = new ObjectInputStream(req.getInputStream());
ObjectOutputStream out = new ObjectOutputStream(resp.getOutputStream());
String sourceName = (String)in.readObject();
String sourceType = (String)in.readObject();
out.writeObject(add_Source(sourceName, sourceType));
// Where add_Source does something and returns a Boolean
}
the client
public Boolean addSource(String SourceName, String SourceType) throws Exception {
URL servlet = new URL(webBase,"servlet/AZUniversalServlet");
Integer code = new Integer(5);
Serializable objs[] = { code, SourceName, SourceType };
in = postObjects(servlet, objs);
Boolean result = (Boolean)in.readObject();
return result;
}
private ObjectInputStream postObjects(URL myServlet, Serializable myObjs[])
throws Exception {
URLConnection con = servlet.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

ObjectOutputStream out = new ObjectOutputStream(con.getOutputStream());
int numObjects = myObjs.length;
for(int x= 0; x<numObjects; x++) {
out.writeObject(myObjs[x]);
}

out.flush();
out.close();

return new ObjectInputStream(con.getInputStream());
}
good luck
[This message has been edited by Todd M Bush (edited June 13, 2000).]
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20535
    ∞

See www.javaranch.com/common.html.
Use the HTTP class to send objects to your servlets. Inherit the ObjectServlet class for the servlet to catch the objects and send an object back.


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Tero Ahonen
Greenhorn

Joined: May 24, 2001
Posts: 26
Hi Chris!
I have a same kind of problem, but I can pass Objects from applet to servlet but not from servlet to applet. How does your object transferring from servlet to applet works?
My applet to servlet code:
Applet
String location = "http://localhost:8080/servlet/TestServlet";
Hashtable h = new Hashtable();
URL url = new URL(location);
URLConnection con = url.openConnection();
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty(
"Content-Type",
"application/x-www-form-urlencoded");
ObjectOutputStream out = new ObjectOutputStream
(con.getOutputStream());
out.writeObject(h);
out.flush();
Servlet
ObjectInputStream in = new ObjectInputStream
(request.getInputStream());
Hashtable h = (Hashtable)in.readObject();
in.close();

------------------

-Tero
Tero Ahonen
Greenhorn

Joined: May 24, 2001
Posts: 26
Hi!
I have the ObjectServlet and all other javaranch.common classes. Is there any example how to use ObjectServlet for passing objects from applet to servlet and back.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing objects between a servlet and an applet