wood burning stoves 2.0*
The moose likes Servlets and the fly likes Passing Object from Applet to Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Passing Object from Applet to Servlet" Watch "Passing Object from Applet to Servlet" New topic
Author

Passing Object from Applet to Servlet

Ashish Thakur
Greenhorn

Joined: Mar 18, 2000
Posts: 22
Hi!
I am trying to send myObject(extending Object implements Serialize) from applet to Servlet using the UrlConnection class. But am unable to do so.
I am using the JavaWebServer2.0 evaluation version. Is it that the server does not support this facility in this eval. copy , Or do i have to do Something else during sending and reading the myObject using the ObjectInput/OutputStream
Any help will be much appreciated.
Thanks for reading this.
Java Rules.
Ashish T.
Todd Bush
Greenhorn

Joined: Mar 06, 2000
Posts: 14
Hopefully this will help you. This is how I am doing it....
This is called to add a sourceName and sourceType to the db. A boolean is returned weither the action was successful or not. The code Integer indicates to the Servlet which function to use. My servlet houses many functions. I place a call to postObject to do the actual transfer (code at the bottom)
public Boolean addSource(String SourceName, String SourceType) throws Exception {
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.
Todd
 
GeeCON Prague 2014
 
subject: Passing Object from Applet to Servlet