package com.yuntu.test.servlet;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
/**
* Servlet implementation class HelloFreeMarkSerlet
*/
@WebServlet("/HelloFreeMarkSerlet")
@SuppressWarnings("serial")
public class HelloFreeMarkerServlet extends HttpServlet {
Configuration cfg = null;
public void init() throws ServletException{
cfg = new Configuration();
cfg.setServletContextForTemplateLoading(getServletContext(),
"/WEB-INF/tamplates");
}
@SuppressWarnings("unchecked")
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Map root = new HashMap();
root.put("message", "hello world");
root.put("nmae", "迷失");
Template t = cfg.getTemplate("test.ftl");
response.setContentType("text/html;charset=" + t.getEncoding());
Writer out = response.getWriter();
try {
t.process(root, out);
} catch (TemplateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
@see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
@SuppressWarnings("unchecked")
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
public void destroy(){
super.destroy();
}
}