Archive for the ‘struts’ Category

Filter Vs Interceptor

Posted: June 8, 2012 in struts

Filter: 

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.

Filters can perform many different types of functions. We’ll discuss examples of the italicized items in this paper:

  •   Authentication-Blocking requests based on user identity.
  •   Logging and auditing-Tracking users of a web application.
  •   Image conversion-Scaling maps, and so on.
  •   Data compression-Making downloads smaller.
  •   Localization-Targeting the request and response to a particular locale.

Request Filters can:

  •    perform security checks
  •   reformat request headers or bodies
  •   audit or log requests

Response Filters can:

  •   Compress the response stream
  •   append or alter the response stream
  •   create a different response altogether

Examples that have been identified for this design are

  •   Authentication Filters
  •   Logging and Auditing Filters
  •   Image conversion Filters
  •   Data compression Filters
  •   Encryption Filters
  •   Tokenizing Filters
  •   Filters that trigger resource access events
  •   XSL/T filters
  •   Mime-type chain Filter

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 can be defined within a target class as an interceptor method, or in an associated class called an interceptor class. Interceptor classes contain methods that are invoked in conjunction with the methods or lifecycle events of the target class.

1)  Cookie Interceptor

2)  Checkbox Interceptor

3)  FileUpload Interceptor

Difference:

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. That’s the main difference.

for authentication of web pages you would use a servlet filter. For security stuff in your business layer or logging/bugtracing (a.k.a. independent of the web layer) you would use an Interceptor.

Apart from the fact that both Interceptors and filters are based on intercepting filter,there are few differences when it comes to Struts2.

Filters: (1)Based on Servlet Specification (2)Executes on the pattern matches on the request.(3) Not configurable method calls
Interceptors: (1)Based on Struts2. (2)Executes for all the request qualifies for a front controller( A Servlet filter ).And can be configured to execute additional interceptor for a particular action execution.(3)Methods in the Interceptors can be configured whether to execute or not by means of excludemethods or includeMethods

 

Advertisements