java - 更改 web.xml 中的执行顺序后,Servlet 过滤器不起作用

标签 java servlets web.xml servlet-filters

我插入了 3 个过滤器。如果我更改 web.xml 中的执行顺序,所有过滤器都不会运行。下面是有效的 web.xml。但是,如果我将“Level 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() {}  ;

}  

最佳答案

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

首先,<url-pattern>按照这些元素在部署描述符中出现的顺序匹配过滤器映射。

接下来,<servlet-name>按照这些元素在部署描述符中出现的顺序匹配过滤器映射。

正如 doc 中提到的那样

chain is formed indirectly via filter mappings. The order of the filters in the chain is the same as the order that filter mappings appear in the web application deployment descriptor.

关于java - 更改 web.xml 中的执行顺序后,Servlet 过滤器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29840846/

相关文章:

java - 日文字体显示问题

java - 如何将特定数据从 JSON 文件解析为 html 下拉菜单

java - 配置 Jersey MVC 以在没有 web.xml 的情况下提供 Viewable

java - 我们可以只为一部分 servlet 配置过滤器吗?

web-services - 如何添加单独的 Web 部署描述符?

java - 如何缓存/读取json文件?

java - 在Ubuntu 16.04中使用JAVA中的MapReduce在文本文件中搜索给定单词

java - 是否可以在 JSP 中创建本地页面范围?

java - 将 html 表从 jsp 页面传递到 servlet

java - 无法使用单个连接执行多个准备语句(数据源)