I've been writing servlets and filters for years and it is something that has always irked me a little - the checking and casting of ServletRequest
and ServletResponse
parameters to HttpServletRequest
and HttpServletResponse
, particularly with filters as there is no standard HttpFilter
base class to do this for you - unlike servlets.
The question is - when would you ever see anything other that HttpServletRequest
/HttpServletResponse
being passed to your servlet/filter and what should you do if you do?
The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. The FilterChain passed in to this method allows the Filter to pass on the request and response to the next entity in the chain.
Java Servlet Filter is used to intercept the client request and do some pre-processing. It can also intercept the response and do post-processing before sending to the client in web application.
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. The servlet filter is pluggable, i.e. its entry is defined in the web.
In some yet-to-be-realized world where there are other servlet types besides HttpServlets.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With