M
Matthew Zimmer
Hi all,
I've got a question about what I'm doing wrong with a servlet I'm
running. Basically, imagine that I have a servlet that runs some
operation and then returns a simple value. The client code makes the
request to the servlet which properly does the operation. However, the
client then attempts to read the result and throws an IOException like this:
java.io.IOException: Server returned HTTP response code: 405 for URL:
http://localhost:8080/myServlets/myServlet
at
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:709)
Here's some of the code that's causing the problems:
Servlet:
public void service(
HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
super.service(request, response);
boolean sentResponse = false;
response.setContentType("text/xml");
try
{
boolean ok = false;
// do a bunch of code here, including writing a file to the
hard drive. This appears to be working as I check the server and all
the stuff is there.
// ok = some result based on the code.
String res = (ok ? "woo hoo" : "doh");
PrintWriter out = response.getWriter();
out.write(res);
out.flush();
out.close();
sentResponse = true;
}
catch (Exception e)
{
e.printStackTrace();
}
try
{
if (!sentResponse)
{
PrintWriter out = response.getWriter();
out.write("<doh>doh</doh>");
out.flush();
out.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
Client: (note, HttpMessage is nothing more than a wrapper class to
create a url)
private void contactServlet()
{
try
{
HttpMessage servletMessage = new HttpMessage();
// fill in my servletMessage with the appropriate data
URL servlet = servletMessage.getURL();
byte[] args = servletMessage.getArguments();
InputStream istream = sendPostMessage(servlet, args);
}
catch (Exception e)
{
// I always crash on sendPostMessage(...) at con.getInputStream
e.printStackTrace();
}
}
public InputStream sendPostMessage(URL servlet, byte[] args) throws
IOException
{
URLConnection con = servlet.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty(
"Content-Type",
"application/x-www-form-urlencoded");
sendHeaders(con);
DataOutputStream out = new DataOutputStream(con.getOutputStream());
out.writeBytes(args);
out.flush();
out.close();
return con.getInputStream(); // this crashes
}
I'm sorry for the length of code, but I wanted to make sure you got most
of what I had. I've trimmed out the stuff that's not really related
(AFAIK) to the problem. Also, I use the same mechinism for several
different servlets and none of them are giving me this problem. Does
anybody have any idea what I might be doing wrong here? I've tried
using both "get" and "post" methods with no success. Thanks for any
help you have.
Matthew Zimmer
I've got a question about what I'm doing wrong with a servlet I'm
running. Basically, imagine that I have a servlet that runs some
operation and then returns a simple value. The client code makes the
request to the servlet which properly does the operation. However, the
client then attempts to read the result and throws an IOException like this:
java.io.IOException: Server returned HTTP response code: 405 for URL:
http://localhost:8080/myServlets/myServlet
at
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:709)
Here's some of the code that's causing the problems:
Servlet:
public void service(
HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
super.service(request, response);
boolean sentResponse = false;
response.setContentType("text/xml");
try
{
boolean ok = false;
// do a bunch of code here, including writing a file to the
hard drive. This appears to be working as I check the server and all
the stuff is there.
// ok = some result based on the code.
String res = (ok ? "woo hoo" : "doh");
PrintWriter out = response.getWriter();
out.write(res);
out.flush();
out.close();
sentResponse = true;
}
catch (Exception e)
{
e.printStackTrace();
}
try
{
if (!sentResponse)
{
PrintWriter out = response.getWriter();
out.write("<doh>doh</doh>");
out.flush();
out.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
Client: (note, HttpMessage is nothing more than a wrapper class to
create a url)
private void contactServlet()
{
try
{
HttpMessage servletMessage = new HttpMessage();
// fill in my servletMessage with the appropriate data
URL servlet = servletMessage.getURL();
byte[] args = servletMessage.getArguments();
InputStream istream = sendPostMessage(servlet, args);
}
catch (Exception e)
{
// I always crash on sendPostMessage(...) at con.getInputStream
e.printStackTrace();
}
}
public InputStream sendPostMessage(URL servlet, byte[] args) throws
IOException
{
URLConnection con = servlet.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty(
"Content-Type",
"application/x-www-form-urlencoded");
sendHeaders(con);
DataOutputStream out = new DataOutputStream(con.getOutputStream());
out.writeBytes(args);
out.flush();
out.close();
return con.getInputStream(); // this crashes
}
I'm sorry for the length of code, but I wanted to make sure you got most
of what I had. I've trimmed out the stuff that's not really related
(AFAIK) to the problem. Also, I use the same mechinism for several
different servlets and none of them are giving me this problem. Does
anybody have any idea what I might be doing wrong here? I've tried
using both "get" and "post" methods with no success. Thanks for any
help you have.
Matthew Zimmer