Save This Page
Home » velocity-1.5 » org.apache.velocity.tools » view » servlet » [javadoc | source]
org.apache.velocity.tools.view.servlet
public class: VelocityViewServlet [javadoc | source]
java.lang.Object
   javax.servlet.GenericServlet
      javax.servlet.http.HttpServlet
         org.apache.velocity.tools.view.servlet.VelocityViewServlet

All Implemented Interfaces:
    Serializable, Servlet, ServletConfig

Direct Known Subclasses:
    VelocityLayoutServlet

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.toolbox
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, no toolbox is instantiated.
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 is initialized with default settings.

There are methods you may wish to override to access, alter or control any part of the request processing chain. Please see the javadocs for more information on :

Field Summary
public static final  String CONTENT_TYPE    The HTTP content type context key. 
public static final  String DEFAULT_CONTENT_TYPE    The default content type for the response 
public static final  String DEFAULT_OUTPUT_ENCODING    Default encoding for the output stream 
public static final  String SERVLET_CONTEXT_KEY    Key used to access the ServletContext in the Velocity application attributes. 
protected static final  String TOOLBOX_KEY    Key used to access the toolbox configuration file path from the Servlet or webapp init parameters ("org.apache.velocity.toolbox"). 
protected static final  String INIT_PROPS_KEY    This is the string that is looked for when getInitParameter is called ("org.apache.velocity.properties"). 
protected  ToolboxManager toolboxManager    A reference to the toolbox manager. 
Method from org.apache.velocity.tools.view.servlet.VelocityViewServlet Summary:
createContext,   doGet,   doPost,   doRequest,   error,   findInitParameter,   getResponseWriter,   getTemplate,   getTemplate,   getVelocityEngine,   getVelocityProperty,   handleRequest,   init,   initToolbox,   initVelocity,   loadConfiguration,   mergeTemplate,   performMerge,   requestCleanup,   setContentType,   setVelocityEngine
Methods from javax.servlet.http.HttpServlet:
service
Methods from javax.servlet.GenericServlet:
destroy,   getInitParameter,   getInitParameterNames,   getServletConfig,   getServletContext,   getServletInfo,   getServletName,   init,   init,   log,   log,   service
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.apache.velocity.tools.view.servlet.VelocityViewServlet Detail:
 protected Context createContext(HttpServletRequest request,
    HttpServletResponse response) 

    Creates and returns an initialized Velocity context.

    A new context of class ChainedContext is created and initialized.
 public  void doGet(HttpServletRequest request,
    HttpServletResponse response) throws IOException, ServletException 
    Handles GET - calls doRequest()
 public  void doPost(HttpServletRequest request,
    HttpServletResponse response) throws IOException, ServletException 
    Handle a POST request - calls doRequest()
 protected  void doRequest(HttpServletRequest request,
    HttpServletResponse response) throws IOException, ServletException 
    Handles with both GET and POST requests
 protected  void error(HttpServletRequest request,
    HttpServletResponse response,
    Exception e) throws ServletException 
    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.
 protected String findInitParameter(ServletConfig config,
    String key) 
    Looks up an init parameter with the specified key in either the ServletConfig or, failing that, in the ServletContext.
 protected Writer getResponseWriter(HttpServletResponse response) throws IOException, UnsupportedEncodingException 

    Procure a Writer with correct encoding which can be used even if HttpServletResponse's getOutputStream() method has already been called.

    This is a transitional method which will be removed in a future version of Velocity. It is not recommended that you override this method.

 public Template getTemplate(String name) throws Exception, ResourceNotFoundException, ParseErrorException 
    Retrieves the requested template.
 public Template getTemplate(String name,
    String encoding) throws Exception, ResourceNotFoundException, ParseErrorException 
    Retrieves the requested template with the specified character encoding.
 protected VelocityEngine getVelocityEngine() 
    Returns the underlying VelocityEngine being used.
 protected String getVelocityProperty(String key,
    String alternate) 
    Simplifies process of getting a property from VelocityEngine, because the VelocityEngine interface sucks compared to the singleton's. Use of this method assumes that #initVelocity(ServletConfig) has already been called.
 protected Template handleRequest(HttpServletRequest request,
    HttpServletResponse response,
    Context ctx) throws Exception 

    Handle the template processing request.

 public  void init(ServletConfig config) throws ServletException 

    Initializes servlet, toolbox and Velocity template engine. Called by the servlet container on loading.

    NOTE: If no charset is specified in the default.contentType property (in your velocity.properties) and you have specified an output.encoding property, then that will be used as the charset for the default content-type of pages served by this servlet.

 protected  void initToolbox(ServletConfig config) throws ServletException 
    Initializes the ServletToolboxManager for this servlet's toolbox (if any).
 protected  void initVelocity(ServletConfig config) throws ServletException 
    Initializes the Velocity runtime, first calling loadConfiguration(ServletConfig) to get a org.apache.commons.collections.ExtendedProperties of configuration information and then calling velocityEngine.init(). Override this to do anything to the environment before the initialization of the singleton takes place, or to initialize the singleton in other ways.
 protected ExtendedProperties loadConfiguration(ServletConfig config) throws IOException 
    Loads the configuration information and returns that information as an ExtendedProperties, which will be used to initialize the Velocity runtime.

    Currently, this method gets the initialization parameter VelocityServlet.INIT_PROPS_KEY, which should be a file containing the configuration information.

    To configure your Servlet Spec 2.2 compliant servlet runner to pass this to you, put the following in your WEB-INF/web.xml file
    <servlet>
    <servlet-name> YourServlet </servlet-name>
    <servlet-class> your.package.YourServlet </servlet-class>
    <init-param>
    <param-name> org.apache.velocity.properties </param-name>
    <param-value> velocity.properties </param-value>
    </init-param>
    </servlet>
    
    Alternately, if you wish to configure an entire context in this fashion, you may use the following:
    <context-param>
    <param-name> org.apache.velocity.properties </param-name>
    <param-value> velocity.properties </param-value>
    <description> Path to Velocity configuration </description>
    </context-param>
    
    Derived classes may do the same, or take advantage of this code to do the loading for them via :
    ExtendedProperties p = super.loadConfiguration(config);
    
    and then add or modify the configuration values from the file.
 protected  void mergeTemplate(Template template,
    Context context,
    HttpServletResponse response) throws Exception, IOException, MethodInvocationException, ResourceNotFoundException, UnsupportedEncodingException, ParseErrorException 
    Merges the template with the context. Only override this if you really, really really need to. (And don't call us with questions if it breaks :)
 protected  void performMerge(Template template,
    Context context,
    Writer writer) throws Exception, MethodInvocationException, ResourceNotFoundException, ParseErrorException 
    This is here so developers may override it and gain access to the Writer which the template will be merged into. See VELTOOLS-7 for discussion of this.
 protected  void requestCleanup(HttpServletRequest request,
    HttpServletResponse response,
    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.
 protected  void setContentType(HttpServletRequest request,
    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(defaultContentType);
    
    
    where defaultContentType is set to the value of the default.contentType property, or "text/html" if that is not set.

 protected  void setVelocityEngine(VelocityEngine ve) 
    Sets the underlying VelocityEngine