在web.xml

时间:2015-04-24 07:05:39

标签: java servlets web.xml servlet-filters

我插入了3个过滤器。所有过滤器都没有运行如果我在web.xml中更改了执行顺序。下面是有效的web.xml。但如果我把"等级3"在web.xml文件的底部过滤,它不会运行一些过滤器。它的行为就像其他过滤器不存在一样。如果我将3级过滤器放在最底部,它只会通过1级过滤器。也没有错误。

web.xml的工作顺序

  <filter>
        <filter-name>LoginFilter_Level3</filter-name>
        <filter-class>Filter.LoginFilter_Level3</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>LoginFilter_Level3</filter-name>   
        <url-pattern>/Patient_InsertAllergy</url-pattern>
        ..................
        ....................
    </filter-mapping>


<filter>
    <filter-name>LoginFilter_Level1</filter-name>
    <filter-class>Filter.LoginFilter_Level1</filter-class>
</filter>
<filter-mapping>
    <url-pattern>/SocialHistorySrvlt</url-pattern>
    ........................
    ...........................
 </filter-mapping>


<filter>
    <filter-name>LoginFilter_Level2</filter-name>
    <filter-class>Filter.LoginFilter_Level2</filter-class>
</filter>
<filter-mapping>
    <filter-name>LoginFilter_Level2</filter-name>
    <url-pattern>/Patient_InsertAllergy</url-pattern>
         <url-pattern>/VitalsSrvlt</url-pattern>
         .....................................
         ......................................
</filter-mapping>

以下是Java过滤器类。

public class LoginFilter_Level2 implements Filter {

    private static final boolean debug = true;

    // The filter configuration object we are associated with.  If
    // this value is null, this filter instance is not currently
    // configured. 
   // private FilterConfig filterConfig = null;

    public LoginFilter_Level2() {
    }    

    public void init(FilterConfig arg0) throws ServletException {}  

public void doFilter(ServletRequest req, ServletResponse resp,FilterChain chain) throws IOException, ServletException
{  

    HttpServletRequest request = (HttpServletRequest)req;
    HttpServletResponse response = (HttpServletResponse)resp;

    HttpSession session = request.getSession(false);
    Integer attribute =null;


    if(session!=null && session.getAttribute("subUSerTypeID")!=null)
    {
        attribute = Integer.parseInt(session.getAttribute("subUSerTypeID").toString());
    }     


    if(attribute==1|| attribute==2)
    {       
        RequestDispatcher dispatch = request.getRequestDispatcher("/Patient_DisplayAll");
        dispatch.forward(req, resp);
    }
    else
    {
        chain.doFilter(request,response);
    }   

}  
    public void destroy() {}  ;

}

等级1

public class LoginFilter_Level1 implements Filter {

    private static final boolean debug = true;

    // The filter configuration object we are associated with.  If
    // this value is null, this filter instance is not currently
    // configured. 
   // private FilterConfig filterConfig = null;

    public LoginFilter_Level1() {
    }    

    public void init(FilterConfig arg0) throws ServletException {}  

public void doFilter(ServletRequest req, ServletResponse resp,FilterChain chain) throws IOException, ServletException
{  

    HttpServletRequest request = (HttpServletRequest)req;
    HttpServletResponse response = (HttpServletResponse)resp;

    HttpSession session = request.getSession(false);
    Integer attribute =null;


    if(session!=null && session.getAttribute("SubUserID")!=null)
    {
        attribute = Integer.parseInt(session.getAttribute("SubUserID").toString());

    }
     System.out.println("SubuserID level1= "+session.getAttribute("SubUserID"));

    if(attribute==null)
    {
        RequestDispatcher dispatch = request.getRequestDispatcher("index.jsp");
        dispatch.forward(req, resp);
        System.out.println("Executed 1");
    }
    else
    {
        System.out.println("Executed 2");
        chain.doFilter(request,response);
    }


}  
    public void destroy() {}  ;

}

等级3

 public class LoginFilter_Level3 implements Filter {

    private static final boolean debug = true;

    // The filter configuration object we are associated with.  If
    // this value is null, this filter instance is not currently
    // configured. 
   // private FilterConfig filterConfig = null;

    public LoginFilter_Level3() {
    }    

    public void init(FilterConfig arg0) throws ServletException {}  

public void doFilter(ServletRequest req, ServletResponse resp,FilterChain chain) throws IOException, ServletException
{  

    HttpServletRequest request = (HttpServletRequest)req;
    HttpServletResponse response = (HttpServletResponse)resp;


     if(request.getParameter("idPatient")!=null)
     {
        int idPatient = Integer.parseInt(request.getParameter("idPatient"));

        PatientTable pt=new PatientTable();         

        int idUser=Integer.parseInt(request.getSession(false).getAttribute("UserID").toString()); 

        ResultSet rs2=pt.getIdUserOfPatient(idPatient);

        int dbIdUser=0;

        try
        {
          while(rs2.next())
          {
            dbIdUser=rs2.getInt("idUser"); //changed the idUser to idSubUser
          }
        } 
        catch (SQLException ex)
        {
            Logger.getLogger(Problems_DisplayAll.class.getName()).log(Level.SEVERE, null, ex);
        }

        if(dbIdUser!=idUser)
        {
          response.sendRedirect("index.jsp");
        }
        else
        {
            chain.doFilter(request,response); 
        }

     }    
}  
    public void destroy() {}  ;

}  

1 个答案:

答案 0 :(得分:1)

容器用于构建要应用于特定请求URI的过滤器链的顺序如下:

首先,lambdaA匹配过滤器映射的顺序与这些元素在部署描述符中出现的顺序相同。

接下来,<url-pattern>匹配过滤器映射的顺序与这些元素在部署描述符中出现的顺序相同。

doc

中提及
  

链是通过过滤器映射间接形成的。链中的过滤器顺序与过滤器映射在Web应用程序部署描述符中显示的顺序相同。