• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Filters and Servlets

 
Shyam Kasthala
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me the exact difference b/w servlets and filters.Thankyou.
 
Shyam Kasthala
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am a bit confused so plz can anyone tell me that when should we use a filter and when should we use a servlet.bye.
 
Krishna Latha Grandhi
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friend,

A filter is a Java class that is invoked in response to a request for a resource in a Web Application. Resources include Java Servlets, JavaServer pages (JSP), and static resources such as HTML pages or images. A filter intercepts the request and can examine and m odify the response and request objects or execute other tasks.

Filters are an advanced J2EE feature primarily intended for situations where the developer cannot change the coding of an existing resource and needs to modify the behavior of that resource. Generally, it is more efficient to modify the code to change the behavior of the resource itself rather than using filters to modify the resource. In some situations, using filters can add unnecessary complexity to an application and degrade performance.

Regards,
Hari Krishna.G.
 
Shyam Kasthala
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thank you very much.That's very informative.
 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! So there really is no need for me to bother with filters...
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello "kasthala"-

Welcome to JavaRanch.

On your way in you may have missed that JavaRanch has a policy on display names, and yours does not comply with it; specifically, a first name and a last name are required. Please adjust it accordingly, which you can do right here. Thanks for your prompt attention to this matter.
 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Filters are not always used. Certain common repeated tasks are best done outside a core resource like a Servlet/JSP. The filters can be used for caching and compressing content, logging and auditing, image conversions (scaling up or down etc), authenticating incoming requests, XSL transformation of XML content, localization of the request and the response, site hit count etc.


A filter dynamically intercepts requests and responses to transform or use the information contained in the requests or responses but typically do not themselves create responses. Filters can also be used to transform the response from the Servlet or JSP before sending it back to client. Filters improve reusability by placing recurring tasks in the filter as a reusable unit.

A good way to think of Servlet filters is as a chain of steps that a request and response must go through before reaching a Servlet, JSP, or static resource such as an HTML page in a Web application.


Design Pattern: Servlet filters use the slightly modified version of the chain of responsibility design pattern. Unlike the classic (only one object in the chain handle the request) chain of responsibility where filters allow multiple objects (filters) in a chain to handle the request.

-- Excerpts from Java/J2EE Job Interview Companion
 
Kartik Lax
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A good example for the use of a filter would be one on j_security_check (a container-managed security specific class) in which you can't tamper with the source or catch any exceptions.
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tom Cramer:
A good example for the use of a filter would be one on j_security_check (a container-managed security specific class) in which you can't tamper with the source or catch any exceptions.


i ddin't get u :-(
tampering the code mean?
are u abt to say decompiling hte code
how can we do it man
the class files are in private folder
i think we cannot access htme
 
Shyam Kasthala
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
U said "A filter intercepts the request and can examine and m odify the response and request objects or execute other tasks".When a request comes in,normally it goes to a servlet then how does the filter intercept and through which it should intercept.Does the request goes to the filter and then goes to the servlet or vice versa?
 
Kartik Lax
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The web container actually calls the servlet filter when the servlet is referenced. It checks the mapping from your web.xml

Have a look at this skeleton code, this should help you.

class Filter implements javax.servlet.Filter
{

init(){
//the container will automatically call init() and then doFilter()
//and finally destroy
}

destroy(){
}

doFilter(FilterChain chain, request,response)
{
//this is the method where you will have your code


//request will be available here, you can read from or write into it

/*chain.doFilter = the action that you requested in the servlet
Say you submitted with action POST for LoginServlet in your form
chain.doFilter = LoginServlet.doPost (if you comment this line you
can see that LoginServlet will not be called)*/

chain.doFilter

//do any post-processing
}

}
 
Shyam Kasthala
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks Tom That's very well said.That's very informative.Bye.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic