Java Servlet Filter implement

Description

Java Servlet Filter implement

import java.io.IOException;

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.annotation.WebFilter;

@WebFilter("/*")// ww w. j av a 2s .  c  o  m
public class Main implements Filter {

   private FilterConfig filterConf = null;

   public void init(FilterConfig filterConf) {
      this.filterConf = filterConf;
   }

   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
         throws IOException, ServletException {
      String userAddy = request.getRemoteHost();

      filterConf.getServletContext().log("Vistor User IP: " + userAddy);

      chain.doFilter(request, response);

   }

   public void destroy() {
   }
}



PreviousNext

Related