| javax.servlet.http.HttpServlet org.apache.velocity.tools.view.servlet.VelocityViewServlet
All known Subclasses: org.apache.velocity.tools.view.servlet.VelocityLayoutServlet,
VelocityViewServlet | public class VelocityViewServlet extends HttpServlet (Code) | | A servlet to process Velocity templates. This is comparable to the
the JspServlet for JSP-based applications.
The servlet provides the following features:
- renders Velocity templates
- provides support for an auto-loaded, configurable toolbox
- provides transparent access to the servlet request attributes,
servlet session attributes and servlet context attributes by
auto-searching them
- logs to the logging facility of the servlet API
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, the servlet will check for a toolbox file at
'/WEB-INF/toolbox.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.servlet.velocity.properties'.
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 :
author: Dave Bryson author: Jon S. Stevens author: Gabe Sidler author: Geir Magnusson Jr. author: Kent Johnson author: Daniel Rall author: Nathan Bubna version: $Id: VelocityViewServlet.java 488468 2006-12-19 00:19:30Z nbubna $ |
Method Summary | |
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) | public void | doPost(HttpServletRequest request, HttpServletResponse response) | protected void | doRequest(HttpServletRequest request, HttpServletResponse response) | protected void | error(HttpServletRequest request, HttpServletResponse response, Exception e) Invoked when there is an error thrown in any part of doRequest() processing. | 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) 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. | public Template | getTemplate(String name) Retrieves the requested template.
Parameters: name - The file name of the template to retrieve relative to thetemplate root. | public Template | getTemplate(String name, String encoding) 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. | protected Template | handleRequest(HttpServletRequest request, HttpServletResponse response, Context ctx) | public void | init(ServletConfig config) Initializes servlet, toolbox and Velocity template engine. | protected void | initToolbox(ServletConfig config) Initializes the ServletToolboxManager for this servlet's
toolbox (if any). | protected void | initVelocity(ServletConfig config) Initializes the Velocity runtime, first calling
loadConfiguration(ServletConfig) to get a
org.apache.commons.collections.ExtendedProperties
of configuration information
and then calling velocityEngine.init(). | protected ExtendedProperties | loadConfiguration(ServletConfig config) Loads the configuration information and returns that
information as an ExtendedProperties, which will be used to
initialize the Velocity runtime. | protected void | mergeTemplate(Template template, Context context, HttpServletResponse response) Merges the template with the context. | protected void | performMerge(Template template, Context context, Writer writer) This is here so developers may override it and gain access to the
Writer which the template will be merged into. | 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. | protected void | setContentType(HttpServletRequest request, HttpServletResponse response) Sets the content type of the response. | protected void | setVelocityEngine(VelocityEngine ve) |
CONTENT_TYPE | final public static String CONTENT_TYPE(Code) | | The HTTP content type context key.
|
DEFAULT_CONTENT_TYPE | final public static String DEFAULT_CONTENT_TYPE(Code) | | The default content type for the response
|
DEFAULT_OUTPUT_ENCODING | final public static String DEFAULT_OUTPUT_ENCODING(Code) | | Default encoding for the output stream
|
DEFAULT_PROPERTIES_PATH | final protected static String DEFAULT_PROPERTIES_PATH(Code) | | Default velocity properties file path. If no alternate value for
this is specified, the servlet will look here.
|
DEFAULT_TOOLBOX_PATH | final protected static String DEFAULT_TOOLBOX_PATH(Code) | | Default toolbox configuration file path. If no alternate value for
this is specified, the servlet will look here.
|
DEFAULT_TOOLS_PROPERTIES | final public static String DEFAULT_TOOLS_PROPERTIES(Code) | | Default Runtime properties.
|
INIT_PROPS_KEY | final protected static String INIT_PROPS_KEY(Code) | | This is the string that is looked for when getInitParameter is
called ("org.apache.velocity.properties").
|
SERVLET_CONTEXT_KEY | final public static String SERVLET_CONTEXT_KEY(Code) | | Key used to access the ServletContext in
the Velocity application attributes.
|
TOOLBOX_KEY | final protected static String TOOLBOX_KEY(Code) | | Key used to access the toolbox configuration file path from the
Servlet or webapp init parameters ("org.apache.velocity.toolbox").
|
createContext | protected Context createContext(HttpServletRequest request, HttpServletResponse response)(Code) | | Creates and returns an initialized Velocity context.
A new context of class
ChainedContext is created and
initialized.
Parameters: request - servlet request from client Parameters: response - servlet reponse to client |
error | protected void error(HttpServletRequest request, HttpServletResponse response, Exception e) throws ServletException(Code) | | 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. Parameters: response - HttpServletResponse object from servlet container. Parameters: e - Exception that was thrown by some other part of process. |
findInitParameter | protected String findInitParameter(ServletConfig config, String key)(Code) | | Looks up an init parameter with the specified key in either the
ServletConfig or, failing that, in the ServletContext.
|
getResponseWriter | protected Writer getResponseWriter(HttpServletResponse response) throws UnsupportedEncodingException, IOException(Code) | | 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.
Parameters: response - The response. A Writer , possibly created using thegetOutputStream() . |
getVelocityEngine | protected VelocityEngine getVelocityEngine()(Code) | | Returns the underlying VelocityEngine being used.
|
init | public void init(ServletConfig config) throws ServletException(Code) | | 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.
Parameters: config - servlet configuation |
initToolbox | protected void initToolbox(ServletConfig config) throws ServletException(Code) | | Initializes the ServletToolboxManager for this servlet's
toolbox (if any).
Parameters: config - servlet configuation |
initVelocity | protected void initVelocity(ServletConfig config) throws ServletException(Code) | | 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.
Parameters: config - servlet configuration parameters |
loadConfiguration | protected ExtendedProperties loadConfiguration(ServletConfig config) throws IOException(Code) | | 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.
Parameters: config - ServletConfig passed to the servlets init() functionCan be used to access the real path via ServletContext (hint) ExtendedProperties loaded with configuration values to be usedto initialize the Velocity runtime. throws: IOException - I/O problem accessing the specified file, if specified. |
requestCleanup | protected void requestCleanup(HttpServletRequest request, HttpServletResponse response, Context context)(Code) | | 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 Parameters: response - servlet reponse Parameters: context - Context created by the VelocityViewServlet.createContext |
setContentType | protected void setContentType(HttpServletRequest request, HttpServletResponse response)(Code) | | 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.
Parameters: request - servlet request from client Parameters: response - servlet reponse to client |
setVelocityEngine | protected void setVelocityEngine(VelocityEngine ve)(Code) | | Sets the underlying VelocityEngine
|
Methods inherited from javax.servlet.http.HttpServlet | protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException(Code)(Java Doc) protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException(Code)(Java Doc) protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException(Code)(Java Doc) protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException(Code)(Java Doc) protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException(Code)(Java Doc) protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException(Code)(Java Doc) protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException(Code)(Java Doc) protected long getLastModified(HttpServletRequest req)(Code)(Java Doc) protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException(Code)(Java Doc) public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException(Code)(Java Doc)
|
|
|