Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a filter to detect if the user requested a page that is not found?

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.

like image 211
Mahmoud Saleh Avatar asked Dec 01 '10 12:12

Mahmoud Saleh


People also ask

What is filter can filter be used as request or response?

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.

What is filter request?

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.


2 Answers

If you don't have authentication, you can.

  1. Make a Filter
  2. Use HttpServletResponseWrapper and override the sendError() and setStatus()
  3. Pass the wrapped response through chain.doFilter(req, wrapper)
  4. If you get a sendError() in your wrapper, see if it's a 404.
  5. Take appropriate response.

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.

like image 105
Martin Algesten Avatar answered Sep 30 '22 18:09

Martin Algesten


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.

like image 42
jmj Avatar answered Sep 30 '22 18:09

jmj