Hi,
I know this has been discussed many times on this list, but I keep having
problems with client connecting to a servlet via URLConnection and POST-ing
an object to it.
The client blocks when trying to open an input stream from the servlet.
This is my client code:
ObjectOutputStream outputToServlet = null;
InputStream inputFromServlet = null;
try {
URL reloadServlet = new URL("http://localhost:8080/servletalias");
URLConnection servletConnection = reloadServlet.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(getServletDescriptor(className)); // just
my business object
outputToServlet.flush();
outputToServlet.close();
outputToServlet = null;
System.out.println("Connecting 1");
inputFromServlet = servletConnection.getInputStream();
System.out.println("Connecting 2");
int resp = inputFromServlet.read();
String response = "" + (char)resp;
System.out.println(response);
inputFromServlet.close();
inputFromServlet = null;
if (response == null)
return false;
return true;
}
catch (IOException e) {
e.printStackTrace();
return false;
}
finally {
try {
if (outputToServlet != null)
outputToServlet.close();
if (inputFromServlet != null)
inputFromServlet.close();
}
catch (IOException e) {}
}
And this is my servlet code:
ObjectInputStream in = null;
PrintWriter outstr = null;
try {
ServletDescriptor servDescr = null;
BufferedReader inTest = null;
try {
in = new ObjectInputStream(request.getInputStream());
response.setContentType("text/plain");
outstr = response.getWriter();
servDescr = (ServletDescriptor)in.readObject();
if (servDescr == null)
throw new IOException("Object not loaded");
/* do my "business logic" here */
outstr.println("OK");
System.out.println("OK");
}
catch(Exception e) {
outstr.println("ERROR");
System.out.println("ERROR");
}
}
finally {
if (in != null)
in.close();
if (outstr != null) {
outstr.flush();
outstr.close();
}
System.out.println("CLOSED");
}
The servlet and the client run on the same NT machine in different JVMs. The
servlet code is all executed, the object passed from the client is read
without problems, no exception is thrown and everything seems ok. But the
client blocks on
inputFromServlet = servletConnection.getInputStream();
(it prints out "Connecting 1", but not "Connecting 2"). When the servlet JVM
exits, the client throws
java.net.SocketException: Connection reset by peer: JVM_recv in socket input
stream read
I've tried opening inputstreams and outputstreams in different order on both
client and servlet, but nothing seems to help.
Does anyone know what's going on here? I use Jetty web server.
Any help is much appreciated.
Thanks
Petr Jiricka
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html