I want to provide a custom 404 error page in my Spring 3.1 web application, but I cannot deactivate Jetty 8's default 404 error page.
Jetty 8, out of the box, provides a default 404 error page:
when visiting a Jetty-hosted website, and providing a URL path that is not handled by any servlet (e.g. by visiting http://www.example.com/nonexisting
), Jetty responses with its own default HTML error page:
HTTP ERROR 404
Problem accessing /nonexisting. Reason:
Not Found
Powered by Jetty://
To replace this default behavior,
DefaultHandler
from my Jetty XML file,web.xml
to include both Servlet 2.5 and Servlet 3.0 error handler locations pointing to /error
,@Controller
for handling the request to /error
,but my website still returns Jetty's own default HTML error page.
Jetty 8's official documentation talks about setting up a "custom error pages", but the suggestions there say
@Controller
as mentioned above)/
URI." (I don't want to do this as inside my web.xml
I have already mapped Spring MVC's DispatcherServlet
to /. How can I turn off Jetty's default error handler and have the error handling be done as pointed out above?
The solution to my problem was to add a custom org.eclipse.jetty.server.handler.ErrorHandler
.
If a user doesn't explicitly specify some ErrorHandler
, the Jetty server instance seems to register a default ErrorHandler
.
As outlined on https://www.eclipse.org/jetty/documentation/jetty-9/index.html#custom-error-pages, to register a custom ErrorHandler
, you can follow the following steps.
org.eclipse.jetty.server.handler.ErrorHandler
subclass, e.g. com.example.CustomErrorHandler
.CustomErrorHandler
in a jar
file and then copying that jar
file into the ${jetty.base}/lib/ext
directory.ErrorHandler
registered as a bean:file jetty.xml
:
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN"
"http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<!-- more configuration -->
<Call name="addBean">
<Arg>
<New class="com.example.CustomErrorHandler">
<Set name="server"><Ref refid="Server" /></Set>
</New>
</Arg>
</Call>
</Configure>