1 package example; 2 3 import java.util.*; 4 import java.io.*; 5 import javax.servlet.*; 6 import javax.servlet.http.*; 7 import freemarker.template.*; 8 9 15 public class HelloServlet extends HttpServlet { 16 private Configuration cfg; 17 18 public void init() { 19 cfg = new Configuration(); 22 cfg.setServletContextForTemplateLoading( 24 getServletContext(), "WEB-INF/templates"); 25 } 29 30 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 31 throws ServletException, IOException { 32 33 Map root = new HashMap(); 35 root.put("message", "Hello World!"); 36 37 Template t = cfg.getTemplate("test.ftl"); 39 40 resp.setContentType("text/html; charset=" + t.getEncoding()); 44 Writer out = resp.getWriter(); 45 46 try { 48 t.process(root, out); 49 } catch (TemplateException e) { 50 throw new ServletException( 51 "Error while processing FreeMarker template", e); 52 } 53 } 54 } | Popular Tags |