Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Applet/Servlet communication failing...

 
Jeff Weeks
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have a rather annoying issue communicating between an Applet and a servlet. The basic flow of information is as follows:
The applet constructs a HashMap of criteria information for a query (name/value pairs, like "productCode" => "1001233"). This HashMap is transfered to the servlet through a POST request. This part works fine.
The servlet receives this criteria, and performs a query, and attempts to return back a custom object (named FormulaMapBean (which also contains references to ComponentBean and FormulaBean)). This part fails. I've ran with tracing of 5, and found the following in the Java console:
Reading bean
Connecting http://localhost:8082/standards/classes/com/kellogg/london/common/beans/standards/FormulaMapBean.class with no proxy
Connecting http://localhost:8082/standards/classes/com/kellogg/london/common/beans/standards/FormulaMapBean.class with cookie "JSESSIONID=D85890B193E5A4A8F0F0D0662226A577"
Connecting http://localhost:8082/standards/classes/com/kellogg/london/common/beans/standards/FormulaBean.class with no proxy
Connecting http://localhost:8082/standards/classes/com/kellogg/london/common/beans/standards/FormulaBean.class with cookie "JSESSIONID=D85890B193E5A4A8F0F0D0662226A577"
Connecting http://localhost:8082/standards/classes/com/kellogg/london/common/beans/standards/ComponentBean.class with no proxy
Connecting http://localhost:8082/standards/classes/com/kellogg/london/common/beans/standards/ComponentBean.class with cookie "JSESSIONID=D85890B193E5A4A8F0F0D0662226A577"
Finding information ...
Releasing classloader: sun.plugin.ClassLoaderInfo@82751, refcount=0
Done ...
java.lang.ClassNotFoundException: com.kellogg.london.common.beans.standards.FormulaMapBean
at sun.applet.AppletClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.applet.AppletClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at java.io.ObjectInputStream.resolveClass(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at com.kellogg.london.applets.standards.AppletServletCommunicator.readBean(AppletServletCommunicator.java:179)
at com.kellogg.london.applets.standards.FormulaQueryApplet.init(FormulaQueryApplet.java:47)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: open HTTP connection failed.
at sun.applet.AppletClassLoader.getBytes(Unknown Source)
at sun.applet.AppletClassLoader.access$100(Unknown Source)
at sun.applet.AppletClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
... 17 more

What I find really strange, however, is that this works perfectly fine when using a Java class (I've tried changing the HashMap, and returning it, and that works perfectly fine, however, returning these custom objects doesn't).
Any ideas?
These objects are serializable. They're initially loaded (by the servlet) from a web service, and this works fine as well! It's the transition from Servlet to Applet that keeps screwing up, and I dont understand why.
General flow (in Applet):
comm.startPost("application/octet-stream");
comm.sendBean(params);
comm.endPost();
System.out.println("Reading bean");
Object o = comm.readBean(); // fails if o is a FormulaMapBean
General flow (in servlet):
comm.setInputStream(request.getInputStream());
Object o = comm.readBean(); // criteria hashmap (works!)
FormulaMapBean fmb = getFormula(map);
comm.setOutputStream(response.getOutputStream());
comm.sendBean(fmb);
where comm is a class I wrote with the following relevant methods:
public void startPost(String type) {
try {
URL testServlet = new URL( servletAddress );
servletConnection = testServlet.openConnection();
// make sure we can both read and write to servlet
servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);
// Don't use a cached version of URL connection.
servletConnection.setUseCaches (false);
servletConnection.setDefaultUseCaches (false);
// Specify the content type that we will send binary data
servletConnection.setRequestProperty("Content-Type", type);
servletOutputStream = servletConnection.getOutputStream();
// send your data to the servlet
} catch(Exception e) {
e.printStackTrace();
}
}
public void endPost() {
try {
servletInputStream = servletConnection.getInputStream();
} catch(Exception e) {
e.printStackTrace();
}
}
public void sendBean(Object o) {
try {
ObjectOutputStream outputToServlet = new ObjectOutputStream(servletOutputStream);
outputToServlet.writeObject(o);
outputToServlet.flush();
outputToServlet.close();
} catch(Exception e) {
e.printStackTrace();
}
}
public Object readBean() {
Object o = null;
try {
ObjectInputStream inputFromServlet = new ObjectInputStream(servletInputStream);
o = inputFromServlet.readObject();
inputFromServlet.close();
} catch(Exception e) {
e.printStackTrace();
}
return o;
}

Any help would be greatly appreciated (and I appologize for the long post... this stuff's fairly new to me, so I'm not sure all what's relevant). It looks like it's a case of having to somehow 'Register' with the ObjectInputSteam where the FormulaMapBean.class is located... except that, from the Java Console output (first few lines) it seems to me that is does, indeed, know where it is!
Cheers,
Jeff
 
Dana Hanna
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
put the "com.kellogg.london.common.beans.standards.FormulaMapBean" class in the classpath for the applet (like in the JAR file it's loaded from), or somewhere in the codebase. There are other ways to make it dynamically load - but it's easiest just to put it with the applet code in a jar.
 
Jeff Weeks
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers! All working now
I thought I'd already done that
I was loaded my applet from the /webcontext/classes directory which I had simply assumed was the standards.war:/web-inf/classes directory, and that all the classes were in there. Obviously that wasn't the case
Thanks,
Jeff
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic