Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Passing objects between a servlet and an applet

 
Greenhorn
Posts: 7
  • 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:
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
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Trailboss
Posts: 23866
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Ahonen
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic