4

Say I have a J2EE filter. Say I want to do something in this filter, but conditionally, and depending on the servlet the request will end up hitting.

for example, I want a single filter that will prevent any non GET/POST request methods, but I have a few servlets that can deal with OPTIONS request method. So I if I knew what the target servlet was, and checked that it had some marker (i.e. interface or annotation) so that the filter can pass the OPTIONS request through (and otherwise return 405).

So far, the best I came up with is: request.getServletContext().getServletRegistration(request.getServletPath().substring(1)).getClassName() (with proper NULL checks) gives me the class name of the servlet that the request will hit.

Pawel Veselov
  • 3,996
  • 7
  • 44
  • 62
  • This will help you http://stackoverflow.com/questions/3125296/can-i-exclude-some-concrete-urls-from-url-pattern-inside-filter-mapping – sandeep verma Apr 03 '15 at 06:34
  • @sandeepverma no, sorry, it doesn't. – Pawel Veselov Apr 03 '15 at 08:22
  • 2
    I don't believe there is any Servlet API that is going to help you accomplish this goal. The problem is that from the Servlet container perspective it may not know the servlet target until after ALL the filters have run, as any filter in the filter chain could do something to change the request. – jbergmark Apr 04 '15 at 19:11
  • @jbergmark well, I really wanna ask just that, given the request, and web.xml as it is, what servlet would take this request? Not accounting for any runtime forwarding filters may do... – Pawel Veselov Apr 06 '15 at 20:43

0 Answers0