A.6. Generating the Server Response: HTTP Status Codes

Format of an HTTP Response

Status line (HTTP version, status code, message), response headers, blank line, document, in that order. For example:

HTTP/1.1 200 OK
Content-Type: text/plain

Hello World

Methods That Set Status Codes

These are methods in HttpServletResponse. Set status codes before you send any document content to browser.

  • public void setStatus(int statusCode)

    Use a constant for the code, not an explicit int.

  • public void sendError(int code, String message)

    Wraps message inside small HTML document.

  • public void sendRedirect(String url)

    Relative URLs permitted in 2.2.

Status Code Categories

  • 100-199: informational; client should respond with some other action.

  • 200-299: request was successful. ...

Get Core Servlets and JavaServer Pages™ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.