Breaking News
Home / ServerSide / Sniffer Http Filter J2EE

Sniffer Http Filter J2EE

How to write a Sniffer Http Filter in J2EE?Do you want to know the request headers and other parameters for each of the HTTP requests coming into the application server? If yes, a Sniffer filter like the one show below can dump all the incoming parameters to be analyzed later. This for sure helps in debugging. Later this can be extended to dump only when you want to trace some bug or issue by adding a property switch that dumps this information. Further you can store this information to a file or database are required.

<!>Snippet

  1. package livrona.servlets;
  2. import java.io.IOException;
  3. import java.util.Enumeration;
  4.  
  5. import javax.servlet.Filter;
  6. import javax.servlet.FilterChain;
  7. import javax.servlet.FilterConfig;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.ServletRequest;
  10. import javax.servlet.ServletResponse;
  11. import javax.servlet.http.HttpServletRequest;
  12.  
  13. public final class SniffFilter implements Filter {
  14.         private FilterConfig filterConfig = null;
  15.  
  16.         public void init(FilterConfig filterConfig) throws ServletException {
  17.                 this.filterConfig = filterConfig;
  18.         }
  19.  
  20.         public void destroy() {
  21.                 this.filterConfig = null;
  22.         }
  23.         public void doFilter(
  24.                 ServletRequest request,
  25.                 ServletResponse response,
  26.                 FilterChain chain)
  27.                 throws IOException, ServletException {
  28.  
  29.                 if (filterConfig == null)
  30.                         return;
  31.  
  32.                 HttpServletRequest req = (HttpServletRequest) request;
  33.  
  34.                 Enumeration e = req.getHeaderNames();
  35.                 while (e.hasMoreElements()) {
  36.                         String headerName = (String) e.nextElement();
  37.                         System.out.println(headerName + "=" + req.getHeader(headerName));
  38.                 }
  39.  
  40.                 System.out.println("RemoteHost = " + req.getRemoteHost());
  41.  
  42.                 chain.doFilter(request, response);
  43.         }
  44. }
[pb_builder]

Check Also

What webservices standards are supported by Siebel Business Applications?

The following Web services standards are supported by Siebel Business Applications:     * Web Services …

Leave a Reply