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); }
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).]
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.
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
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Passing objects between a servlet and an applet