aspose file tools*
The moose likes Servlets and the fly likes Interview question: What's the difference between and Servlet and Filter? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Interview question: What Watch "Interview question: What New topic
Author

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

Tejas Jain
Ranch Hand

Joined: Mar 04, 2008
Posts: 119
Is Filter a special servlet? Can I create a web app only use Filter w/o Servlet?


"Knowing is not enough, you must apply... Willing is not enough, you must do."
--Bruce Lee
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61589
    
  67

1) No.

2) Only if you want to use the wrong tool for the job. Servlets serve one purpose, filters another.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
rk sharma
Ranch Hand

Joined: Jun 25, 2011
Posts: 50

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


Regards,

RK
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Filters have multiple uses , they are different from servlets . They can be used fir security , localization, data compression etc.


Learning and Learning!-- Java all the way!
Abhishek Purwar
Ranch Hand

Joined: Dec 15, 2007
Posts: 63

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.


Abhishek Purwar,
Bangalore.
Tejas Jain
Ranch Hand

Joined: Mar 04, 2008
Posts: 119
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
Bartender

Joined: Oct 14, 2005
Posts: 18860
    
    8

There's no requirement for web.xml to have any servlet declarations.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61589
    
  67

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

Joined: Mar 04, 2008
Posts: 119
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

Joined: Jan 10, 2002
Posts: 61589
    
  67

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

Joined: Jun 06, 2007
Posts: 2716
    
    6

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


SCJP, SCWCD.
|Asking Good Questions|
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18860
    
    8

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

Joined: Dec 15, 2007
Posts: 63

@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.
 
Consider Paul's rocket mass heater.
 
subject: Interview question: What's the difference between and Servlet and Filter?