File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interview question: What's the difference between and Servlet and Filter?

 
Tejas Jain
Ranch Hand
Posts: 119
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is Filter a special servlet? Can I create a web app only use Filter w/o Servlet?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) No.

2) Only if you want to use the wrong tool for the job. Servlets serve one purpose, filters another.
 
Radhakrishna Sharma Gorenta
Ranch Hand
Posts: 63
Google App Engine Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlet is an interface which can be used to generate different types of dynamic response, whereas Filter is a concept for one particular requirement just like authentication
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Filters have multiple uses , they are different from servlets . They can be used fir security , localization, data compression etc.
 
Abhishek Purwar
Ranch Hand
Posts: 63
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Filter is used for filtering the request and perform some action like authenticity of session, user is valid or not for that request, etc.
Servlet is used for performing the action which needs to be taken for particular request like user login, get the response based on user role, interacts with database for getting the data, business logic execution, etc.

Filter has different purpose and servlet has different purpose.
 
Tejas Jain
Ranch Hand
Posts: 119
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know SUN added Filter for different propose from Servlet.

But a Filter can receive HTTP Request from a client and send HTTP Response back to the client.
Technically you can have some processes done in Filter and then send it right back to client w/o going through HTTPServlet or JSP.

I have never tried, but I think it is possible to have a web.xml that only have Filters (chain of filters) defined w/o any Servlet or JSP.
Will web container give any errors because of the web.xml?
 
Paul Clapham
Sheriff
Pie
Posts: 20201
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no requirement for web.xml to have any servlet declarations.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tejas Jain wrote:Technically you can have some processes done in Filter and then send it right back to client w/o going through HTTPServlet or JSP.

Technically, you can also bang in a screw with a hammer, but it's a poor tool for the job.


Use the right tool for the job. Just because you can use the wrong tool to get a job done doesn't mean that you should.
 
Tejas Jain
Ranch Hand
Posts: 119
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A servlet, with some helper classes, can do anything Filters are defined to do. SUN added Filter framework to Servlet in 2.3. I do not feel Filter is very useful.

SUN (including open sources) used to like to create many different APIs to do same simple tasks in different ways. This might be one of the reason of SUNdown.

Bear, I like your picture. I actually saw someone did it. At least, it is quicker with just one bang... Are you from Yellow Stone.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tejas Jain wrote:I do not feel Filter is very useful.

You may feel that way, but I do not. I find servlet filters every very useful and use them often. They help to keep cruft out of the servlets themselves.
 
Amit Ghorpade
Bartender
Posts: 2851
10
Fedora Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tejas Jain wrote:A servlet, with some helper classes, can do anything Filters are defined to do. SUN added Filter framework to Servlet in 2.3. I do not feel Filter is very useful.



Well filters work as wrappers to servlets. So they can intercept both incoming as well as outgoing requests, then we have chaining. How do you think this can be done in servlets? RequestDispatcher is not what I am asking here.
Finally there is something called as "Design Patterns". If your code does not follow/define standards, it does not exist ;).

Hope that helps
 
Paul Clapham
Sheriff
Pie
Posts: 20201
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tejas Jain wrote:A servlet, with some helper classes, can do anything Filters are defined to do.


I would be interested to see how you could use a servlet to do something like, for example, using XSLT to transform the output of another servlet as in this tutorial.
 
Abhishek Purwar
Ranch Hand
Posts: 63
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Tejas: Filter is suitable if one wants to modify/filter request based on some conditions. Servlet is useful when you want to control, preprocess and/or postprocess requests.

Filter is useful for following scenarios :
1)Query the request and act accordingly.
2)Block the request-and-response pair from passing any further.
3)Modify the request headers and data. You do this by providing a customized version of the request.
4)Modify the response headers and data. You do this by providing a customized version of the response.
5)Interact with external resources.

And for more information about filter, check following link : http://www.oracle.com/technetwork/java/filters-137243.html

And one more thing, every component is defined for certain purpose and we cannot mix purpose of two component. In java, servlet is base for everything in JEE and one can do whatever one wants using servlet.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic