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

All Implemented Interfaces:
    Serializable, Servlet, ServletConfig

Direct Known Subclasses:
    ControllerServlet, MockVelocityServlet

Deprecated! This - servlet has been replaced by VelocityViewServlet, available from the Velocity-Tools sub-project. VelocityViewServlet provides support for quick, clean MVC web development. VelocityServlet will be removed in a future version of Velocity.

Base class which simplifies the use of Velocity with Servlets. Extend this class, implement the 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 object
There 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 :
If you put a String with key "contentType" object into the context within either your servlet or within your template, then that will be used to override the default content type specified in the properties file.
Field Summary
public static final  String REQUEST    The context key for the HTTP request object. 
public static final  String RESPONSE    The context key for the HTTP response object. 
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    Encoding for the output stream 
protected static final  String INIT_PROPS_KEY    This is the string that is looked for when getInitParameter is called (org.apache.velocity.properties). 
Method from org.apache.velocity.servlet.VelocityServlet Summary:
chooseCharacterEncoding,   createContext,   doGet,   doPost,   doRequest,   error,   getTemplate,   getTemplate,   handleRequest,   handleRequest,   init,   initVelocity,   loadConfiguration,   mergeTemplate,   requestCleanup,   setContentType
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.servlet.VelocityServlet Detail:
 protected String chooseCharacterEncoding(HttpServletRequest request) 
      Deprecated!
    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()). Called by #setContentType(HttpServletRequest, HttpServletResponse) if an encoding isn't already specified by Content-Type. By default, chooses the value of RuntimeSingleton's output.encoding property.
 protected Context createContext(HttpServletRequest request,
    HttpServletResponse response) 
      Deprecated!
    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.
 public  void doGet(HttpServletRequest request,
    HttpServletResponse response) throws IOException, ServletException 
      Deprecated!
 public  void doPost(HttpServletRequest request,
    HttpServletResponse response) throws IOException, ServletException 
      Deprecated!
 protected  void doRequest(HttpServletRequest request,
    HttpServletResponse response) throws IOException, ServletException 
      Deprecated!
    Handles all requests (by default).
 protected  void error(HttpServletRequest request,
    HttpServletResponse response,
    Exception cause) throws IOException, ServletException 
      Deprecated!
    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.
 public Template getTemplate(String name) throws Exception, ResourceNotFoundException, ParseErrorException 
      Deprecated!
    Retrieves the requested template.
 public Template getTemplate(String name,
    String encoding) throws Exception, ResourceNotFoundException, ParseErrorException 
      Deprecated!
    Retrieves the requested template with the specified character encoding.
 protected Template handleRequest(Context ctx) throws Exception 
Deprecated! Use - #handleRequest( HttpServletRequest request, HttpServletResponse response, Context ctx )

      Deprecated!
    Implement this method to add your application data to the context, calling the getTemplate() method to produce your return value.

    In the event of a problem, you may simple return null or throw a more meaningful exception.
 protected Template handleRequest(HttpServletRequest request,
    HttpServletResponse response,
    Context ctx) throws Exception 
      Deprecated!
    Implement this method to add your application data to the context, calling the getTemplate() method to produce your return value.

    In the event of a problem, you may handle the request directly and return null or throw a more meaningful exception for the error handler to catch.
 public  void init(ServletConfig config) throws ServletException 
      Deprecated!
    Performs initialization of this servlet. Called by the servlet container on loading.
 protected  void initVelocity(ServletConfig config) throws ServletException 
      Deprecated!
    Initializes the Velocity runtime, first calling loadConfiguration(ServletConvig) to get a java.util.Properties of configuration information and then calling Velocity.init(). Override this to do anything to the environment before the initialization of the singelton takes place, or to initialize the singleton in other ways.
 protected Properties loadConfiguration(ServletConfig config) throws IOException, FileNotFoundException 
Deprecated! Use - VelocityViewServlet from the Velocity Tools library instead.

      Deprecated!
    Loads the configuration information and returns that information as a Properties, 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 :
    Properties 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 
      Deprecated!
    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 requestCleanup(HttpServletRequest request,
    HttpServletResponse response,
    Context context) 
      Deprecated!
 protected  void setContentType(HttpServletRequest request,
    HttpServletResponse response) 
      Deprecated!