HTTP status codes - novalexei/mod_servlet GitHub Wiki

HTTP status codes and error handling

Sun is not always shining and sometimes we encounter an error. In this case we can return some HTTP status code which will tell the client what's wrong. For the list and description of HTTP status codes you can refer to Section 10 of RFC2616. HTTP status codes are also listed in fields of http_response class.

You can indicate which status code you want to return to the client by calling method http_response::set_status.

As I already mentioned in Redirect, Forward, Include section, Apache server tries being smart and substitutes the raw error codes with its own error messages. Sometimes it is expected and sometimes not, but this is the way it is.

If you want to provide your own error pages for specific error codes you can do it right in the web.xml deployment descriptor with error-code tags. It gives the flexibility of having your error pages specific for your web application.

For example, if you have custom page error404.html in your web application, add this section to the web.xml:

    <error-page> 
        <error-code>404</error-code> 
        <location>/error404.html</location> 
    </error-page>

And return 404 from a servlet like this:

resp.set_status(http_response::SC_NOT_FOUND);

You also can throw an exception in the servlet body. This exception will be caught by mod_servlet container, reported and HTTP status code 500 (Internal Server Error) will be returned.

And I believe we are ready to write some Filters now.

To The Programming Guide

⚠️ **GitHub.com Fallback** ⚠️