public class VelocityViewServlet
extends javax.servlet.http.HttpServlet
A servlet to process Velocity templates. This is comparable to the the JspServlet for JSP-based applications.
The servlet provides the following features:
VelocityViewServlet supports the following configuration parameters in web.xml:
true
. If set to false
, then
the VelocityView
used by this servlet will not be shared
with VelocityViewFilter
s, other VelocityViewServlets or
org.apache.velocity.tools.view.jsp.VelocityViewTag's in the
application.true
. If set to false
, then
the default toolbox configuration will not be added to your (if any)
custom configuration.false
. If set to true
, then
then the final toolbox configuration (the combination of any custom
one(s) provided by yourself and/or the default configuration(s))
will have all invalid tools, properties, and/or data removed prior to
configuring the ToolboxFactory for this servlet by a
ConfigurationCleaner
HttpServletResponse
's writer. If this parameter is
set to true
, then the output of the merge process will be
buffered before being fed to the response. This allows the error(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Throwable)
method to be overridden to return a "500 Internal Server Error" or
at least not return any of the failed request content. Essentially,
setting this to true
degrades performance in order to enable
a more "correct" error response"Modifier and Type | Field and Description |
---|---|
static String |
BUFFER_OUTPUT_PARAM |
Constructor and Description |
---|
VelocityViewServlet() |
Modifier and Type | Method and Description |
---|---|
protected org.apache.velocity.context.Context |
createContext(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response) |
void |
doGet(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Handles GET - calls doRequest()
|
void |
doPost(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Handle a POST request - calls doRequest()
|
protected void |
doRequest(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Handles with both GET and POST requests
|
protected void |
error(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
Throwable e)
Invoked when there is an error thrown in any part of doRequest() processing.
|
protected void |
fillContext(org.apache.velocity.context.Context context,
javax.servlet.http.HttpServletRequest request) |
protected String |
findInitParameter(javax.servlet.ServletConfig config,
String key)
Looks up an init parameter with the specified key in either the
ServletConfig or, failing that, in the ServletContext.
|
protected org.slf4j.Logger |
getLog() |
protected org.apache.velocity.Template |
getTemplate(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response) |
protected org.apache.velocity.Template |
getTemplate(String name) |
protected String |
getVelocityProperty(String name,
String alternate) |
protected VelocityView |
getVelocityView() |
protected org.apache.velocity.Template |
handleRequest(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
org.apache.velocity.context.Context ctx)
This was a common extension point, but now it is usually
simpler to override
fillContext(org.apache.velocity.context.Context, javax.servlet.http.HttpServletRequest) to add custom things
to the Context or override a getTemplate(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
method to change how Template s are retrieved. |
void |
init(javax.servlet.ServletConfig config)
Initializes servlet and VelocityView used to process requests.
|
protected void |
initRequest(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Request and response initialization.
|
protected void |
manageResourceNotFound(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
org.apache.velocity.exception.ResourceNotFoundException e)
Manages the
ResourceNotFoundException to send an HTTP 404 result
when needed. |
protected void |
mergeTemplate(org.apache.velocity.Template template,
org.apache.velocity.context.Context context,
javax.servlet.http.HttpServletResponse response) |
protected void |
requestCleanup(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
org.apache.velocity.context.Context context)
Cleanup routine called at the end of the request processing sequence
allows a derived class to do resource cleanup or other end of
process cycle tasks.
|
protected void |
setContentType(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Sets the content type of the response.
|
protected void |
setVelocityView(VelocityView view) |
doDelete, doHead, doOptions, doPut, doTrace, getLastModified, service, service
public static final String BUFFER_OUTPUT_PARAM
public void init(javax.servlet.ServletConfig config) throws javax.servlet.ServletException
Initializes servlet and VelocityView used to process requests. Called by the servlet container on loading.
init
in interface javax.servlet.Servlet
init
in class javax.servlet.GenericServlet
config
- servlet configuationjavax.servlet.ServletException
protected String findInitParameter(javax.servlet.ServletConfig config, String key)
config
- servlet configkey
- parameter keyprotected VelocityView getVelocityView()
protected void setVelocityView(VelocityView view)
protected org.slf4j.Logger getLog()
public void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException
doGet
in class javax.servlet.http.HttpServlet
request
- servlet requestresponse
- servlet responsejavax.servlet.ServletException
- from underlying callIOException
- from underlying callpublic void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException
doPost
in class javax.servlet.http.HttpServlet
request
- servlet requestresponse
- servlet responsejavax.servlet.ServletException
- from underlying callIOException
- from underlying callprotected void doRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws IOException
request
- HttpServletRequest object containing client requestresponse
- HttpServletResponse object for the responseIOException
- from underlying processingprotected void initRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws IOException
Request and response initialization. Default version does only one thing: set request POST parameters encoding to Velocity input encoding.
request
- HttpServletRequest object containing client requestresponse
- HttpServletResponse object for the responseIOException
- if thrown by underlying handlingprotected org.apache.velocity.Template handleRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, org.apache.velocity.context.Context ctx)
This was a common extension point, but now it is usually
simpler to override fillContext(org.apache.velocity.context.Context, javax.servlet.http.HttpServletRequest)
to add custom things
to the Context
or override a getTemplate(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
method to change how Template
s are retrieved.
This is only recommended for more complicated use-cases.
request
- client requestresponse
- client responsectx
- VelocityContext to fillprotected org.apache.velocity.context.Context createContext(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
protected void fillContext(org.apache.velocity.context.Context context, javax.servlet.http.HttpServletRequest request)
protected void setContentType(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
The default implementation is :
response.setContentType(getVelocityView().getDefaultContentType());
where defaultContentType is set to the value of the default.contentType
property, or "text/html" if that was not set for the VelocityView
.
request
- servlet request from clientresponse
- servlet reponse to clientprotected org.apache.velocity.Template getTemplate(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
protected org.apache.velocity.Template getTemplate(String name)
protected void mergeTemplate(org.apache.velocity.Template template, org.apache.velocity.context.Context context, javax.servlet.http.HttpServletResponse response) throws IOException
IOException
protected void error(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Throwable e)
request
- original HttpServletRequest from servlet container.response
- HttpServletResponse object from servlet container.e
- Exception that was thrown by some other part of process.protected void manageResourceNotFound(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, org.apache.velocity.exception.ResourceNotFoundException e) throws IOException
ResourceNotFoundException
to send an HTTP 404 result
when needed.request
- The request object.response
- The response object.e
- The exception to check.IOException
- If something goes wrong when sending the HTTP error.protected void requestCleanup(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, org.apache.velocity.context.Context context)
request
- servlet request from clientresponse
- servlet responsecontext
- Context that was merged with the requested templateCopyright © 2002–2018 The Apache Software Foundation. All rights reserved.