freemarker.ext.servlet
Class FreeMarkerServlet

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by freemarker.ext.servlet.FreeMarkerServlet
All Implemented Interfaces:
java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

public class FreeMarkerServlet
extends javax.servlet.http.HttpServlet

This is a general-purpose FreeMarker servlet with following features:

Supported initialization parameters are:

Version:
$Id: FreeMarkerServlet.java 1101 2013-04-01 04:17:32Z run2000 $
Author:
Attila Szegedi, attila@szegedi.org
See Also:
Serialized Form

Field Summary
protected  boolean debug
           
protected  int maximumAge
           
static long serialVersionUID
          Class UUID for serialization.
protected  long updateInterval
           
 
Constructor Summary
FreeMarkerServlet()
           
 
Method Summary
protected  TemplateCache createCache()
          Creates the TemplateCache to be used for this servlet.
protected  HttpRequestParametersHashModel createRequestParametersHashModel(javax.servlet.http.HttpServletRequest request)
          Get the request parameters from the servlet as a FreeMarker hash model.
 void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Implement the "GET" method for HTTP requests.
 void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Implement the "POST" method for HTTP requests.
protected  java.lang.String getTemplatePath()
          Retrieve the template path.
 void init()
          Initialise the servlet.
protected  void initializeServletContext(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Called when servlet detects in a request processing that application-global (that is, ServletContext-specific) attributes are not yet set.
protected  void initializeSession(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Called when servlet detects in a request processing that session-global (that is, HttpSession-specific) attributes are not yet set.
protected  void postTemplateProcess(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Template template, TemplateWriteableHashModel data)
          Called after the execution returns from template.process().
protected  boolean preprocessRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Called as the first step in request processing, before the templating mechanism is put to work.
protected  boolean preTemplateProcess(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Template template, TemplateWriteableHashModel data)
          Called before the execution is passed to template.process().
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPut, doTrace, getLastModified, service, service
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serialVersionUID

public static final long serialVersionUID
Class UUID for serialization.

See Also:
Constant Field Values

debug

protected boolean debug

updateInterval

protected long updateInterval

maximumAge

protected int maximumAge
Constructor Detail

FreeMarkerServlet

public FreeMarkerServlet()
Method Detail

init

public void init()
Initialise the servlet.

Overrides:
init in class javax.servlet.GenericServlet

createCache

protected TemplateCache createCache()
Creates the TemplateCache to be used for this servlet.


doGet

public void doGet(javax.servlet.http.HttpServletRequest request,
                  javax.servlet.http.HttpServletResponse response)
           throws javax.servlet.ServletException,
                  java.io.IOException
Implement the "GET" method for HTTP requests.

Overrides:
doGet in class javax.servlet.http.HttpServlet
Parameters:
request - the servlet request
response - the servlet response
Throws:
javax.servlet.ServletException
java.io.IOException

doPost

public void doPost(javax.servlet.http.HttpServletRequest request,
                   javax.servlet.http.HttpServletResponse response)
            throws javax.servlet.ServletException,
                   java.io.IOException
Implement the "POST" method for HTTP requests.

Overrides:
doPost in class javax.servlet.http.HttpServlet
Parameters:
request - the servlet request
response - the servlet response
Throws:
javax.servlet.ServletException
java.io.IOException

preprocessRequest

protected boolean preprocessRequest(javax.servlet.http.HttpServletRequest request,
                                    javax.servlet.http.HttpServletResponse response)
                             throws javax.servlet.ServletException,
                                    java.io.IOException
Called as the first step in request processing, before the templating mechanism is put to work. By default does nothing and returns false. This method is typically overridden to manage serving of non-template resources (i.e. images) that reside in template directory.

Parameters:
request - the HTTP request
response - the HTTP response
Returns:
true to indicate this method has processed the request entirely, and that the further request processing should not take place.
Throws:
javax.servlet.ServletException
java.io.IOException

getTemplatePath

protected final java.lang.String getTemplatePath()
Retrieve the template path. This is the root of the directory structure being cached by the TemplateCache.

Returns:
the template path being cached

createRequestParametersHashModel

protected HttpRequestParametersHashModel createRequestParametersHashModel(javax.servlet.http.HttpServletRequest request)
Get the request parameters from the servlet as a FreeMarker hash model.

Parameters:
request - the servlet request parameters as an HttpServletRequest
Returns:
the request parameters as a TemplateHashModel

initializeServletContext

protected void initializeServletContext(javax.servlet.http.HttpServletRequest request,
                                        javax.servlet.http.HttpServletResponse response)
                                 throws javax.servlet.ServletException,
                                        java.io.IOException
Called when servlet detects in a request processing that application-global (that is, ServletContext-specific) attributes are not yet set. This is a generic hook you might use in subclasses to perform a specific action on first request in the context. By default it does nothing.

Parameters:
request - the actual HTTP request
response - the actual HTTP response
Throws:
javax.servlet.ServletException
java.io.IOException

initializeSession

protected void initializeSession(javax.servlet.http.HttpServletRequest request,
                                 javax.servlet.http.HttpServletResponse response)
                          throws javax.servlet.ServletException,
                                 java.io.IOException
Called when servlet detects in a request processing that session-global (that is, HttpSession-specific) attributes are not yet set. This is a generic hook you might use in subclasses to perform a specific action on first request in the session. By default it does nothing.

Parameters:
request - the actual HTTP request
response - the actual HTTP response
Throws:
javax.servlet.ServletException
java.io.IOException

preTemplateProcess

protected boolean preTemplateProcess(javax.servlet.http.HttpServletRequest request,
                                     javax.servlet.http.HttpServletResponse response,
                                     Template template,
                                     TemplateWriteableHashModel data)
                              throws javax.servlet.ServletException,
                                     java.io.IOException
Called before the execution is passed to template.process(). This is a generic hook you might use in subclasses to perform a specific action before the template is processed. By default does nothing. A typical action to perform here is to inject application-specific objects into the TemplateWriteableHashModel.

Parameters:
request - the actual HTTP request
response - the actual HTTP response
template - the template that will get executed
data - the data that will be passed to the template
Returns:
true to process the template, false to suppress template processing.
Throws:
javax.servlet.ServletException
java.io.IOException

postTemplateProcess

protected void postTemplateProcess(javax.servlet.http.HttpServletRequest request,
                                   javax.servlet.http.HttpServletResponse response,
                                   Template template,
                                   TemplateWriteableHashModel data)
                            throws javax.servlet.ServletException,
                                   java.io.IOException
Called after the execution returns from template.process(). This is a generic hook you might use in subclasses to perform a specific action after the template is processed. It will be invoked even if the template processing throws an exception. By default does nothing.

Parameters:
request - the actual HTTP request
response - the actual HTTP response
template - the template that was executed
data - the data that was passed to the template
Throws:
javax.servlet.ServletException
java.io.IOException