views:

23

answers:

2

I want to write a servlet that will return an http response like so:

HTTP/1.1 500 <short custom message>
Content-Length: ...

<longer custom message>

The reason is that I want a programmatic client to be able to process the response message to take a particular response but I also want to fill in the response body with a longer explanation so that it's easy to hit using a browser.

Now, HttpServletResponse has a sendError(int, String) method that allows me to specify the error code and a message. The javadocs only say that the message will be embedded in some kind of html page, but nothing about setting the http response message. After calling this method, you are not allowed to write anything else to the response. In my tests (with jetty), the message is used both for the http response and an html body, which would be fine with me, except that I want to specify two different strings and I don't think the setting of the http response message is guaranteed with a different implementation.

There's also a setStatus(int) method that you can call with any code, and then you can write your own html body. This is close, except that you can't specify the http response message.

Finally, there's a setStatus(int, String) method that actually does exactly what I want, but it's deprecated due to some kind of ambiguity. I'm assuming some servlet containers were writing the message to the response body and closing the response.

Aside from using the deprecated method, I'm assuming I'm screwed here, but I'm curious if anyone else knows any tricks?

+2  A: 

You can customize error pages by specifying error specific pages in web.xml:

<error-page>
  <error-code>500</error-code>
  <location>/error500.jsp</location>
</error-page>

In the jsp file you can use request attributes to show a custom message and other info.

FRotthowe
Thanks, that is an excellent point and would probably help most people with this problem :) I happen to be using embedded jetty without the whole web app setup: this is a completely stand-alone servlet. I can at least poke around to see if the error page can somehow be setup in code, but I'll probably settle on having programmatic callers just look in the body for the details.
Jeremy Huiskamp
A: 

There's also a setStatus(int) method that you can call with any code, and then you can write your own html body. This is close, except that you can't specify the http response message.

Is there any reason why you would not use response.setStatus(500) to set the HTTP status code, followed by writing to the response's output stream?

This is how "writing to the response body" is achieved at the lowest level.

Here's an example:

public class ErrorServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

        // 500 error
        resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        resp.getWriter().print("<html><head><title>Oops an error happened!</title></head>");
        resp.getWriter().print("<body>Something bad happened uh-oh!</body>");
        resp.getWriter().println("</html>");
    }
}

web.xml:

<web-app>
    <servlet>
        <servlet-name>myerror</servlet-name>
        <servlet-class>brown.testservlet.ErrorServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>myerror</servlet-name>
        <url-pattern>/myErrorPage</url-pattern>
    </servlet-mapping>
</web-app>

And the output:

$ curl -I http://localhost:8080/testservlet/myErrorPage  
HTTP/1.1 500 Internal Server Error  
Content-Length: 108  
Server: Jetty(6.1.16)  

$ curl -v http://localhost:8080/testservlet/myErrorPage
* About to connect() to localhost port 8080 (#0)
*   Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /testservlet/myErrorPage HTTP/1.1
> User-Agent: curl/7.20.0 (i686-pc-mingw32) libcurl/7.20.0 OpenSSL/0.9.8k zlib/1.2.3
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 500 Internal Server Error
< Content-Length: 108
< Server: Jetty(6.1.16)
<
<html><head><title>Oops an error happened!</title></head><body>Something bad happened uh-oh!</body></html>
* Connection #0 to host localhost left intact
* Closing connection #0
matt b
Yes, the part of my question that you quoted states that I understand this option. It also points out that this does not allow you to set the http response message (the "Internal Server Error" in your example, which is automatically supplied by the servlet container).
Jeremy Huiskamp
Even though `response.setStatus(int, String)` is deprecated, you *should* be able to use it to set the status line message
matt b