Hai,all
I am facing a strange problem between
applet and servlet.I am
testing a example about sending data from applet,getting data in
servlet and then sending the data to the applet.When I use applet to connect servlet,
I always get the error information:java.io.FileNotFoundException:
http://localhost:8080/webbuilder/webtrend/servlet/DataStreamEcho.So I need your help.
I am using URL Connection object and opening a connection to the servlet.A part of my program is:
try{
URL url = new URL("http://localhost:8080/webbuilder/webtrend/servlet/DataStreamEcho");
if( url == null ){
throw new IOException("Server URL has not been set");
}
System.out.println("Attempting to connect to " + url);
// Attempt to connect to the host
java.net.URLConnection con = url.openConnection();
// Initialize the connection
con.setUseCaches(false);
con.setDoOutput(true);
con.setDoInput(true);
// Data will always be written to a byte array buffer so
// that we can tell the server the length of the data
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
// Create the output stream to be used to write the
// data to our buffer
DataOutputStream out = new DataOutputStream(byteOut);
System.out.println("Writing test data");
// Write the test data
out.writeBoolean(true);
out.writeByte(1);
out.writeChar(2);
out.writeShort(3);
out.writeInt(4);
out.writeFloat(5);
out.writeDouble(6);
out.writeUTF("Hello, Karl");
// Flush the data to the buffer
out.flush();
// Set the content that we are sending
con.setRequestProperty("Content-type",
"application/octet-stream");
// Set the length of the data buffer we are sending
con.setRequestProperty("Content-length",
"" + buf.length);
// Get the output stream to the server and send our
// data buffer
DataOutputStream dataOut =
new DataOutputStream(con.getOutputStream());
//out.write(buf, 0, buf.length);
dataOut.write(buf);
// Flush the output stream and close it
dataOut.flush();
dataOut.close();
System.out.println("Reading response");
// Get the input stream we can use to read the response
DataInputStream in =
new DataInputStream(con.getInputStream());
// Read the data from the server
boolean booleanValue = in.readBoolean();
byte byteValue = in.readByte();
char charValue = in.readChar();
short shortValue = in.readShort();
int intValue = in.readInt();
float floatValue = in.readFloat();
double doubleValue = in.readDouble();
String stringValue = in.readUTF();
// Close the input stream
in.close();
System.out.println("Data read: " +
booleanValue + " " +
byteValue + " " +
((int) charValue) + " " +
shortValue + " " +
intValue + " " +
floatValue + " " +
doubleValue + " " +
stringValue);
}
catch (Exception ex) {
ex.printStackTrace();
}
I get the result is:
Attempting to connect to
http://localhost:8080/webbuilder/webtrend/servlet/DataStreamEcho Writing test data
Reading response
java.io.FileNotFoundException:
http://localhost:8080/webbuilder/webtrend/servlet/DataStreamEcho at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:707)
at sun.plugin.net.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:384)
at TestDataStream._invokeMethod(TestDataStream.java:112)
at TestDataStream._initialize(TestDataStream.java:29)
at TestDataStream.init(TestDataStream.java:15)
at sun.applet.AppletPanel.run(AppletPanel.java:347)
at java.lang.Thread.run(Thread.java:536)
I put the TestDataStream.java,TestDataStream.class and DataStreamEcho.java,DataStreamEcho.class in the same path.They all in F:\webbuilder\webapps\cms\webtrend\
Please let me know if iam wrong.
I want somebody to answer my question.
regards
thanks
EricDu
E-mail:cow775@sohu.com or
dpdzq@sina.com