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 Filters and Servlets 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 "Filters and Servlets" Watch "Filters and Servlets" New topic
Author

Filters and Servlets

Shyam Kasthala
Ranch Hand

Joined: Mar 30, 2006
Posts: 48
Can anyone tell me the exact difference b/w servlets and filters.Thankyou.


Thanks & Regards<br /> Shyam Kasthala<br />ILFS Infotech Limited
Shyam Kasthala
Ranch Hand

Joined: Mar 30, 2006
Posts: 48
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

Joined: Nov 08, 2005
Posts: 110
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

Joined: Mar 30, 2006
Posts: 48
Hi,
Thank you very much.That's very informative.
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Wow! So there really is no need for me to bother with filters...


SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41579
    
  54
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.


Ping & DNS - my free Android networking tools app
ak pillai
author
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
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


java j2ee job interview questions with answers | Learn the core concepts and the key areas
Kartik Lax
Greenhorn

Joined: Apr 23, 2006
Posts: 24
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

Joined: Apr 16, 2005
Posts: 689
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


A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Shyam Kasthala
Ranch Hand

Joined: Mar 30, 2006
Posts: 48
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

Joined: Apr 23, 2006
Posts: 24
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

Joined: Mar 30, 2006
Posts: 48
Hi,
Thanks Tom That's very well said.That's very informative.Bye.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Filters and Servlets