2

Is it possible to configure the ErrorPageErrorHandler in way that it redirects to a static Page if no content/service is found?

Here is my Code:

server = new Server(port);

    Resource webRoot = Resource.newResource(webContent);
    if (!webRoot.exists()) {
        logger.warn("Unable to find root resource:" + webRoot.getName());
    } else {
        logger.info("Root resource is " + webRoot.getName());
    }

    ResourceHandler res = new ResourceHandler();
    res.setBaseResource(webRoot);
    res.setDirAllowed(false);

    //servlet handler
    ServletContextHandler servletCtx = new ServletContextHandler(ServletContextHandler.SESSIONS);
    servletCtx.setContextPath("/service");
    servletCtx.addServlet(new ServletHolder("sample", new MyServletSample()), "/sample");

    ErrorPageErrorHandler errorHandler = new ErrorPageErrorHandler();
    errorHandler.addErrorPage(404, "index.html");
    servletCtx.setErrorHandler(errorHandler);

    // static file handler
    ContextHandler staticCtx = new ContextHandler("/");
    staticCtx.setBaseResource(webRoot);
    staticCtx.setHandler(res);


    // add handlers
    HandlerList handlerList = new HandlerList();
    handlerList.addHandler(servletCtx);
    handlerList.addHandler(staticCtx);

    // add handerList to server
    server.setHandler(handlerList);

This code show me index.html on localhost:8080 and I can access the sample service http://localhost:8080/service/sample. However, I want to show a static error page (i.e. documentation) to show up if an error like "404 Not Found" occured.

With this code, the Error handler logs:

"WARN o.e.j.server.handler.ErrorHandler - No error page found index.html"

. What is correct way/syntax to define the URI?

Thanks in advance!

remouter
  • 73
  • 1
  • 6

1 Answers1

0

This was answered before at https://stackoverflow.com/a/32383973/775715

Don't mix ResourceHandler and ServletContextHandler unless you REALLY know what you are doing, and fully understand the nature of javax.servlet.ServletContext and all of the rules it brings to the table.

See also:

Here's an example of your setup working with NO ResourceHandler, 1 ServletContextHandler, and a DefaultServlet providing the static file serving.

// servlet handler
ServletContextHandler servletCtx = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletCtx.setContextPath("/");
servletCtx.setBaseResource(webRoot); // what static content to serve
servletCtx.setWelcomeFiles(new String[] { "index.html" });
servletCtx.addServlet(new ServletHolder("sample", new MyServletSample()), "/service/sample");

ErrorPageErrorHandler errorHandler = new ErrorPageErrorHandler();
errorHandler.addErrorPage(404, "/index.html");
servletCtx.setErrorHandler(errorHandler);

// static file serving, and context based error handling
ServletHolder defaultServ = new ServletHolder("default", DefaultServlet.class);
defaultServ.setInitParameter("dirAllowed","false");
servletCtx.addServlet(defaultServ,"/");

// add handlers
HandlerList handlerList = new HandlerList();
handlerList.addHandler(servletCtx);
handlerList.addHandler(new DefaultHandler()); // non-context error handling
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136