This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes Filter vs Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Filter vs Servlet" Watch "Filter vs Servlet" New topic
Author

Filter vs Servlet

Bob CHOI
Ranch Hand

Joined: Nov 10, 2006
Posts: 127
Is the observation valid that Filter is not only an enhancement to Servlet/GenericServlet/HttpServlet, but also funtionally effecient replacement in most cases?


Hard work rewards
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Replacement is a very tough word, but indeed, common functionality across servlets can often be factored into a reusable Filter. The Cocoon framework is almost exactly that - effectively a 'filter based' request-response cycle, which works very well.

You have come to a significant level of enlightenment.

-Cameron McKenzie
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Heck, why bother with a screwdriver when you can bang a screw in with a hammer if you hit it hard enough?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bob CHOI
Ranch Hand

Joined: Nov 10, 2006
Posts: 127
Thanks, Cameron!

Another extensive view on below is that Filters have generally advantages over RequestDispatcher aided Servlets.

- implements interface vs extends abstract class
- XML configuration vs hardcoded RequestDispatcher

Filters:



Servlets:



[ January 02, 2007: Message edited by: Bob CHOI ]
[ January 02, 2007: Message edited by: Bob CHOI ]
Bob CHOI
Ranch Hand

Joined: Nov 10, 2006
Posts: 127
Thanks, Bear!

The oberservation can't be justified without the context - The screwdriver that is better for you is the one suitable for you.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The methods of your servlets and filters will be called at different points along the request/response cycle.

Filters were not meant to be a replacement for servlets.
They allow you to apply pre and post servlet processing.

As Bear suggested, learn about all the tools available to you and learn how to apply the correct one to the task at hand.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Originally posted by Bob CHOI:
The screwdriver that is better for you is the one suitable for you.


You miss my point. A hammer is never "the screwdriver that is better for you".

There are technical reasons that a filter is not a suitable replacement for a servlet, but even brushing those aside, using a tool incorrectly, even if it can adequately do the job, obfuscates your design and code. Others will not be expecting you to misuse the tool in this manner and all you will do is create confusion and code that deviates from the conventional.

Using unconventional and obfuscating methods "just because I can" is not the sign of a mature developer.
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
Bear's point that replacing all servlets with filters would be to "obfuscate your design and code" is an important one. Another way to highlight the difference between servlets and filters is to consider the types of problems that each is designed to address.

My understanding of filters suggests that they are most powerful when used to modularly address cross-cutting concerns that would otherwise have to be coded in multiple places (ie servlets). But without any servlets, there would be no cross-cutting concerns.

For example, logging & security are both cross-cutting concerns that will apply throughout your application. Filters are one option for addressing those concerns in a single (code) location, rather than writing security and logging code into each servlet you write.


Fun programming etcetera!
 
wood burning stoves
 
subject: Filter vs Servlet