Filter not working


March 2019


1.4k time


I basically have a filter and in it's doFilter method, i'm just printing a message to console and i have applied that filter to every page of my sample web app.


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="3.0">



and my filter code

package com.mypkg.filters;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
//other imports

public class LoginFilter implements Filter {

public void init(FilterConfig config) throws ServletException {
     System.out.println("Filter init ran!");

public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
    System.out.println("Filter running!");

public void destroy(FilterConfig config) throws ServletException {
     //System.out.println("Filter destoryed!");

Now when i first ran this filter, i got "Filter running" message but after that i included hibernate and other libraries, it stopped working, i removed them and same result. Filter doesn't run. Checked url pattern and everything.. Clueless!!

Any pointers?

2 answers


First of all, in you doFilter method you should call the next filter in the chain:

chain.doFilter(request, response);

Without this your request will not be processed further after your filter.

This can be a reason of your problem.


Also your destroy() method is wrong. Here how you should rewrite it:

public void destroy() {
     System.out.println("Filter destoryed!");

Maybe you have another Filter executed before yours, that does not call chain.doFilter(), thus preventing next filters (including yours) to be executed.