Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why write a servlet filter for a DispatcherType other than REQUEST?

I've seen plenty of servlet filters in my time for doing all kinds of things but always on REQUEST never anything else. Does anyone have any use cases or examples of why it might be useful to write a servlet filter for another dispatcherType, other than REQUEST?

The DispatcherTypes as in javax.servlet package.

public enum DispatcherType {
  FORWARD, INCLUDE, REQUEST, ASYNC, ERROR
}
like image 935
crabe Avatar asked Oct 15 '25 17:10

crabe


1 Answers

Well the names are rather self-explaining. The REQUEST is indeed the most often used one and it's applied on the incoming request chain. Here is when you may need to use the others (because the REQUEST only dispatched filters will not be applied):

  • FORWARD - if you want to filter request/response when one servlet forwards the request to another servlet. Often this is used when a servlet forwards to a JSP page.
  • INCLUDE - if you want to filter request/response when one servlet calls another servlet in order to include it's response in the own response. Often this is used when a JSP page includes JSP page.
  • ASYNC - This is something I personally never used but AFAIK it's needed to be able to filter the asynchronous requests introduced in Servlet 3 spec
  • ERROR - if you want to filter request/response when the servlet call results in error.

See https://sling.apache.org/documentation/the-sling-engine/filters.html#filter-chains for some more details.

like image 57
Milen Dyankov Avatar answered Oct 17 '25 06:10

Milen Dyankov