views:

63

answers:

3

I read everytime Filters are like servlets. Resin is a servlet container. Is there exist any filter container to initialize filters or does it take the role in servlet container only. Can anybody elaborate how it works?

+4  A: 
  • Servlets are components that handle an HTTP request/response cycle
  • Filters are components that intercept HTTP requests and response before/after they reach the target servlet
  • Servlet containers make sure the two points above work - i.e. they instantiate servlets and filters and "give" them the requests/responses
Bozho
+1, Concise yet covers all the points clearly.
Andrzej Doyle
And Filters are *part* of Servlet API. Note the package name http://java.sun.com/javaee/6/docs/api/javax/servlet/Filter.html
BalusC
A: 

Java Servlet Filters allow you to "layer on" additional behavior in front of a Servlet, JSP page, or even static resources like css, js, and image files.

The classic example of Filter use is Authorization: checking to ensure the user is authorized to view the specified resource.

Some observations:

  • A Filter can do work before the specified resource (servlet, etc.), after, or both
  • Multiple Filters may be applied to the same resource
Drew Wills
+1  A: 

There is nothing like Filter container, it is included in servlet container.

fastcodejava