1

Can we use Filter instead of Servlet? Can filter (without using servlet) be able to serve incoming request?

Juns
  • 511
  • 1
  • 5
  • 10

2 Answers2

1

Filters are used to modify the header and/or content of a request or response. I have never seen them used to create the response itself and I don't think they can be used for that, since they need to be "chained" with an actual resource. From the Java EE 6 tutorial:

A filter is an object that can transform the header and content (or both) of a request or response. Filters differ from web components in that filters usually do not themselves create a response. Instead, a filter provides functionality that can be “attached” to any kind of web resource.

David Levesque
  • 22,181
  • 8
  • 67
  • 82
1

Filters have a wide array of uses; the Servlet 2.3 specification suggests the following uses:

  • authentication filters
  • logging and auditing filters
  • image conversion filters
  • data compression filters
  • encryption filters
  • tokenizing filters
  • filters that trigger resource access events
  • XSL/T filters that transform XML content
  • MIME-type chain filters

servlet filter

Use a Filter when you want to filter and/or modify requests based on specific conditions.
Use a Servlet when you want to control, preprocess and/or postprocess requests.

Filter are best suited for authorization, because it can be configured to run for all pages of a site. So you only need one filter to protect all your pages.

Useful Links:

  1. filter tutorial
  2. filter in detail
  3. referred answer
Community
  • 1
  • 1
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90