5

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.

riddle_me_this
  • 8,575
  • 10
  • 55
  • 80
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498

3 Answers3

7

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.

Martin Algesten
  • 13,052
  • 4
  • 54
  • 77
  • what do you think of Bozho's solution, i guess it's pretty good. – Mahmoud Saleh Dec 01 '10 at 12:53
  • I guess the `HttpURLConnection` would work something like incoming request for `http://foo/some/stuff` - gets intercepted by filter, creates a HttpURLRequest for `http://foo/some/stuff?disableFilter=true` - filter lets this one through. It depends on situation - this solution would create quite a few extra sockets, so depending on load may be not great (HttpURLConnection doesn't do keep-alive, so you would end up with many sockets in CLOSED_WAIT if you have hight load). – Martin Algesten Dec 01 '10 at 12:58
  • @sword, @bozho: Using `HttpURLConnection` would only result in an infinite loop in the filter, because the *same* filter would be invoked again... You would need to add some paramter to the request URL and let the filter intercept on that to avoid calling the `HttpURLConnection` again. – BalusC Dec 01 '10 at 16:20
  • @BalusC look at my reply... notice `?disableFilter=true` – Martin Algesten Dec 01 '10 at 16:28
4

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.

jmj
  • 237,923
  • 42
  • 401
  • 438
  • i already know this simple solution, i guess the question was clear, i have to make such solution with filter. – Mahmoud Saleh Dec 01 '10 at 12:21
  • But, *why* would you like to use a Filter for that? – BalusC Dec 01 '10 at 12:28
  • 1
    issue is here: http://stackoverflow.com/questions/4153438/spring-security-issue-with-404-error and i cannot use the solution of making two different patterns one for secure, and other for anonymous, so i guess a filter maybe useful here, what do you think ? – Mahmoud Saleh Dec 01 '10 at 12:32
  • 1
    So do you have authentication involved or not (that would complicate things)? – Martin Algesten Dec 01 '10 at 12:37
2

"Page exists" is not something trivial. Pages need not to exist physically as files.

Apart from the option mentioned by org.life.java to simulate a request using HttpURLConnection, you can create a HttpServletResponseWrapper, override the setStatus method, and whenever it is set to 404, take extra measures.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140