I would like to run a search whenever a 404 is encountered. For example, a user would like to view restaurants with Italian cuisine, they would go to http://www.mywebsite.com/italian so instead of showing a custom 404 error page, I would like add a filter which first checks if a 404 would have been thrown, if so, attempt to search against a list of search items, if a match exists, forward request to that page rather than throw 404 error.
Example:
www.mywebsite.com/italian --> www.mywebsite.com/search.do?cuisine=italian
www.mywebsite.com/london --> www.mywebsite.com/search.do?city=london
www.mywebsite.com/rest --> www.mywebsite.com/show.do?rest_id=XXX
The question I guess is how would I determine if there doesn’t exists a servlet with url-mapping "/italian" or "/italian/*" within my server config?
Using (in Filter.doFilter) request.getRequestDispatcher(request.getRequestURI()) always returns a non-null value which I cannot use to determine whether the request is valid or not. I can use a 404 error statement but the result would have a response status of 404 rather than 200.
You can just map the 404 error page on a servlet URL pattern.
This allows for doing fine grained business and HTTP request/response controlling job in the servlet before displaying the desired JSP.