Quantcast
Channel: Developer Feed - Snippet
Viewing all articles
Browse latest Browse all 178

Sniffer Http Filter J2EE

$
0
0

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.

<!--break-->Snippet

  1. packagelivrona.servlets;
  2. importjava.io.IOException;
  3. importjava.util.Enumeration;
  4.  
  5. importjavax.servlet.Filter;
  6. importjavax.servlet.FilterChain;
  7. importjavax.servlet.FilterConfig;
  8. importjavax.servlet.ServletException;
  9. importjavax.servlet.ServletRequest;
  10. importjavax.servlet.ServletResponse;
  11. importjavax.servlet.http.HttpServletRequest;
  12.  
  13. publicfinalclass SniffFilter implements Filter {
  14.         private FilterConfig filterConfig =null;
  15.  
  16.         publicvoid init(FilterConfig filterConfig)throws ServletException {
  17.                 this.filterConfig= filterConfig;
  18.         }
  19.  
  20.         publicvoid destroy(){
  21.                 this.filterConfig=null;
  22.         }
  23.         publicvoid doFilter(
  24.                 ServletRequest request,
  25.                 ServletResponse response,
  26.                 FilterChain chain)
  27.                 throwsIOException, 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. }

Viewing all articles
Browse latest Browse all 178

Trending Articles