JavaWeb---过滤器的使用--禁止IP登陆

 

package filter;

/**
 * 过滤IP的过滤器
 */
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class FilterIP implements Filter {

 protected String fip = "";// 要过滤的IP
 protected FilterConfig fcon = null;// 过滤器配置对象

 @Override
 public void destroy() {
  System.out.println("FilterIP 过滤器销毁");
  this.fcon = null;
 }

 /**
  * 执行过滤方法
  *
  * @author admin
  * @param request
  *            请求对象
  * @param response
  *            响应对象
  * @param chain
  *            过滤器对象
  */
 @Override
 public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {
  System.out.println("FilterIP 过滤器执行过滤方法");
  response.setContentType("text/html");
  response.setCharacterEncoding("gb2312");
  // 得到请求的IP地址 和要过滤的Ip地址进行对比
  String ip = request.getRemoteAddr();
  if (fip.equals(ip)) {
   // 对方就是要过滤的对象 不能访问
   String path = "ErrorInfo.jsp";
   RequestDispatcher desc = request.getRequestDispatcher(path);
   desc.forward(request, response);
  } else {
   // 不是 就将过滤器加到过滤链
   chain.doFilter(request, response);
  }
 }

 @Override
 public void init(FilterConfig config) throws ServletException {
  System.out.println("FilterIP 过滤器初始化");
  this.fcon = config;
  fip = fcon.getInitParameter("fip");
  if (fip == null) {
   fip = "";
  }
  System.out.println("要过滤的IP地址为:" + fip);
 }

}

你可能感兴趣的:(Java学习)