1

Possible Duplicate:
servlet vs filter

I am java beginner. Now I am learning about filters. What is the difference between servlet & filter. I have read about filter,

a filter is an object that can transform a request or modify a response. Filters are not servlets; they don't actually create a response

I am not able to understand that. Can any one explain it to me.

Community
  • 1
  • 1

1 Answers1

4

Example of filter is GZIP Filter that compresses response. Some servlet generates output, and gzip filter just compresses it. Filters may be mapped to process output from multiple servlets, or just for any path, independently of how exactly servlets are mapped.

Another examples of filters:

  • filter to remove whitespace from output
  • filter to set caching headers
  • filter to check if user can actually access given URL

In each case, filter has opportunity to alter request or response, but typically doesn't generate response on its own.

Peter Štibraný
  • 32,463
  • 16
  • 90
  • 116