Extending Filter Implemtations


Let’s say you are going to extend a class implementing Filter interface.

public class MyFilter implements Filter {


    @Override
    public void init(final FilterConfig config) throws ServletException {

        logger = newLogger();
    }


    protected Logger getLogger() {

        if (logger == null) {
            // before init() or after destroy();
            throw new IllegalStateException("no logger");
        }

        return logger;
    }


    @Override
    public void destroy() {

        logger = null;
    }


    private Logger logger;
}

This is how we should override those two important life-cycle methods

public class YourFilter extends MyFilter {


    @Override
    public void init(final FilterConfig config) throws ServletException {

        super.init(config);

        // do your jobs, after.
        getLogger().log("init()");
    }


    @Override
    public void destroy() {

        // do your jobs, before.
        getLogger().log("destroy()");

        super.destroy();
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s