Creating own Interceptor

Though Interceptors are provided by default in the stack, one can also create custom interceptors and add it to already existing list of interceptors. It is very easy to create your own interceptor. Custom made Interceptors provide cross-cutting application features.

Creating own Interceptor


Creating own Interceptor

Though Interceptors are provided by default in the stack, one can also create custom interceptors and add it to already existing list of interceptors. It is very easy to create your own interceptor.

Custom made Interceptors provide cross-cutting application features. Custom interceptors can be customized according to the need. Interceptors can be executed before and after an action is executed.

Following Interceptor interface needs to be extended:

public interface Interceptor extends Serializable{
void destroy();
void init();
String intercept(ActionInvocation invocation)
throws Exception;
}

Interceptor's init() method is called while initializing the interceptor

Interceptor's destroy() method is called during the destruction of interceptor

Code is placed in intercept (ActionInvocation). If init() and destroy() methods are not needed then AbstractInterceptor class can be extended.

To invoke Interceptor after an action is executed, place the code after invocation.invoke (). (invocation.invoke() is responsible for executing the next interceptor in the stack or action)