Reputation: 185
I have inserted 3 filters.All the filters do not run If I change the order of execution in the web.xml. Below is the web.xml that works. But if I put "Level 3" filter at the bottom of the web.xml file, it doesn't run some filters. It acts like the other filters do not exist. If I put the level 3 filter at the very bottom, it only passes through the Level 1 filter. there no error either.
The working order of 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>
Here are the Java filter classes.
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() {} ;
}
Level 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() {} ;
}
Level 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() {} ;
}
Upvotes: 1
Views: 880
Reputation: 2614
The order the container uses in building the chain of filters to be applied for a particular request URI is as follows:
First, the <url-pattern>
matching filter mappings in the same order that these elements appear in the deployment descriptor.
Next, the <servlet-name>
matching filter mappings in the same order that these elements appear in the deployment descriptor.
as it is mentioned in 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.
Upvotes: 1