The Intercepting Filter (II)
11 February 2008This post is in continuation to The Intercepting Filter (I). Do read that one before going through this.
Intercepting Filter facilitates to insert or remove filters runtime during deployment. Also to be noted that Intercepting Filter deals with requests/response and not with applications appearance. The design pattern Intercepting Filter perform with minimum effect on external code processing. It provides authentication, authorization, transformation and conversion of requests and responses as needed.
The example below demonstrates use of filter. It is very basic example which shows minimal use of Filter. SimpleFilterDemo class implements Filter class. doFilter() is the important method in this class which will consists of logic for filtering request/response. The output Simple Filter Demo will be shown in the browser.
import java.io.*; import javax.servlet.*; public class SimpleFilterDemo implements Filter { private FilterConfig filterConfig; public void init(FilterConfig filterConfig) { this.filterConfig = filterConfig; System.out.println("Simple Filter Demo"); } public void doFilter( ServletRequest request, ServletResponse response, FilterChain filterChain ) throws ServletException, IOException { PrintWriter pw = response.getWriter(); pw.println("<html>"); pw.println("<head>"); pw.println("</head>"); pw.println("<body>"); pw.println("<h3>Simple Filter Demo</h3>"); pw.println("</body>"); pw.println("</html>"); } public void destroy() { } }
Related Posts:
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: The Intercepting Filter (II)