MarteJ
asked on
Problems with Applet/Servlet communication with serialization
Hi,
I have a test JApplet with a tread running. Every X seconds, the tread sends a serialized Vector to the servlet as a http request and receives a serialized Vector as a http response. I have two questions:
1) In my applet, I have a Thread with the inner class UpdateHandler as its runnable target. In UpdateHandler, the above mentioned applet-servlet communication takes place.
When running my applet, I got an exception: java.io.NotSerializedExcep tion for UpdateHandler, and when I let UpdateHanler implement Serializable, that error seemed to be fixed. Then I got the same error for the Tread _updateThread. I created an inner class UpdateTread which extends Thread and implements Serializable. Now both errors were gone. Seems strange to me that the objects containing the code for sending an object to/from the servlet, also need to be Serializable, not only the passed Vector. Why is it so?
2) When the NotSerializedExceptions are gone, I get another error when trying to read the response from servlet, when instantiating new ObjectInputStream(servletC onnection. getInputSt ream()) - see posted code. The exception is:
java.io.FileNotFoundExcept ion: http://hostIP:portNo./emilion/servlet/result/TestServlet
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:707)
at sun.plugin.net.protocol.ht tp.HttpURL Connection .getInputS tream(Http URLConnect ion.java:3 84)
at result.TestApplet$UpdateHa ndler._req uestResult sFromServl et(TestApp let.java:1 58)
at result.TestApplet$UpdateHa ndler._req uestResult s(TestAppl et.java:11 7)
at result.TestApplet$UpdateHa ndler.run( TestApplet .java:99)
at java.lang.Thread.run(Threa d.java:536 )
Any ideas what causes the error? Help greatly appreciated!
package result;
import java.awt.*;
import java.util.*;
import java.net.URL;
import java.net.URLConnection;
import java.io.ObjectOutputStream ;
import java.io.ObjectInputStream;
import java.io.Serializable;
import javax.swing.*;
public class TestApplet extends JApplet implements {
private UpdateHandler _updateHandler;
private Thread _updateThread;
private URL _resultServlet;
public void init() {
super.init();
try {
String servletPath = "/emilion/servlet/result/T estServlet ";
_resultServlet = new URL("http", getCodeBase().getHost(), getCodeBase().getPort(), servletPath);
} catch(Exception x) {
x.printStackTrace();
}
// Set up the GUI
QCPanel bigViewPanel = new QCPanel();
QCLabel label = new QCLabel("TEST TEST");
bigViewPanel.add(label);
getContentPane().add(bigVi ewPanel, BorderLayout.CENTER);
// Updating thread
_updateHandler = new UpdateHandler();
_updateThread = new UpdateTread(_updateHandler );
_updateThread.start();
}
private class UpdateTread extends Thread implements Serializable {
public UpdateTread(UpdateHandler updateHandler) {
super(updateHandler);
}
}
private class UpdateHandler implements Runnable, Serializable {
public final static int UPDATE_INTERVAL = 10;
public UpdateHandler() {
}
public void run() {
try {
_requestFromServlet();
// Update GUI with responded data from servlet
//…..
} catch (InterruptedException ie) {
}
}
private synchronized void _ requestFromServlet () throws InterruptedException {
while(true) {
_request();
wait(UPDATE_INTERVAL*1000) ;
}
}
public Vector _request() {
ObjectOutputStream outputToServlet;
ObjectInputStream inputFromServlet;
Vector testVector = new Vector();
testVector.add("AAA");
testVector.add("BBB");
testVector.add("CCC");
try {
// Set up servlet connection
URLConnection servletConnection = _resultServlet.openConnect ion();
servletConnection.setDoInp ut(true);
servletConnection.setDoOut put(true);
servletConnection.setUseCa ches(false );
servletConnection.setDefau ltUseCache s(false);
servletConnection.setReque stProperty ("Content- Type", "application/octet-stream" );
// REQUEST
// Send testVector to servlet
outputToServlet = new ObjectOutputStream(servlet Connection .getOutput Stream());
outputToServlet.writeObjec t(testVect or);
outputToServlet.flush();
outputToServlet.close();
// RESPONSE
// Read the response from the servlet. The servlet will return a sericalized Vector containing
// Objects (just for testing)
inputFromServlet = new ObjectInputStream(servletC onnection. getInputSt ream()); //THIS IS WHERE THE EXCEPTION OCCURS
Vector vectorFromServlet = (Vector)inputFromServlet.r eadObject( );
inputFromServlet.close();
return vectorFromServlet;
} catch(Exception x) {
x.printStackTrace();
return null;
}
}
}
}
package result;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream ;
import java.util.Vector;
public class TestServlet extends HttpServlet {
private Vector _viewModels;
private Vector _viewsFromApplet;
public void init() {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ObjectInputStream inputFromApplet = null;
_viewsFromApplet = null;
try {
// Get an input stream from the applet
inputFromApplet = new ObjectInputStream(request. getInputSt ream());
_viewsFromApplet = (Vector)inputFromApplet.re adObject() ;
inputFromApplet.close();
} catch(Exception x) {
x.printStackTrace();
}
//Respond to applet
try {
_respondResultsToApplet(re sponse, _getObjectVector());
} catch(Exception x) {
x.printStackTrace();
}
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
private void _respondResultsToApplet(Ht tpServletR esponse response, Vector objects) throws Exception {
if (objects != null) {
ObjectOutputStream outputToApplet = new ObjectOutputStream(respons e.getOutpu tStream()) ;
outputToApplet.writeObject (objects);
outputToApplet.flush();
outputToApplet.close();
} else {
}
}
private Vector _getObjectVector() {
// Handle input data from applet
///...
// Objects to be passed back to applet
Vector v = new Vector();
v.add(new Object());
v.add(new Object());
return v;
}
}
I have a test JApplet with a tread running. Every X seconds, the tread sends a serialized Vector to the servlet as a http request and receives a serialized Vector as a http response. I have two questions:
1) In my applet, I have a Thread with the inner class UpdateHandler as its runnable target. In UpdateHandler, the above mentioned applet-servlet communication takes place.
When running my applet, I got an exception: java.io.NotSerializedExcep
2) When the NotSerializedExceptions are gone, I get another error when trying to read the response from servlet, when instantiating new ObjectInputStream(servletC
java.io.FileNotFoundExcept
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:707)
at sun.plugin.net.protocol.ht
at result.TestApplet$UpdateHa
at result.TestApplet$UpdateHa
at result.TestApplet$UpdateHa
at java.lang.Thread.run(Threa
Any ideas what causes the error? Help greatly appreciated!
package result;
import java.awt.*;
import java.util.*;
import java.net.URL;
import java.net.URLConnection;
import java.io.ObjectOutputStream
import java.io.ObjectInputStream;
import java.io.Serializable;
import javax.swing.*;
public class TestApplet extends JApplet implements {
private UpdateHandler _updateHandler;
private Thread _updateThread;
private URL _resultServlet;
public void init() {
super.init();
try {
String servletPath = "/emilion/servlet/result/T
_resultServlet = new URL("http", getCodeBase().getHost(), getCodeBase().getPort(), servletPath);
} catch(Exception x) {
x.printStackTrace();
}
// Set up the GUI
QCPanel bigViewPanel = new QCPanel();
QCLabel label = new QCLabel("TEST TEST");
bigViewPanel.add(label);
getContentPane().add(bigVi
// Updating thread
_updateHandler = new UpdateHandler();
_updateThread = new UpdateTread(_updateHandler
_updateThread.start();
}
private class UpdateTread extends Thread implements Serializable {
public UpdateTread(UpdateHandler updateHandler) {
super(updateHandler);
}
}
private class UpdateHandler implements Runnable, Serializable {
public final static int UPDATE_INTERVAL = 10;
public UpdateHandler() {
}
public void run() {
try {
_requestFromServlet();
// Update GUI with responded data from servlet
//…..
} catch (InterruptedException ie) {
}
}
private synchronized void _ requestFromServlet () throws InterruptedException {
while(true) {
_request();
wait(UPDATE_INTERVAL*1000)
}
}
public Vector _request() {
ObjectOutputStream outputToServlet;
ObjectInputStream inputFromServlet;
Vector testVector = new Vector();
testVector.add("AAA");
testVector.add("BBB");
testVector.add("CCC");
try {
// Set up servlet connection
URLConnection servletConnection = _resultServlet.openConnect
servletConnection.setDoInp
servletConnection.setDoOut
servletConnection.setUseCa
servletConnection.setDefau
servletConnection.setReque
// REQUEST
// Send testVector to servlet
outputToServlet = new ObjectOutputStream(servlet
outputToServlet.writeObjec
outputToServlet.flush();
outputToServlet.close();
// RESPONSE
// Read the response from the servlet. The servlet will return a sericalized Vector containing
// Objects (just for testing)
inputFromServlet = new ObjectInputStream(servletC
Vector vectorFromServlet = (Vector)inputFromServlet.r
inputFromServlet.close();
return vectorFromServlet;
} catch(Exception x) {
x.printStackTrace();
return null;
}
}
}
}
package result;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream
import java.util.Vector;
public class TestServlet extends HttpServlet {
private Vector _viewModels;
private Vector _viewsFromApplet;
public void init() {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ObjectInputStream inputFromApplet = null;
_viewsFromApplet = null;
try {
// Get an input stream from the applet
inputFromApplet = new ObjectInputStream(request.
_viewsFromApplet = (Vector)inputFromApplet.re
inputFromApplet.close();
} catch(Exception x) {
x.printStackTrace();
}
//Respond to applet
try {
_respondResultsToApplet(re
} catch(Exception x) {
x.printStackTrace();
}
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
private void _respondResultsToApplet(Ht
if (objects != null) {
ObjectOutputStream outputToApplet = new ObjectOutputStream(respons
outputToApplet.writeObject
outputToApplet.flush();
outputToApplet.close();
} else {
}
}
private Vector _getObjectVector() {
// Handle input data from applet
///...
// Objects to be passed back to applet
Vector v = new Vector();
v.add(new Object());
v.add(new Object());
return v;
}
}
> Seems strange to me that the objects
>containing the code for sending an object
>to/from the servlet, also need to be
>Serializable, not only the passed Vector. Why
>is it so?
This is weird.. You shouldn't serialize the object itself if you do not serialize it, just the object that you send through the ObjectInputStream and ObjectOutStream. Are you sure you are not sending the object itself ( UpdateHandler)?
>The exception is:
>
>java.io.FileNotFoundExcep tion: http://hostIP:portNo./emilion/servlet/result/TestServlet
>
> Any ideas what causes the error? Help greatly appreciated!
Can it be the dot you have after the port number?
>containing the code for sending an object
>to/from the servlet, also need to be
>Serializable, not only the passed Vector. Why
>is it so?
This is weird.. You shouldn't serialize the object itself if you do not serialize it, just the object that you send through the ObjectInputStream and ObjectOutStream. Are you sure you are not sending the object itself ( UpdateHandler)?
>The exception is:
>
>java.io.FileNotFoundExcep
>
> Any ideas what causes the error? Help greatly appreciated!
Can it be the dot you have after the port number?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I can see two possible problems with that url. Try
String servletPath = "./emilion/servlet/result/ TestServle t";
_resultServlet = new URL(getCodeBase(), servletPath);
String servletPath = "./emilion/servlet/result/
_resultServlet = new URL(getCodeBase(), servletPath);
ASKER
Thanks for quick respond.
2) I just replaced my host IP adress and port number with "hostIP:portNo.", where the portNo. is a 4 digit number.
1) I removed the implementation of Serializable and the error is not there any more... But I get it in my original applet, not this test applet, but that's another story...
I still get the FileNotFoundException, though.
2) I just replaced my host IP adress and port number with "hostIP:portNo.", where the portNo. is a 4 digit number.
1) I removed the implementation of Serializable and the error is not there any more... But I get it in my original applet, not this test applet, but that's another story...
I still get the FileNotFoundException, though.
>>I still get the FileNotFoundException, though.
Including trying my suggestion?
Including trying my suggestion?
Have you also checked about the dot (.) after the port number or is it just a syntax error?
ASKER
CEHJ,
I posted my previous comment before refreshing this page. Now that I have tried your suggestion I get the following error:
Bad URL in getProxySettings: http://<hostIP>:<port>./emilion/s ervlet/res ult/TestSe rvlet
when I add the dot at the beginning of the path.
Thanks anyway!
girionis,
The following address worked - thanks!
http://hostIP:portNo/emilion/servlet/result.TestServlet
In my original applet, I still need to figure out the Serialization trouble, I might have to come back to that later...
Thanks to all who take the time to help, I really appreciate it. Hope you approve with how I accept answers, please tell me if I am not follwing the standard.
I posted my previous comment before refreshing this page. Now that I have tried your suggestion I get the following error:
Bad URL in getProxySettings: http://<hostIP>:<port>./emilion/s
when I add the dot at the beginning of the path.
Thanks anyway!
girionis,
The following address worked - thanks!
http://hostIP:portNo/emilion/servlet/result.TestServlet
In my original applet, I still need to figure out the Serialization trouble, I might have to come back to that later...
Thanks to all who take the time to help, I really appreciate it. Hope you approve with how I accept answers, please tell me if I am not follwing the standard.
>In my original applet, I still need to figure out
>the Serialization trouble, I might have to come
>back to that later...
You *only* need to serialize the objects you are sending across the wire, nothing else. I would infer from the original error message that you are trying to send an object (maybe accidentally) that is not serializable.
>the Serialization trouble, I might have to come
>back to that later...
You *only* need to serialize the objects you are sending across the wire, nothing else. I would infer from the original error message that you are trying to send an object (maybe accidentally) that is not serializable.
ASKER