Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between a valve and a filter

Can someone explain the difference between a servlet filter and tomcat valve. What is the advantage of using one over the other?

like image 538
Sivakumar Avatar asked Apr 04 '11 10:04

Sivakumar


People also ask

What is a filter valve?

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.

What is Tomcat valve?

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.

What is a tomcat filter?

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.


1 Answers

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.

like image 80
Tomasz Nurkiewicz Avatar answered Sep 24 '22 15:09

Tomasz Nurkiewicz