By default, the Velocity Servlet Extension is configured to respond to HTTP requests with a content type text/html. Change this request type by setting the default MIME type using dsconfig. For example, the following can be used to set the default type to XML:

$ bin/dsconfig set-http-servlet-extension-prop \
  --extension-name Velocity \
  --set default-mime-type:application/xml

HTML requests can be supported as well as clients that seek content in other formats. Create one or more Velocity template loaders to load templates for other content types like XML or JSON.

The ability to serve multiple formats of a document to clients at the same URL is typically called "content negotiation". HTTP clients indicate the type of content desired using the Accept header. A client may use a header like the following to indicate that they prefer content in XML but will fallback to HTML if necessary:


The following can be used to create a Velocity template loader for XML content:

$ bin/dsconfig create-velocity-template-loader \
  --extension-name Velocity \
  --loader-name XML \
  -–set evaluation-order-index:502 \
  --set mime-type-matcher:application/xml \
  –-set mime-type:application/xml \
  -–set template-suffix:.vm.xml

Upon receiving a request, the Velocity Servlet first creates an ordered list of requested media types from most desired to least based on the value of the Accept header. Starting from the most desired type, it will then iterate over the defined template loaders according to the evaluation-order-index property from lowest value to highest.

A template loader can indicate that it can handle content for requested media type by comparing the requested type to its mime-type-matcher property. A loader can be configured to load templates from a specific directory or load template files having a particular suffix. For example, XML templates are expected to be named using a .vm.xml suffix. If a loader indicates it handles the requested content type and a template exists for the requested view, the template is loaded and used to generate a response to the client. If no loaders are found for the requested media type, the next most preferred media type (if any) is tried. If no loaders indicated that they could satisfy the requested view, the client is sent an HTTP 404 (not found) error. If no loaders could provide acceptable media but the requested view exists in some other format, the client is sent an HTTP 406 (not acceptable) error.

In this example, a template file called hello.vm.xml can be used to generate a response in XML:

For this example, a template file called hello.vm.xml can be used to generate a response in XML:
<hello name=”$”/>
In this case, the response will contain an HTTP Content-Type header with the value of the mime-type property of the Velocity template loader.