3

How can I disable the default response body when Jetty catches an exception and returns an error? I am not using any XML or WAR and I don't want to add any.

I prefer to avoid doing a try { // Servlet code } catch (Exception e) { resp.setStatus(500) } in every servlet.

If I don't do that, Jetty will return a 500 response with a body that specifies the stack trace. If a not found endpoint is reached, Jetty will return a 404 response with a body that says "Powered by Jetty". I want to remove those bodies and just keep the response code.

This is the code that starts my Jetty server:

private static void startServer() throws Exception {
    final org.eclipse.jetty.server.Server server = new org.eclipse.jetty.server.Server(7070);
    final WebAppContext context = new WebAppContext("/", "/");
    context.setConfigurations(new Configuration[] { new AnnotationConfiguration(), new WebInfConfiguration() });
    context.setExtraClasspath("build/classes/main/com/example");
    server.setHandler(context);
    server.start();
    server.join();
}

2 Answers2

3

The solution is described in the Jetty documentation:

A class extending ErrorHandler is needed in order to override Jetty default behavior when generating the error page. It may be registered via the ContextHandler or the Jetty server.

The CustomErrorHandler class:

public class CustomErrorHandler extends ErrorHandler {

    @Override
    protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) throws IOException {}
}

I then added this to my Jetty embedded configuration: context.setErrorHandler(new CustomErrorHandler());

  • If you are adding this to a WebAppContext, you need to extend from `ErrorPageErrorHandler`, not `ErrorHandler` (as that's for non-context, and Server side errors) – Joakim Erdfelt Nov 11 '19 at 12:37
1

In your WEB-INF/web.xml of your war file, specify the <error-page> element you want to use to handle the errors.

Example:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
  <servlet>
    <servlet-name>myerror</servlet-name>
    <servlet-class>com.company.MyErrorServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>myerror</servlet-name>
    <url-pattern>/myerror</url-pattern>
  </servlet-mapping>

  <error-page>
    <location>/myerror</location>
  </error-page>
</web-app>

The <error-page> element can be quite powerful.

See other answer at https://stackoverflow.com/a/16340504/775715

Community
  • 1
  • 1
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • 1
    I'm not using any XML or WAR artifact, just annotations for servlets. Is there a programatic solution? In your solution, will every error (500, 404, etc) go to the same MyErrorServlet? I want to return the same error code but without the Jetty response body that leaks data. –  Apr 03 '17 at 19:08
  • The fact that you are using a `WebAppContext` means you are using a war (it can be an exploded directory). You cannot specify error-page with annotations (there's no annotation for it), you must use the `WEB-INF/web.xml`. See the other answer to understand error dispatch and the attributes about the error state. – Joakim Erdfelt Apr 03 '17 at 19:27