17

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.

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
  • need this in javascript..... I also object to the question closure. It was unfortunately worded. The people voting to close it should be encouraged first to edit the question. – Adam Mar 05 '19 at 11:39

7 Answers7

13

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 of int constants that you'll find elsewhere, but it also has a static String getStatusText(int) method that returns a human-readable description of the status code.

Here is the Maven dependency:

<dependency>
    <groupId>commons-httpclient</groupId>
    <artifactId>commons-httpclient</artifactId>
    <version>3.1</version>
</dependency>

Example code:

import org.apache.commons.httpclient.HttpStatus;

String responseMessage = HttpStatus.getStatusText(HttpStatus.SC_OK);
System.out.println(responseMessage);

Prints:

OK
BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
skaffman
  • 398,947
  • 96
  • 818
  • 769
8

Latest apache http components v4+ ( httpCoponents )

Introduces the following: HttpStatus as an enum containing all of the httpStatuses

For their description use: EnglishReasonPhraseCatalog.INSTANCE.getReason(code, Locale.ENGLISH);

Where your status code == code, eg: 200

For example:

HttpStatus.SC_FORBIDDEN == 403
EnglishReasonPhraseCatalog.INSTANCE.getReason(403, Locale.ENGLISH) == "Forbidden"
Chaiavi
  • 769
  • 9
  • 23
  • 1
    Yep, `org.apache.commons.httpclient.HttpStatus` is [no longer supported](https://hc.apache.org/httpclient-legacy/); [org.apache.http.HttpStatus](https://www.javadoc.io/static/org.apache.httpcomponents/httpcore/4.4.4/org/apache/http/HttpStatus.html) should be used instead. But [EnglishReasonPhraseCatalog.getReason()](https://www.javadoc.io/static/org.apache.httpcomponents/httpcore/4.4.4/org/apache/http/impl/EnglishReasonPhraseCatalog.html#getReason(int,%20java.util.Locale)) is need to get an HTTP status code's phrase; that info is no longer part of a `HttpStatus` object. – Mass Dot Net Jun 14 '22 at 20:17
7

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
  • 2,466
  • 4
  • 26
  • 20
  • org.springframework.http.HttpStatus.name() can be used for the status code description and HttpStatus.value() for the status code number – Somu Nov 01 '11 at 22:07
  • 1
    `HttpStatus.valueOf(status_code)?.getReasonPhrase()` returns the pretty print. – sebnukem Aug 06 '14 at 22:22
5

Maybe the easiest solution is to use next function:

httpResponse.getStatusLine().getReasonPhrase()

It returns exactly what's needed.

olshevski
  • 4,971
  • 2
  • 35
  • 34
4

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
  • 7,987
  • 12
  • 40
  • 54
1

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
  • 1,844
  • 13
  • 12
  • 5
    Why does `Response.Status.fromStatusCode(405)` return `null`? – Stefan Jun 30 '11 at 11:31
  • 1
    @Stefan: I just spent a while banging my head against this too. NOT_ALLOWED is just not included in Response.Status enum. Note that there is in fact an interface http://docs.oracle.com/javaee/6/api/javax/ws/rs/core/Response.StatusType.html that lets you create your own status types. Useful, but only if whatever you're using it for will accept a StatusType instance rather than a Status instance... – PapaFreud Jan 15 '13 at 15:10
  • @Stefan it exists in `ClientResponse.Status` and in `Responses.METHOD_NOT_ALLOWED_TYPE`. I ended up creating my own enum. – GuiSim Mar 14 '17 at 21:43
0

You can use my HttpStatus JSP Tag Libray:

<%@ page isErrorPage="true" %>
<%@ taglib prefix="hs" uri="http://erik.thauvin.net/taglibs/httpstatus" %>
<html><head>
<title><hs:code/> <hs:reason default="Server Error"/></title>
</head>
<h1><hs:reason default="Server Error"/></h1>
Cause: <pre><hs:cause default="Unable to complete your request."/></pre>  

Specifically, as shown in the example above, the <hs:reason/> tag is used to display the reason phrase (human-readable description) for the current HTTP status code in the page's title and first heading.

Additionally, the <hs:code/> tag is used to display the current HTTP status code and the <hs:cause/> tag to display the message from the exception that caused the error.

  • Please go into detail why your library solves this problem, specifically. Whenever posting a code snippet it helps to explain what it is doing, and why it addresses the issue. – Nathaniel Ford Dec 04 '15 at 18:50