0

I am trying to send some text back from server side to the client. I have tried response.setStatus, response.setHeader but they all does not work. I need some help

Here is my client:

 public static void main(String[] args) throws MalformedURLException, IOException {
    URL url = new URL("http://localhost:8080/WebServiceDesignStyles3ProjectServer/NewServlet/www");

    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    url.openConnection();
    con.setRequestMethod("GET");
    con.setRequestProperty("Accept", "text/xml");
    con.setDoOutput(true);
    con.setDoInput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeUTF("aaa");
    wr.flush();
    wr.close();

    InputStream is = con.getInputStream();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    String line;
    StringBuffer response = new StringBuffer();
    while ((line = rd.readLine()) != null) {
        response.append(line);
        response.append('\r');
    }
    rd.close();
    System.out.println(response);
    System.out.println(con.getResponseCode());
    System.out.println(con.getResponseMessage());

}

}

Here is my doGET method from server:

   @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    //processRequest(request, response);
 response.setStatus(404);
  response.setContentType("text/xml");
  PrintWriter writer=response.getWriter();
  writer.append("this is 404");
 writer.flush();






}

But my client still prints out 200 and OK, which are default. How can I send some message back to client.

Edit:

Solved, I should not have request body for doGet.

Thanks.

4
  • Have you tried accessing your server in a web browser to see what it will return? Commented Mar 14, 2015 at 1:48
  • Try to open the URL in browser and see whether it is 404 and so on. Commented Mar 14, 2015 at 1:49
  • The most obvious thing to investigate is whether your code is actually serving the URL you are trying to access. If you can debug your servlet container, place breakpoints in the doGet method, or alternatively insert logging statements at the beginning of doGet to ensure that the method is actually invoked. Commented Mar 14, 2015 at 1:50
  • I opened the URL in my browser, it did not show 404. I set up breakpoint in doGet, unfortunately doGet is not invoked. Actually doPost is invoked. Commented Mar 14, 2015 at 16:36

2 Answers 2

3

Actually doPost is invoked.

That presumably doesn't have the lines

response.setStatus(404);
response.setContentType("text/xml");
PrintWriter writer=response.getWriter();
writer.append("this is 404");

Either ensure doGet() is being used or implement the above in doPost().

Sign up to request clarification or add additional context in comments.

Comments

3

When you set doOutput to true you also set the request method to POST: see the Javadoc. So your doGet() method isn't being invoked.

It doesn't make sense to try to combine GET with output to the request. A GET request cannot have a request body.

1 Comment

Thanks, I just found this issue. I removed all request body part.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.