freemarker.ext.servlet
Class FreemarkerServlet
java.lang.Object
|
+--javax.servlet.GenericServlet
|
+--javax.servlet.http.HttpServlet
|
+--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:
- It utilizes a TemplateCache that is fully parameterizable
- It makes all request, request parameters, session, and servlet
context attributes available to templates through "Request", "RequestParameters",
"Session", and "Application" variables. The implementation for these variables
is efficient; rather than copying the attributes and parameters into
SimpleHash
,
special wrapper classes are used that implement the TemplateHashModel
interface.
Supported initialization parameters are:
- TemplatePath specifies the location of the templates.
By default, the location is the web application-relative URI of
the location.
Alternatively, you can prepend it with file:// to indicate a literal
path in the file system (i.e. file:///var/www/project/templates/).
Note that three slashes were used to specify an absolute path.
Also, you can prepend it with class://path/to/template/package to
indicate that you want to load templates from the specified package
accessible through the classloader of the servlet.
Default value is class:// (that is, the root of the class hierarchy).
Note that this default is different than the default in FreeMarker 1.x, when
it defaulted /, that is to loading from the webapp root directory.
- TemplateUpdateInterval value for update interval of the
template cache in milliseconds. Default is 60000 (ten minutes)
- NoCache if set to true, generates headers in the response
that advise the HTTP client not to cache the returned page. Default false
- Version:
- 1.0
- Author:
- Attila Szegedi, szegedia@freemail.hu
- See Also:
- Serialized Form
Method Summary |
protected void |
createCache()
|
protected HttpRequestParametersHashModel |
createRequestParametersHashModel(javax.servlet.http.HttpServletRequest request)
|
void |
doGet(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
|
void |
doPost(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
|
protected java.lang.String |
getTemplatePath()
|
void |
init()
|
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,
TemplateModelRoot 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,
TemplateModelRoot 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 |
serialVersionUID
public static final long serialVersionUID
debug
protected boolean debug
FreemarkerServlet
public FreemarkerServlet()
init
public void init()
throws javax.servlet.ServletException
- Overrides:
init
in class javax.servlet.GenericServlet
createCache
protected void createCache()
throws java.io.IOException
doGet
public void doGet(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
throws javax.servlet.ServletException,
java.io.IOException
- Overrides:
doGet
in class javax.servlet.http.HttpServlet
doPost
public void doPost(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
throws javax.servlet.ServletException,
java.io.IOException
- Overrides:
doPost
in class javax.servlet.http.HttpServlet
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
tipically overridden to manage serving of non-template resources (i.e. images)
that reside in template directory.
- Parameters:
request
- the HTTP requestresponse
- the HTTP response- Returns:
- true to indicate this method has processed the request entirely,
and that the further request processing should not take place.
getTemplatePath
protected final java.lang.String getTemplatePath()
createRequestParametersHashModel
protected HttpRequestParametersHashModel createRequestParametersHashModel(javax.servlet.http.HttpServletRequest request)
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 requestresponse
- the actual HTTP response
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 requestresponse
- the actual HTTP response
preTemplateProcess
protected boolean preTemplateProcess(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
Template template,
TemplateModelRoot 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 TemplateModelRoot.
- Parameters:
request
- the actual HTTP requestresponse
- the actual HTTP responsetemplate
- the template that will get executeddata
- the data that will be passed to the template- Returns:
- true to process the template, false to suppress template processing.
postTemplateProcess
protected void postTemplateProcess(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
Template template,
TemplateModelRoot 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 requestresponse
- the actual HTTP responsetemplate
- the template that was executeddata
- the data that was passed to the template