• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Filter vs Servlet

 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Need some more inputs please. :roll:
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adeel,

I'm still wondering. Where are you going with this?
What is it that you're trying to do?
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic