问题
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,
- I've removed the DefaultHandler from my Jetty XML file,
- I've edited my web.xml to include both Servlet 2.5 and Servlet 3.0 error handler locations pointing to /error,
- I've set up a dedicated @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
- to configure a custom Jetty error handler (I don't want to do this, I want to do it inside my own Spring
@Controller
as mentioned above) - to create a "catch all context and create a "root" web app mapped to the
/
URI." (I don't want to do this as inside myweb.xml
I have already mapped Spring MVC'sDispatcherServlet
to /.
How can I turn off Jetty's default error handler and have the error handling be done as pointed out above?
回答1:
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 http://www.eclipse.org/jetty/documentation/current/custom-error-pages.html, to register a custom ErrorHandler
, you can follow the following steps.
- Implement some
org.eclipse.jetty.server.handler.ErrorHandler
subclass, e.g.com.example.CustomErrorHandler
. - Make this subclass available to your Eclipse server instance, e.g. by bundling
CustomErrorHandler
in ajar
file and then copying thatjar
file into the${jetty.base}/lib/ext
directory. - Configure your Jetty server instance to have this custom
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>
回答2:
Here's how to define custom error pages -
http://wiki.eclipse.org/Jetty/Howto/Custom_Error_Pages
来源:https://stackoverflow.com/questions/17202839/deactivate-jettys-default-404-error-handler