Java Reference
In-Depth Information
IMPLEMENTING AOP IN PLAIN CODE
Java SE does not offer out‐of‐the‐box support for AOP. You can achieve plain AOP by using third‐
party frameworks such as AspectJ or Spring. Such frameworks used to depend on XML‐only
coni guration; however, you can now achieve AOP through the use of annotations. Implementation and
coni guration of both frameworks are beyond the scope of this topic, but they have a proven record
and can easily be implemented. They both provide a valid alternative to the Java EE implementation.
However, Java web applications have the advantage of using servlets to intercept the request or the
response, which works similarly to aspects. To implement a servlet i lter, create a new class i le and
implement the servlet i lter interface. Then provide an implementation of the doFilter () method, as
shown in Listing 8‐1.
LISTING 8‐1: Simple implementation of a servlet i lter
package com.devchronicles.interceptor.filter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SecurityFilter implements Filter {
@SuppressWarnings("unused")
private FilterConfig filterConfig = null;
@Override
public void doFilter(ServletRequest request, ServletResponse response,
F ilterChain filterChain) throws IOException, ServletException {
Log.info(((HttpServletRequest) request).getRemoteAddr());
//perform some security check
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
}
The web container needs the coni guration shown in Listing 8‐2 to activate the servlet i lter on given
uniform resource locators (URLs). This is placed in the web application's web.xml i le.
 
Search WWH ::




Custom Search