net.thauvin.erik.httpstatus:httpstatus on Maven (2024)

Tag library to display the code, reason, cause and/or message for HTTP status codes in JSP error pages

net.thauvin.erik.httpstatus:httpstatus on Maven (1)net.thauvin.erik.httpstatus:httpstatus on Maven (2)net.thauvin.erik.httpstatus:httpstatus on Maven (3)net.thauvin.erik.httpstatus:httpstatus on Maven (4)net.thauvin.erik.httpstatus:httpstatus on Maven (5)net.thauvin.erik.httpstatus:httpstatus on Maven (6)

net.thauvin.erik.httpstatus:httpstatus on Maven (7)net.thauvin.erik.httpstatus:httpstatus on Maven (8)net.thauvin.erik.httpstatus:httpstatus on Maven (9)net.thauvin.erik.httpstatus:httpstatus on Maven (10)

A simple JSP Tag Library to display the code, reason, cause and/or message for HTTP status codes in JSP error pages.

Table of Contents

  • Examples
  • Usage
    • Gradle
    • bld
    • Maven
  • JSP Tags
    • hs:cause
    • hs:code
    • hs:message
    • hs:reason
  • StatusCode Bean
  • Reasons
  • Command Line Usage
  • Contributing

Examples

For example:

<%@ 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>Message: <pre><hs:message default="A server error has occured."/></pre>...

or

<%@ page isErrorPage="true" import="net.thauvin.erik.httpstatus.Reasons" %><%= Reasons.getReasonPhrase(pageContext.getErrorData().getStatusCode()) %>

would display on a 501 status code:

Not Implemented

Include the following in your build.gradle file:

repositories { mavenCentral()}dependencies { implementation 'net.thauvin.erik.httpstatus:httpstatus:1.1.1'}

Include the following in your bld build file:

scope(compile).include( dependency("net.thauvin.erik.httpstatus","httpstatus", version(1, 1, 0)));

As a Maven artifact:

<dependency> <groupId>net.thauvin.erik.httpstatus</groupId> <artifactId>httpstatus</artifactId> <version>1.1.1</version></dependency>

JSP Tags

hs:cause

The <hs:cause/> tag displays the cause of current HTTP status code, if any. A shorthand for:

<%= pageContext.getErrorData().getThrowable().getCause().getLocalizedMessage() %>

Optional attributes are:

AttributeDescription
defaultThe fallback value to output, if no cause is
escapeXmlConverts <, >, &, ', " to their corresponding entity codes. Value is true by default.

hs:code

The <hs:code/> tag displays the current HTTP status code, if any. A shorthand for:

<%= pageContext.getErrorData().getStatusCode() %>

hs:message

The <hs:message/> tag displays the current error message, if any. A shorthand for:

<%= request.getAttribute("javax.servlet.error.message") %>

Optional attributes are:

AttributeDescription
defaultThe fallback value to output, if no error message is available.
escapeXmlConverts <, >, &, ', " to their corresponding entity codes. Value is true by default.

hs:reason

The <hs:reason/> tag displays the reason for an HTTP status code, if any. Optional attributes are:

AttributeDescription
defaultThe fallback value to output, if no reason is available.
codeThe HTTP status error code. If not specified the current status code is used.
escapeXmlConverts <, >, &, ', " to their corresponding entity codes. Value is true by default.

StatusCode Bean

The StatusCode bean can be used to check the class of the status code error. For example, using the JSTL:

<%@ taglib prefix="hs" uri="http://erik.thauvin.net/taglibs/httpstatus" %><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><jsp:useBean id="statusCode" class="net.thauvin.erik.httpstatus.StatusCode"/><c:set target="${statusCode}" property="code"><hs:code/></c:set><c:choose> <c:when test="${statusCode.isClientError()}"> An error occurred on your side. (<hs:reason/>) </c:when> <c:otherwise> An error occurred on our side. (<hs:message/>) </c:otherwise></c:choose>

or in a Servlet:

import net.thauvin.erik.httpstatus.StatusCode;public class ExampleServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) { var statusCode = new StatusCode( (Integer) request.getAttribute("javax.servlet.error.status_code")); if (statusCode.isError()) { if (statusCode.isServerError()) { var reason = statusCode.getReason(); } else { // ... } } }}

The StatusCode bean methods are:

MethodDescription
getReasonReturns the reason for the status code (eg: Internal Server Error)
isClientErrorChecks if the status code is a client error.
isErrorChecks if the status code is a server or client error.
isInfoChecks if the status code is informational.
isRedirectChecks if the status code is a redirect.
isServerErrorChecks if the status code is a server error.
isSuccessChecks if the status code is a success. (OK)
isValidChecks if the status code is valid.

Reasons

The reasons are defined in a ResourceBundle properties as follows:

Status CodeReason
100Continue
101Switching Protocols
102Processing
103Early Hints
110Response is Stale
111Revalidation Failed
112Disconnected Operation
113Heuristic Expiration
199Miscellaneous Warning
200OK
201Created
202Accepted
203Non-Authoritative Information
204No Content
205Reset Content
206Partial Content
207Multi-Status
208Already Reported
214Transformation Applied
218This is fine
226IM Used
299Miscellaneous Persistent Warning
300Multiple Choices
301Moved Permanently
302Found/Moved Temporarily
303See Other
304Not Modified
305Use Proxy
306Unused
307Temporary Redirect
308Permanent Redirect
400Bad Request
401Unauthorized
402Payment Required
403Forbidden
404Not Found
405Method Not Allowed
406Not Acceptable
407Proxy Authentication Required
408Request Timeout
409Conflict
410Gone
411Length Required
412Precondition Failed
413Payload Too Large
414URI Too Long
415Unsupported Media Type
416Range Not Satisfiable
417Expectation Failed
418I'm A Teapot
419Insufficient Space on Resource
420Method Failure
421Misdirected Request
422Unprocessable Content
423Locked
424Failed Dependency
425Too Early
426Upgrade Required
428Precondition Required
429Too Many Requests
430Request Header Fields Too Large
431Request Header Fields Too Large
440Login Timeout
444No Response
449Retry With
450Blocked by Windows Parental Controls
451Unavailable For Legal Reasons
460Client Closed Connection Before Load Balancer Idle Timeout
463X-Forwarded-For Header with More than 30 IP Addresses
494Request Header Too Large
495SSL Certificate Error
496SSL Certificate Required
497HTTP Request Sent to HTTPS Port
498Token Expired/Invalid
499Client Closed Request
500Internal Server Error
501Not Implemented
502Bad Gateway
503Service Unavailable
504Gateway Timeout
505HTTP Version Not Supported
506Variant Also Negotiates
507Insufficient Storage
508Loop Detected
509Bandwidth Limit Exceeded
510Not Extended
511Network Authentication Required
520Unknown Error
521Web Server Is Down
522Connection Timed Out
523Origin Is Unreachable
524A Timeout Occurred
525SSL Handshake Failed
526Invalid SSL Certificate
527Railgun Error
529Site is overloaded
530Site is frozen
540Temporarily Disabled
561Unauthorized
598Network Read Timeout Error
599Network Connect Timeout Error
783Unexpected Token

Command Line Usage

You can query the reason phrase for status codes as follows:

$ java -jar httpstatus-1.1.1.jar 404 500404: Not Found500: Internal Server Error

If no status code is specified, all will be printed:

$ java -jar httpstatus-1.1.1.jar100: Continue101: Switching Protocols102: Processing103: Early Hints110: Response is Stale111: Revalidation Failed112: Disconnected Operation113: Heuristic Expiration199: Miscellaneous Warning200: OK201: Created202: Accepted203: Non-Authoritative Information...

You can also print status codes by response classes:

$ java -jar httpstatus-1.1.1.jar 2xx200: OK201: Created202: Accepted203: Non-Authoritative Information...

Contributing

If you want to contribute to this project, all you have to do is clone the GitHubrepository:

git clone git@github.com:ethauvin/HttpStatus.git

Then use bld to build:

cd HttpStatus./bld compile

The project has an IntelliJ IDEA project structure. You can just open it after allthe dependencies were downloaded and peruse the code.

net.thauvin.erik.httpstatus:httpstatus on Maven (2024)
Top Articles
Latest Posts
Article information

Author: Rev. Leonie Wyman

Last Updated:

Views: 6365

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Rev. Leonie Wyman

Birthday: 1993-07-01

Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

Phone: +22014484519944

Job: Banking Officer

Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.