views:

393

answers:

5

I'm in the situation where I'm writing custom error pages for a webapp (primarily to reduce information disclosure from the servlet container's default error pages). Since I need an error page for each error status code, I'm going to have to have a sensible response for each code. As far as I can tell, these error pages don't have to be particularly user-friendly, but simply redirecting everything to a single "it went wrong" error page is going to make diagnosing problems very difficult.

So I'm wondering if there is a Java library that provides a good mapping between HTTP status codes and a brief human-readable description of them (ideally a 2-4 word "summary", for use as a page title, as well as a 1-3 sentence message expanding on the summary). Then I could just use this in a JSP to provide some feedback on the class of the error. If not I'm sure I can write one myself, but if wheels have been invented I'm happy to use them.

+3  A: 

Abstract class 'HttpURLConnection' provides you with constant int values for all HTTP status codes. Its documentation has a short verbal description for each constant. You could make yourself a simple enum with these values and strings, and use that.

Yuval
+3  A: 

I have not heard of any such library.

The 2 to 4 word summary is simple: just copy the short descriptions from the HTTP specs. The 1 to 3 sentence messages are more difficult. It would be hard to come up with messages that are simple enough for an end-user to understand, and still true to the breadth of meaning set out in the HTTP spec for each code. Then you've got the problem that it is not uncommon for the wrong HTTP status code to be used.

IMO, a better approach is for your servlet to set explicit error messages rather than just status codes.

Stephen C
OK, I thought that might be the case - I'll use RFC 2616 as the basis for this map then. Explicit error messages are used when appropriate - but if Tomcat returns any error code for any reason (possibly before my servlet's even invoked) I need to provide a custom error page to handle it. My situation doesn't seem unique, so I was hoping there was some generic error-code-to-human-response library code somewhere. :-)
Andrzej Doyle
A: 

If you're using Jersey (or have it on your classpath), the javax.ws.rs.core.Response.Status enum includes human-readable "reasons" from the HTTP spec, e.g. Response.Status.fromStatusCode(404).toString() gives "Not Found".

DanC
+3  A: 

So I'm wondering if there is a Java library that provides a good mapping between HTTP status codes and a brief human-readable description of them (ideally a 2-4 word "summary", for use as a page title, as well as a 1-3 sentence message expanding on the summary).

Yes, Apache Commons HttpClient has this functionality. The HttpStatus class has the same list if static int fields that you'll find elsewhere, but it also has a static String getStatusText(int) method that returns a huiman-readable description of the status code.

skaffman
+1  A: 

I think you might like the Spring solution: http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/http/HttpStatus.html

Ed J