Can someone explain the difference between a servlet filter and tomcat valve. What is the advantage of using one over the other?
The bypass valve – otherwise known as a pressure relief valve – is an integral part of the oil filter. The valve is designed to open when the oil filter becomes clogged or when the oil is too thick. This allows the oil to bypass the filter through a center tube.
A Tomcat valve - a new technology introduced with Tomcat 4 which allows you to associate an instance of a Java class with a particular Catalina container. This configuration allows the named class to act as a preprocessor of each request. These classes are called valves, and they must implement the org. apache.
A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both. Filters perform filtering in the doFilter method.
From Tomcat documentation:
A Valve element represents a component that will be inserted into the request processing pipeline for the associated Catalina container
The servlet filters serve the exact same purpose, but valves are Tomcat's specific classes, tightly coupled to Tomcat infrastructure/API. On the other hand servlet API is implemented by all compatible web containers. That said, valves won't work with e.g. Jetty, which has different API for that, while servlet filters will.
The other important difference is that valves are working on container level (intercepting all applications/requests), while servlet filters are intercepting all request only to a given application. So if you want to implement server-wide interceptor, valves are necessary.
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