Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filters vs interceptors in web application

Tags:

java

I could not get the correct difference between filters and interceptors. So Please explain me the exact use of filters and interceptors in java based web application with some sample snippet.

like image 948
pradeep Avatar asked Feb 28 '16 18:02

pradeep


People also ask

What is interceptor in web?

An HTTP interceptor intercepts all received HTTP requests and outbound HTTP responses. Integration Server makes the raw HTTP request and response, including the HTTP header information, accessible to the HTTP interceptor.

Which one is called first filter or interceptor?

Filter is related to the Servlet API and HandlerIntercepter is a Spring specific concept. Interceptors will only execute after Filters. Fine-grained pre-processing tasks are suitable for HandlerInterceptors (authorization checks, etc.)

What is the use of filter security interceptor?

Performs security handling of HTTP resources via a filter implementation.

Which interceptor is used in an application?

Spring Interceptor are used to intercept client requests and process them. Sometimes we want to intercept the HTTP Request and do some processing before handing it over to the controller handler methods.


1 Answers

Filters are used in Web Applications to perform some actions on the request or the response, before it reaches or after it leaves the actual action handler on the sever ( which might be a Servlet, a REST service, a JSF Managed Bean, etc.). By using filters you can for example check to see if certain requests are authorised for the logged in user, and you can actually cancel the request and return a response to the client, without allowing the request to reach the actual action handler.

If you have more than one filter you will have to chain them.

Interceptors are acting on class methods. It allows you to do some additional processing while invoking a method of an object, without the need to alter the method body. This can be very useful when:

  • you do not have access to the method's body
  • the processing is something that is repeated for a specific type of methods and you do not want to put that extra code everywhere (for example logging the input parameters and the output result in order to track the execution or check the security constraints on a specific method, if you have defined some).
like image 176
iullianr Avatar answered Oct 27 '22 19:10

iullianr