How To get ServletContext inside Filter

I was coding last night and I have to get some information from Manifest file in filters. In order to read the Manifest I need the ServletContext. Using below code you can get the ServletContext inside Filter.

public class SomeFilter implements Filter {
FilterConfig config;

public void setFilterConfig(FilterConfig config) {
this.config = config;
}

public FilterConfig getFilterConfig() {
return config;
}

public void init(FilterConfig config) throws ServletException {
setFilterConfig(config);
}

// doFilter and destroy methods…
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {

ServletContext context = getFilterConfig().getServletContext();
Manifest mManifest=new Manifest();
System.out.println(“Manifest “+mManifest.getBuildInfo(context));

}
}

One Reply to “How To get ServletContext inside Filter”

Leave a Reply to Muhammad Atif Riaz Cancel reply

Your email address will not be published. Required fields are marked *