过滤器

原创 2017年09月13日 20:27:36
定义:在对项目中的资源发起请求时,可以通过配置的过滤规则,对指定的资源请求进行拦截,执行完过滤器中的逻辑之后,再根据
写法:①实现Filter接口,②实现init,doFilter,destroy方法,③在web.xml中配置过滤器
//配置文档方式↓
<filter>
<filter-name>Filter</filter-name>
<filter-class>com.Filter</filter-class>
</filter>

<filter-mapping>
<filter-name>Filter</filter-name>
<url-pattern>/*</url-pattern>//可以指定过滤不同页面,此处是过滤所有页面
</filter-mapping>
//注解方式↓
@WebFilter(
    filterName = "Filter",
    urlPatterns = {"/*"},
    initParams = {
        @WebInitParam(name = "encoding",value = "UTF-8")
    }
)

//判断当前页面有没有登录
<filter>
    <filter-name>loginFilter</filter-name>
    <filter-class>homework.filter.LoginFilter</filter-class>
    <init-param>
        <param-name>pass</param-name>
        <param-value>login.jsp,register.jsp</param-value>//指定放行的页面
    </init-param>
</filter>
<filter-mapping>
    <filter-name>loginFilter</filter-name>
    <url-pattern>*.jsp</url-pattern>//过滤所有jsp页面
</filter-mapping>
//======================在LoginFilter.java取得需要放行的页面
public class LoginFilter implements Filter {
    String[] pages=null;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("init LoginFilter");
         pages=filterConfig.getInitParameter("pass").split(",");
        for (String str:pages) {
            System.out.println("who can pass-->"+str);
        }
    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("doFilter LoginFilter");
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        //1、取得客户端请求的资源名称
        String uri = request.getRequestURI();
        uri = uri.substring(uri.lastIndexOf("/")+1);
        //2、判断请求的资源是否在放行之列,检查session中有没有用户信息
        boolean isPass = false;
        for(String page:pages){
            if(page.equals(uri)){
                isPass=true;
                break;
            }
        }
        if(isPass){
            filterChain.doFilter(request,response);
        }else {
            Object object = request.getSession().getAttribute("no");
            //3、有则放行,否则拦截
            if(object !=null){
                filterChain.doFilter(request,response);
            }else{
                request.setAttribute("message","请登录再试");
                request.getRequestDispatcher("../homework/login.jsp").forward(request,response);
            }
        }
    }
    @Override
    public void destroy() {
        System.out.println("destroy LoginFilter");
    }
}

//编码过滤
public class Filter_All implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("过滤器初始化");
    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("过滤器执行");
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        request.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");
        servletResponse.setContentType("text/html;charset=utf-8");
        filterChain.doFilter(servletRequest,servletResponse);
    }
    @Override
    public void destroy() {
        System.out.println("过滤器被销毁");
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)