servlet过滤器(Filter)详解(九)

本文实例为大家分享了servlet过滤器的具体代码,供大家参考,具体内容如下

1.servlet过滤器产生背景

  项目中我们会遇到这样一类的需求,对访问服务器的用户ip实施过滤,只有在允许名单中的ip才能访问服务,为了实现需求,每当有客户端请求时,我们都会写校验ip的代码,客户端能够访问到的servlet我们都需要这样做,很明显这样做有一个缺点,就是代码冗余,维护不方便,如果验证规则改变,修改起来也特别麻烦。

  为了解决以上问题,Filter 技术应运而生。

2.servlet过滤器是什么?

  servlet过滤器是在java servlet 2.3 中定义的。它能够对servlet容器传给servlet组件的ServletRequest 对象和 ServletResponse对象进行检查和修改。

3.案例

  所有servlet过滤器都要实现javax.servlet.Filter接口。

NoteFilter

package com.learn;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by Administrator on 2017/09/29.
 */
public class NoteFilter implements Filter {

 private FilterConfig config = null;

 private String ipTable = null; //ip名单


 @Override
 public void init(FilterConfig filterConfig) throws ServletException {
  System.out.println("note filter initial");
  this.config = filterConfig;
  this.ipTable = config.getInitParameter("ipTable");

 }

 @Override
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  System.out.println("do filter starting");

  //校验ip地址
  if(!veryfyIP(request,response))
   return;

  long befor = System.currentTimeMillis();
  config.getServletContext().log("before call note Filter");

  chain.doFilter(request,response);

  config.getServletContext().log("after call note Filter");
  long after = System.currentTimeMillis();

  String name ="";
  if(request instanceof HttpServletRequest){
   name = ((HttpServletRequest) request).getRequestURI();
  }
  config.getServletContext().log("Note Filter : name:"+name +" time :"+(after -befor)+"ms");


 }

 @Override
 public void destroy() {

 }

 private boolean veryfyIP(ServletRequest request, ServletResponse response){

  String ip = request.getRemoteAddr();
  System.out.println("请求ip:"+ip);
  System.out.println("ipTable 黑名单:"+ipTable);

  if(ip.indexOf(ipTable) == -1){
   System.out.println("校验不通过");
   response.setContentType("text/html");
   PrintWriter out = null;
   try {
    out = response.getWriter();
    out.print("<h1>对不起,你的ip不能访问服务器</h1>");
    out.flush();
    out.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
   return false;

  } else {
   return true;
  }

 }


}

NoteServlet

package com.learn;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Administrator on 2017/09/29.
 */
public class NoteServlet extends HttpServlet {

 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  System.out.println("已经通过了过滤器");


 }
}

web.xml配置

<filter>
  <filter-name>ip</filter-name>
  <filter-class>com.learn.NoteFilter</filter-class>
  <init-param>
   <param-name>ipTable</param-name>
   <param-value>127.0.0.1</param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>ip</filter-name>
  <url-pattern>/note</url-pattern>
 </filter-mapping>
<servlet>
  <servlet-name>note</servlet-name>
  <servlet-class>com.learn.NoteServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>note</servlet-name>
  <url-pattern>/note</url-pattern>
 </servlet-mapping>

结果如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。