欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-26 20:28:04
本文实例为大家分享了servlet过滤器的具体代码,供大家参考,具体内容如下 1.servlet过滤器产生背景   项目中我们会遇到这样一类的需求,对访问服务器的用户i...

本文实例为大家分享了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>

结果如下:

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

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

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

上一篇: Android自定义等待对话框

下一篇: