aspose file tools*
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "Filter vs Servlet" Watch "Filter vs Servlet" New topic
Author

Filter vs Servlet

Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Hey Folks,

Just would like to know that if we throw a servlet into trash bin and do everything with the filter. Because a Filter can do anything a Servlet can.

Suppose we used a Filter rather than a Servlet, though a Servlet would have been fine enough for that task. Means we really didn't need to use the Filter at all, but we did with that.

Would like to know the performance difference and some other issues, if there are some. I think it doesn't affect performance really or may be neglegible.

Couldn't we throw a servlet into trash bin, and use Filter instead?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I did read somewhere, (might have been in "More Servlets and Java Server Pages") that filters would make a good replacement for servlets in an MVC architecture. The servlet in this case is behaving more like a filter anyway, since it's only intercepting requests and delegating all the work to other resources.

Other than that, I can't see a good reason for throwing out servlets altogether.

Use a hammer for pounding in nails, and a wrench for turning bolts.
[ January 03, 2005: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
Use a hammer for pounding in nails, and a wrench for turning bolts.
Nicely put Ben. If you are just using the servlet to render a view for a jsp, then go ahead and use a filter instead if that fits your fancy. But to say that a Filter can do anything a Servlet can is a bit misguided, and probably incorrect.

That falls into the same arguement as a JSP can do anything a servlet can. It may be possible, but using a JSP to preform logic, database functions, etc, is considered bad practice. Not to mention what a nightmare it would be for anyone that needed to maintain your code later on.


“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Paul Bourdeaux:
It may be possible, but using a JSP to preform logic, database functions, etc, is considered bad practice. Not to mention what a nightmare it would be for anyone that needed to maintain your code later on.


I think these things don't suit servlet as well.

And there is a big difference between jsp and servlet. JSP is tag like language, so a web developer, dont know java at all, can author a jsp. And using servlet for presentation is really something not encouraged and a bit complex to do. And no design pattern address this.

But you know if we just use filter in place of servlet, it would be much likely. Although JSP and Servlet differs from eachother more than a Filter and Servlet. Servlet and Filter both qualify to be a Controller.Using Filter in place of Servlet doesn't affect your design pattern at all.

Using servlet where Filter is more suitable might become complex, but using Filter where servlet is more suitable doesn't make a huge difference.

I am not gonna do that, just asking because couldn't think of a real harm. And even after getting your kind replys couldn't make myself satisfied. Need some more inputs.

If hammer then both, Filter and Servlet, are hammers and if wrench then both are wrench. But one can do somemore than other.

thanks.
[ January 04, 2005: Message edited by: Adeel Ansari ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Again, as a controller in an MVC framework, a filter may be a suitable replacement for a servlet. I certainly wouldn't want to stream binary files or do printWriter operations from one though.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Ben Souther:
Again, as a controller in an MVC framework, a filter may be a suitable replacement for a servlet. I certainly wouldn't want to stream binary files or do printWriter operations from one though.


Couldn't get you Ben. Did i say something like that
thanks
[ January 04, 2005: Message edited by: Adeel Ansari ]
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Need some more inputs please. :roll:
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Ben Souther:
Again, as a controller in an MVC framework, a filter may be a suitable replacement for a servlet. I certainly wouldn't want to stream binary files or do printWriter operations from one though.


Ok. But we can do that, right??

If anybody knows the real harm please let me know. However, I am not gonna replace all my servlets with filters.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You've got the reqeust and response objects so I don't see why not.
You could always test this. Try it.

The harm, as I see it, would be obsfucated code. When I see a class that implements filter I assume that I'm dealing with an object that intercepts either the incoming request or the outgoing response. Again, as an MVC controller, that might make sense. Otherwise, I can't see any sense in doing so.

Out of curiosity, what is the point here? You've already stated, more than once, that you're not going to do this.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Ben Souther:
You've got the reqeust and response objects so I don't see why not.
You could always test this. Try it.


Yes I agreed that we can do it as you said. But i meant something else. I am saying that we can do that with a filter as well.

As you said, "When I see a class that implements filter I assume that I'm dealing with an object that intercepts either the incoming request or the outgoing response". Yes Agreed with this too, its a common assumption. Hence, using Filters without any need, where a Servlet can do good, may steer the code reader in wrong direction.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Adeel,

I'm still wondering. Where are you going with this?
What is it that you're trying to do?
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Ben,

Dont worry mate. I am not going to do anything like sin. I was just thinking if we just leave servlet and do all with filter.

I just made all the servlet example of a book with filter. And those are executing fine. so, it was just a thought to use filter everywhere. I was a bit curious about the real harm.

Infact it is written in that book that you can replace all the servlet examples with filter if you want to. . So, I just tried to get the real difference.

thanks.
 
wood burning stoves
 
subject: Filter vs Servlet