qq_36750352
喜只狼果冻
采纳率50%
2019-03-05 15:39 阅读 10.8k

如何配置filter过滤器,如何放行而不是全部拦截

配置过滤器次次失败,比如我通过servlet验证登录,然后我允许验证登录的servlet请求通过以及注册页面,我该如何配置

web.xml

 <filter>
    <filter-name>loginFilter</filter-name>
    <filter-class>com.lero.filter.LoginFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>loginFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

为什么我写的拦截器跳不到login.jsp同事resister.jsp也通过不了

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpSession session = request.getSession();
        Object object = session.getAttribute("name");
        String path = request.getServletPath();
        if ( path.indexOf("register") >-1) {

            filterChain.doFilter(servletRequest, servletResponse);
            return;
        } else {
            response.sendRedirect("login.jsp");
        }
    }

然后我这样写虽然都通过了,但页面确实空白

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpSession session = request.getSession();
        Object object = session.getAttribute("name");
        String path = request.getServletPath();
        if ( path.indexOf("login") >=0) {

            filterChain.doFilter(request, response);
            return;
        } else if(path.indexOf("redister.jsp")>-1){
            filterChain.doFilter(request, response);
        }
    }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

5条回答 默认 最新

  • weixin_36876700 混沌太初 2019-03-05 18:32
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
    
    这里的都不会被拦截
    
    点赞 2 评论 复制链接分享
  • Dream_to_good Dream_to_good 2019-03-05 16:07

    正常的系统,肯定需要都拦截吧 不然要拦截器干嘛,如果想放行一些不想拦截的 看看这个例子吧,
    https://www.cnblogs.com/liyangxj/p/4506148.html

    点赞 评论 复制链接分享
  • weixin_44205057 正在奋斗中的小白 2019-03-05 17:33

    在web中添加个filter拦截就可以了

    点赞 评论 复制链接分享
  • qq_26899265 qq_26899265 2019-03-05 20:40

    /*
    改成

    <url-pattern>/</url-pattern>
    
    点赞 评论 复制链接分享
  • qq_44729767 爸爸的臭猪猪 2019-03-05 23:38

    过滤器的过程就是先从jsp页面获取数据,传到selevt里面进行对比,正确就返回,不正确就再跳转到另外一个报错jsp页面

    点赞 评论 复制链接分享

相关推荐