example
public class: HelloServlet [javadoc |
source]
java.lang.Object
javax.servlet.GenericServlet
javax.servlet.http.HttpServlet
example.HelloServlet
All Implemented Interfaces:
Serializable, Servlet, ServletConfig
This Servlet does not do anything useful, just prints "Hello World!". The
intent is to help you to get started if you want to build your own Controller
servlet that uses FreeMarker for the View. For more advanced example, see the
2nd Web application example.
| Method from example.HelloServlet Summary: |
|---|
|
doGet, init |
| Methods from javax.servlet.http.HttpServlet: |
|---|
|
service |
| Methods from javax.servlet.GenericServlet: |
|---|
|
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, init, log, log, service |
| Method from example.HelloServlet Detail: |
protected void doGet(HttpServletRequest req,
HttpServletResponse resp) throws IOException, ServletException {
// Build the data-model
Map root = new HashMap();
root.put("message", "Hello World!");
// Get the templat object
Template t = cfg.getTemplate("test.ftl");
// Prepare the HTTP response:
// - Use the charset of template for the output
// - Use text/html MIME-type
resp.setContentType("text/html; charset=" + t.getEncoding());
Writer out = resp.getWriter();
// Merge the data-model and the template
try {
t.process(root, out);
} catch (TemplateException e) {
throw new ServletException(
"Error while processing FreeMarker template", e);
}
}
|
public void init() {
// Initialize the FreeMarker configuration;
// - Create a configuration instance
cfg = new Configuration();
// - Templates are stoted in the WEB-INF/templates directory of the Web app.
cfg.setServletContextForTemplateLoading(
getServletContext(), "WEB-INF/templates");
// In a real-world application various other settings should be explicitly
// set here, but for the sake of brevity we leave it out now. See the
// "webapp2" example for them.
}
|