java - Make servlet filter for all 404 responses -
i want make custom filter extends javax.filter, when 404 status of response return static html page. not work , wondering why. here filter:
@webfilter("/*") public class pagenotfoundfilter implements filter { @override public void dofilter(final servletrequest request, final servletresponse response, final filterchain filterchain) throws ioexception, servletexception { httpservletresponse httpresponse = (httpservletresponse) response; if (httpresponse.getstatus() == 404) { httpresponse.sendredirect("pages/pagenotfound.html"); } filterchain.dofilter(request, response); }
but nothing happens, why?
note: not want use:
<error-page> <error-code>404</error-code> <location>/pages/pagenotfound.html</location> </error-page>
but filter
you test status code before request processed (and before 404 error set).
so need test status code after have called filterchain.dofilter(request, response);
but not work either because when response.senderror(404)
called response committed , can't redirect.
therefore best use <error-page>
mechanism.
Comments
Post a Comment