org.apache.velocity.servlet
public abstract class VelocityServlet extends HttpServlet
handleRequest() method,
and add your data to the context. Then call
getTemplate("myTemplate.wm").
This class puts some things into the context object that you should
be aware of:
"req" - The HttpServletRequest object "res" - The HttpServletResponse objectThere are other methods you can override to access, alter or control any part of the request processing chain. Please see the javadocs for more information on :
| Field Summary | |
|---|---|
| static String | CONTENT_TYPE
The HTTP content type context key. |
| static String | defaultContentType
The default content type, itself defaulting to DEFAULT_CONTENT_TYPE if not configured. |
| static String | DEFAULT_CONTENT_TYPE
The default content type for the response |
| static String | DEFAULT_OUTPUT_ENCODING
Encoding for the output stream |
| protected static String | INIT_PROPS_KEY
This is the string that is looked for when getInitParameter is
called ( org.apache.velocity.properties). |
| static String | OLD_INIT_PROPS_KEY
Use of this properties key has been deprecated, and will be
removed in Velocity version 1.5. |
| static String | REQUEST
The context key for the HTTP request object. |
| static String | RESPONSE
The context key for the HTTP response object. |
| static SimplePool | writerPool
Cache of writers |
| Method Summary | |
|---|---|
| protected String | chooseCharacterEncoding(HttpServletRequest request)
Chooses the output character encoding to be used as the value
for the "charset=" portion of the HTTP Content-Type header (and
thus returned by response.getCharacterEncoding()).
|
| protected Context | createContext(HttpServletRequest request, HttpServletResponse response)
Returns a context suitable to pass to the handleRequest() method
Default implementation will create a VelocityContext object, put the HttpServletRequest and HttpServletResponse into the context accessable via the keys VelocityServlet.REQUEST and VelocityServlet.RESPONSE, respectively. |
| void | doGet(HttpServletRequest request, HttpServletResponse response)
Handles HTTP GET requests by calling VelocityServlet. |
| void | doPost(HttpServletRequest request, HttpServletResponse response)
Handles HTTP POST requests by calling VelocityServlet. |
| protected void | doRequest(HttpServletRequest request, HttpServletResponse response)
Handles all requests (by default).
|
| protected void | error(HttpServletRequest request, HttpServletResponse response, Exception cause)
Invoked when there is an error thrown in any part of doRequest() processing.
|
| Template | getTemplate(String name)
Retrieves the requested template.
|
| Template | getTemplate(String name, String encoding)
Retrieves the requested template with the specified
character encoding.
|
| protected Template | handleRequest(HttpServletRequest request, HttpServletResponse response, Context ctx)
Implement this method to add your application data to the context,
calling the getTemplate() method to produce your return
value.
|
| protected Template | handleRequest(Context ctx)
Implement this method to add your application data to the context,
calling the getTemplate() method to produce your return
value.
|
| void | init(ServletConfig config)
Performs initialization of this servlet. |
| protected void | initVelocity(ServletConfig config)
Initializes the Velocity runtime, first calling
loadConfiguration(ServletConvig) to get a
java.util.Properties of configuration information
and then calling Velocity.init(). |
| protected Properties | loadConfiguration(ServletConfig config)
Loads the configuration information and returns that
information as a Properties, 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 | requestCleanup(HttpServletRequest request, HttpServletResponse response, Context context)
A cleanup routine which is called at the end of the VelocityServlet
processing sequence, allowing 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, defaulting to defaultContentType if not overriden. |
org.apache.velocity.properties).response.getCharacterEncoding()).
Called by VelocityServlet if an encoding isn't already specified by
Content-Type. By default, chooses the value of
RuntimeSingleton's output.encoding property.
Parameters: request The servlet request from the client.
Parameters: request servlet request from client response servlet reponse to client
Returns: context
GET requests by calling VelocityServlet.POST requests by calling VelocityServlet.Parameters: request HttpServletRequest object containing client request response HttpServletResponse object for the response
Parameters: request original HttpServletRequest from servlet container. response HttpServletResponse object from servlet container. cause Exception that was thrown by some other part of process.
Parameters: name The file name of the template to retrieve relative to the template root.
Returns: The requested template.
Throws: ResourceNotFoundException if template not found from any available source. ParseErrorException if template cannot be parsed due to syntax (or other) error. Exception if an error occurs in template initialization
Parameters: name The file name of the template to retrieve relative to the template root. encoding the character encoding of the template
Returns: The requested template.
Throws: ResourceNotFoundException if template not found from any available source. ParseErrorException if template cannot be parsed due to syntax (or other) error. Exception if an error occurs in template initialization
Since: Velocity v1.1
getTemplate() method to produce your return
value.
null or throw a more meaningful exception
for the error handler to catch.
Parameters: request servlet request from client response servlet reponse ctx The context to add your data to.
Returns: The template to merge with your context or null, indicating that you handled the processing.
Since: Velocity v1.1
Deprecated: Use VelocityServlet
Implement this method to add your application data to the context, calling thegetTemplate() method to produce your return
value.
null
or throw a more meaningful exception.
Parameters: ctx The context to add your data to.
Returns: The template to merge with your context.
Parameters: config The servlet configuration to apply.
Throws: ServletException
<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 :
Properties p = super.loadConfiguration( config );
and then add or modify the configuration values from the file.
Parameters: config ServletConfig passed to the servlets init() function Can be used to access the real path via ServletContext (hint)
Returns: java.util.Properties loaded with configuration values to be used to initialize the Velocity runtime.
Throws: FileNotFoundException if a specified file is not found. IOException I/O problem accessing the specified file, if specified.
Parameters: template template object returned by the handleRequest() method context context created by the createContext() method response servlet reponse (use this to get the output stream or Writer
Parameters: request servlet request from client response servlet reponse context context created by the createContext() method
Parameters: request The servlet request from the client. response The servlet reponse to the client.