I want to make template engine available in servlets. The concrete template engine implementation should be changeable behind the TemplateEngine interface. With usual dependency injection it could look like this:
public abstract class BaseServlet extends HttpServlet {
private TemplateEngine templateEngine;
public void setTemplateEngine(TemplateEngine te) {
templateEngine = te;
}
protected void render(Result result, HttpServletResponse response) {
templateEngine.render(result, resonse);
}
}
The disadvantage of this approach is that each Servlet that wants to use the render method has to extend BaseServlet. Therefore I'd like to have a statically imported render method.
public class TemplateEngineWrapper {
@Inject
static TemplateEngine templateEngine;
public static void render(Result result, HttpServletResponse response) {
templateEngine.render(result, resonse);
}
}
In the Servlet I would use it this way:
import static TemplateEngineWrapper.render;
...
public void doGet(...) {
render(new Result(200, "Everything is fine."), response);
}
...
Is something wrong with this approach? If so: What would you suggest instead?