码迷,mamicode.com
首页 > 其他好文 > 详细

servlet 过滤器(Filter)(八)

时间:2017-09-30 00:23:20      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:override   color   对象   font   code   ace   try   ram   name   

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)(八)

标签:override   color   对象   font   code   ace   try   ram   name   

原文地址:http://www.cnblogs.com/wwyx-xi/p/7613011.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!