Filter in servlet web application

In this tutorial you will learn about the java servlet filter technology, whenever you make a request and you want to verify that request or you want to do some stuff with request before it reached to actual servlet, you can use the filter for those requirements.

Filter is a server side programme similar to servlet, and you can attach filter with any web resources like servlet, jsp etc. whenever any URL is requested by browser, and if any filter attached with that URL, first filter will come into picture.

We can assign filters to any web resources either active or passive.

We can assign one filter for more than one web resources, as well we write more than one filter of one web resource.

Filter consists two part, one part is execute when request going to web resources like servlet, jsp etc, and second part of filter will be executing while web resource send a response back to browser.

You can develop a filter using servlet API jar. From servlet 2.3 filters are available in servlet API, all filter related calluses and interfaces are into javax.servlet package.

There are mainly three interface by using you can develop filter.

Filter
FilterChain
FilterConfig

You can develop the filter as you can develop servlet, the steps are similar to develop servlet, these steps also requested for develop filter.

In the next tutorial we will learn to develop in filter and assign it to one servlet.