gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:14:35 25 4
gpt4 key购买 nike

我插入了 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com