I was able to get a reference to the ServletContext in a Filter. What I like best about this approach is that it occurs in the init() method which is called upon the first load the web application meaning the execution only happens once.
public class MyFilter implements Filter {
protected FilterConfig filterConfig;
public void init(FilterConfig filterConfig) {
String templatePath = filterConfig.getServletContext().getRealPath(filterConfig.getInitParameter("templatepath"));
Utilities.setTemplatePath(templatePath);
this.filterConfig = filterConfig;
}
I added the "templatepath" to the filterConfig via the web.xml:
MyFilter
com.myapp.servlet.MyFilter
templatepath
/templates
MyFilter
/*