I want to create a filter in my app such that before each request, it detects whether the requested page exists. If it doesn't exist, it will forward the user to an error page.
How do I detect that the page exists?
I need a solution with a filter and not using the web.xml tag method.
A filter is an object that is invoked at the preprocessing and postprocessing of a request. It is mainly used to perform filtering tasks such as conversion, logging, compression, encryption and decryption, input validation etc.
A filter is an object that can transform the header and content (or both) of a request or response. Filters differ from web components in that filters usually do not themselves create a response.
If you don't have authentication, you can.
Filter
sendError()
and setStatus()
chain.doFilter(req, wrapper)
sendError()
in your wrapper, see if it's a 404.You may also have to override getOutputStream()
and getWriter()
to avoid the response to be flushed to the client before you get a chance to do stuff.
You can directly configure it in web.xml
<error-page>
<error-code>404</error-code>
<location>/yourCustom404.jsp</location>
</error-page>
Or Create a filter and Use HTTPURLConnection programatically detect the page exist or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With