top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the difference between interceptor and filters in context of Java?

+4 votes
3,801 views
What is the difference between interceptor and filters in context of Java?
posted Feb 5, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

DEFINITION FOR FILTERS:

A filter dynamically intercepts requests and responses to transform or use the information contained in the requests or responses.

Filters typically do not themselves create responses, but instead provide universal functions that can be "attached" to any type of servlet or JSP page.

But the main difference is that, Filters are applied to the all the requests of the web application,

DEFINITION FOR INTERCEPTORS:

Interceptors are used in conjunction with Java EE managed classes to allow developers to invoke interceptor methods in conjunction with method invocations or lifecycle events on an associated target class.

Common uses of interceptors are logging, auditing, or profiling.

Interceptors are used to handle the pre and post processing of incoming request.

But, interceptors are applied to certain group of handlers. If you want to forward the requests to some particular module, Interceptor is the best place to write your code to divert the request to a particular module.

Filter used web but interceptor can be used with web as well as enterprise applications

EXAMPLE:

A Servlet Filter is used in the web layer only, you can't use it outside of a web context.
Interceptors can be used anywhere.

Filters only apply to the urls for which they are defined.
The interceptor stack fires on every request.

answer Feb 11, 2015 by Karthick.c
0 votes

Filter is a class which use to check server side validation means if you are requesting any thing from the server the fillter will check your request on your server if requested data valid then filter will put on the server otherwise it will give error

Interceptor is very important part of the struts framework which process the request step by step on the controller there are many interceptor working in the form of queue.

answer Aug 13, 2015 by Sandeep Otari
...