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));

}
}

About nitingautam

I am Tech Lead (Java/J2EE/ExtJs) with a MNC located @ Gurgaon.
This entry was posted in HowTo, Java. Bookmark the permalink.

One Response to How To get ServletContext inside Filter

Leave a Reply

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