jsr340-experts@servlet-spec.java.net

[jsr340-experts] Support of Interceptor in Servlet

From: Shing Wai Chan <shing.wai.chan_at_oracle.com>
Date: Mon, 06 Feb 2012 16:51:21 -0800

Interceptors are defined by the Interceptors 1.1 specification, which
was originally defined by EJB and was split off to allow more general use.
"An interceptor method may be defined on a target class itself or on an
interceptor class associated with the target class. An interceptor class
is a class (distinct from the target class) whose methods are invoked in
response to invocations and/or lifecycle events on the target class."

CDI has extended the notion of interceptor to managed beans (Section
3.1.1 of CDI 1.0 spec)
and introduced the notion of interceptor binding using annotation
(Chapter 9 of CDI 1.0 spec).
Basically, the interceptor binding specifies where the interceptor is
applied.

The following sample codes illustrate the idea:

@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD, TYPE})
public @interface LoggingInterceptor {
}

-----

@LoggingInterceptor
@Interceptor
public class LoggingInterceptorSimple {
     @AroundInvoke
     public Object log(InvocationContext context) throws Exception {
         System.out.println("LOG: " + context.getMethod());
         return context.proceed();
     }
}

-----

public class Dummy {
     @LoggingInterceptor
     public void process() {
         ...
     }
}


In Java EE Spec, a transaction interceptor is proposed.
One introduces the interceptor binding @Transactional and more details
can be found in
http://java.net/projects/javaee-spec/lists/jsr342-experts/archive/2011-12/message/2

Since servlets, filters and listeners are concrete classes with a
default constructor,
if they are also not of a non-statiic inner classes and without
implementing javax.enterise.inject.spi.Extension, they will be managed
beans (see Secion 3.1.1 of CDI 1.0 spec).
(The definition in 3.1.1 are quite general. One may need to clarify this
if necessary.)

Note that the javax.interceptor.InvocationContext above allows us to
access the target classes (servlets/filters/listeners), request and
response objects, etc. (See javadoc
http://docs.oracle.com/javaee/6/api/javax/interceptor/InvocationContext.html
)

So, one can in fact perform filter like functionality through
interceptors in servlets.

Is there any concern if Servlet spec support the interceptors above?
We would like to listen to opinion of experience group.

Thanks.

Shing Wai Chan