org.apache.velocity.tools.view
Class VelocityViewServlet

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by org.apache.velocity.tools.view.VelocityViewServlet
All Implemented Interfaces:
java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig
Direct Known Subclasses:
VelocityLayoutServlet, VelocityViewServlet

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:

org.apache.velocity.tools
Path and name of the toolbox configuration file. The path must be relative to the web application root directory. If this parameter is not found, the servlet will check for a toolbox file at '/WEB-INF/tools.xml'.
org.apache.velocity.properties
Path and name of the Velocity configuration file. The path must be relative to the web application root directory. If this parameter is not present, Velocity will check for a properties file at '/WEB-INF/velocity.properties'. If no file is found there, then Velocity is initialized with the settings in the classpath at 'org.apache.velocity.tools.view.velocity.properties'.
org.apache.velocity.tools.shared.config
By default, this is true. If set to false, then the VelocityView used by this servlet will not be shared with VelocityViewFilters, other VelocityViewServlets or VelocityViewTags in the application.
org.apache.velocity.tools.loadDefaults
By default, this is true. If set to false, then the default toolbox configuration will not be added to your (if any) custom configuration. NOTE: The default configuration will also be suppressed if you are using a deprecated toolbox.xml format and do not explicitly set this to true.
org.apache.velocity.tools.cleanConfiguration
By default, this is 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
org.apache.velocity.tools.bufferOutput
By default, the processed templates are merged directly into the 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"

Version:
$Id: VelocityViewServlet.java 791530 2009-07-06 16:06:52Z nbubna $
See Also:
Serialized Form

Field Summary
static java.lang.String BUFFER_OUTPUT_PARAM
           
private  boolean bufferOutput
           
private static long serialVersionUID
           
private  VelocityView view
           
 
Constructor Summary
VelocityViewServlet()
           
 
Method Summary
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, java.lang.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  java.lang.String findInitParameter(javax.servlet.ServletConfig config, java.lang.String key)
          Looks up an init parameter with the specified key in either the ServletConfig or, failing that, in the ServletContext.
protected  org.apache.velocity.runtime.log.Log getLog()
           
protected  org.apache.velocity.Template getTemplate(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
           
protected  org.apache.velocity.Template getTemplate(java.lang.String name)
           
protected  java.lang.String getVelocityProperty(java.lang.String name, java.lang.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 Templates are retrieved.
 void init(javax.servlet.ServletConfig config)
          Initializes servlet and VelocityView used to process requests.
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)
           
 
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

BUFFER_OUTPUT_PARAM

public static final java.lang.String BUFFER_OUTPUT_PARAM
See Also:
Constant Field Values

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

view

private transient VelocityView view

bufferOutput

private boolean bufferOutput
Constructor Detail

VelocityViewServlet

public VelocityViewServlet()
Method Detail

init

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.

Specified by:
init in interface javax.servlet.Servlet
Overrides:
init in class javax.servlet.GenericServlet
Parameters:
config - servlet configuation
Throws:
javax.servlet.ServletException

findInitParameter

protected java.lang.String findInitParameter(javax.servlet.ServletConfig config,
                                             java.lang.String key)
Looks up an init parameter with the specified key in either the ServletConfig or, failing that, in the ServletContext.


getVelocityView

protected VelocityView getVelocityView()

setVelocityView

protected void setVelocityView(VelocityView view)

getVelocityProperty

protected java.lang.String getVelocityProperty(java.lang.String name,
                                               java.lang.String alternate)

getLog

protected org.apache.velocity.runtime.log.Log getLog()

doGet

public void doGet(javax.servlet.http.HttpServletRequest request,
                  javax.servlet.http.HttpServletResponse response)
           throws javax.servlet.ServletException,
                  java.io.IOException
Handles GET - calls doRequest()

Overrides:
doGet in class javax.servlet.http.HttpServlet
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
Handle a POST request - calls doRequest()

Overrides:
doPost in class javax.servlet.http.HttpServlet
Throws:
javax.servlet.ServletException
java.io.IOException

doRequest

protected void doRequest(javax.servlet.http.HttpServletRequest request,
                         javax.servlet.http.HttpServletResponse response)
                  throws java.io.IOException
Handles with both GET and POST requests

Parameters:
request - HttpServletRequest object containing client request
response - HttpServletResponse object for the response
Throws:
java.io.IOException

handleRequest

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 Templates are retrieved. This is only recommended for more complicated use-cases.

Parameters:
request - client request
response - client response
ctx - VelocityContext to fill
Returns:
Velocity Template object or null

createContext

protected org.apache.velocity.context.Context createContext(javax.servlet.http.HttpServletRequest request,
                                                            javax.servlet.http.HttpServletResponse response)

fillContext

protected void fillContext(org.apache.velocity.context.Context context,
                           javax.servlet.http.HttpServletRequest request)

setContentType

protected void setContentType(javax.servlet.http.HttpServletRequest request,
                              javax.servlet.http.HttpServletResponse response)
Sets the content type of the response. This is available to be overriden by a derived class.

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.

Parameters:
request - servlet request from client
response - servlet reponse to client

getTemplate

protected org.apache.velocity.Template getTemplate(javax.servlet.http.HttpServletRequest request,
                                                   javax.servlet.http.HttpServletResponse response)

getTemplate

protected org.apache.velocity.Template getTemplate(java.lang.String name)

mergeTemplate

protected void mergeTemplate(org.apache.velocity.Template template,
                             org.apache.velocity.context.Context context,
                             javax.servlet.http.HttpServletResponse response)
                      throws java.io.IOException
Throws:
java.io.IOException

error

protected void error(javax.servlet.http.HttpServletRequest request,
                     javax.servlet.http.HttpServletResponse response,
                     java.lang.Throwable e)
Invoked when there is an error thrown in any part of doRequest() processing.

Default will send a simple HTML response indicating there was a problem.

Parameters:
request - original HttpServletRequest from servlet container.
response - HttpServletResponse object from servlet container.
e - Exception that was thrown by some other part of process.

manageResourceNotFound

protected void manageResourceNotFound(javax.servlet.http.HttpServletRequest request,
                                      javax.servlet.http.HttpServletResponse response,
                                      org.apache.velocity.exception.ResourceNotFoundException e)
                               throws java.io.IOException
Manages the ResourceNotFoundException to send an HTTP 404 result when needed.

Parameters:
request - The request object.
response - The response object.
e - The exception to check.
Throws:
java.io.IOException - If something goes wrong when sending the HTTP error.

requestCleanup

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. This default implementation does nothing.

Parameters:
request - servlet request from client
response - servlet response
context - Context that was merged with the requested template


Copyright (c) 2003-2007 Apache Software Foundation