2

My client uploads a file to my web application where the file is processed and the file is returned to the client. My client is written in java and so is server side application using servlets. My problem is when there is some error in processing, how to send the error to the client from the server. So that client do not go for downloading the file, instead just exit by printing the error. I don't know what is the standard way of communication this message with the client. What I am doing currently is using response.setHeader :

response.setHeader("codesign-status", "0");
response.setHeader("codesign-msg", "File available for download ");

And I am reading these header on client connection to check for error. Is this standard way of communicating messages with client?

Akhil
  • 533
  • 2
  • 11
  • 26
  • Return an error HTTP Status code, (e.g. HTTP Error 4xx and 5xx) with a message as an entity body. – Buhake Sindi Jan 27 '15 at 12:49
  • Is there a reason you do not prefer standard status codes for your response? 200/OK for success and 500/Internal Server Error for processing errors. – ramp Jan 27 '15 at 12:50
  • @BuhakeSindi But these error may not be http error. This can be simple error with status like -1,-2 and error message String. If this can work, how to send these code from servlet and receive at client. – Akhil Jan 27 '15 at 12:52
  • @ramp But these error may not be http error. This can be simple error with status like -1,-2 and error message String – Akhil Jan 27 '15 at 12:52
  • You will have to return an HTTP error code and part of your message will include your custom status code. – Buhake Sindi Jan 27 '15 at 13:25

1 Answers1

6

Consider using standard HTTP response codes and the sendError()method:

protected void doPost(HttpServletRequest request, HttpServletResponse response) {
    if (!response.isCommitted()) {
        response.sendError(
            HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
            "Error message");
    }
//...
}
Udo Klimaschewski
  • 5,150
  • 1
  • 28
  • 41
  • How to get this error on Java client using HttpConnection? I was to print this error along with status. And status not necessarily be Http error. – Akhil Jan 27 '15 at 13:59
  • Here is a pretty good example for the client side: http://stackoverflow.com/a/1441491/1740554 – Udo Klimaschewski Jan 27 '15 at 14:09