Package org.apache.velocity.servlet
Class VelocityServlet
- java.lang.Object
-
- javax.servlet.GenericServlet
-
- javax.servlet.http.HttpServlet
-
- org.apache.velocity.servlet.VelocityServlet
-
- All Implemented Interfaces:
java.io.Serializable,javax.servlet.Servlet,javax.servlet.ServletConfig
public abstract class VelocityServlet extends javax.servlet.http.HttpServletDeprecated.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 thehandleRequest()method, and add your data to the context. Then callgetTemplate("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 :- loadConfiguration() : for setting up the Velocity runtime
- createContext() : for creating and loading the Context
- setContentType() : for changing the content type on a request by request basis
- handleRequest() : you must implement this
- mergeTemplate() : the template rendering process
- requestCleanup() : post rendering resource or other cleanup
- error() : error handling
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.- Author:
- Dave Bryson, Jon S. Stevens, Geir Magnusson Jr., Kent Johnson, Daniel Rall $Id: VelocityServlet.java 463298 2006-10-12 16:10:32Z henning $
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringCONTENT_TYPEDeprecated.The HTTP content type context key.static java.lang.StringDEFAULT_CONTENT_TYPEDeprecated.The default content type for the responsestatic java.lang.StringDEFAULT_OUTPUT_ENCODINGDeprecated.Encoding for the output streamprotected static java.lang.StringINIT_PROPS_KEYDeprecated.This is the string that is looked for when getInitParameter is called (org.apache.velocity.properties).static java.lang.StringREQUESTDeprecated.The context key for the HTTP request object.static java.lang.StringRESPONSEDeprecated.The context key for the HTTP response object.
-
Constructor Summary
Constructors Constructor Description VelocityServlet()Deprecated.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected java.lang.StringchooseCharacterEncoding(javax.servlet.http.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 byresponse.getCharacterEncoding()).protected ContextcreateContext(javax.servlet.http.HttpServletRequest request, javax.servlet.http.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.voiddoGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)Deprecated.Handles HTTPGETrequests by callingdoRequest(HttpServletRequest, HttpServletResponse).voiddoPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)Deprecated.Handles HTTPPOSTrequests by callingdoRequest(HttpServletRequest, HttpServletResponse).protected voiddoRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)Deprecated.Handles all requests (by default).protected voiderror(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.Exception cause)Deprecated.Invoked when there is an error thrown in any part of doRequest() processing.TemplategetTemplate(java.lang.String name)Deprecated.Retrieves the requested template.TemplategetTemplate(java.lang.String name, java.lang.String encoding)Deprecated.Retrieves the requested template with the specified character encoding.protected TemplatehandleRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Context ctx)Deprecated.Implement this method to add your application data to the context, calling thegetTemplate()method to produce your return value.protected TemplatehandleRequest(Context ctx)voidinit(javax.servlet.ServletConfig config)Deprecated.Performs initialization of this servlet.protected voidinitVelocity(javax.servlet.ServletConfig config)Deprecated.Initializes the Velocity runtime, first calling loadConfiguration(ServletConvig) to get a java.util.Properties of configuration information and then calling Velocity.init().protected java.util.PropertiesloadConfiguration(javax.servlet.ServletConfig config)Deprecated.Use VelocityViewServlet from the Velocity Tools library instead.protected voidmergeTemplate(Template template, Context context, javax.servlet.http.HttpServletResponse response)Deprecated.merges the template with the context.protected voidrequestCleanup(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Context context)Deprecated.A cleanup routine which is called at the end of thedoRequest(HttpServletRequest, HttpServletResponse)processing sequence, allowing a derived class to do resource cleanup or other end of process cycle tasks.protected voidsetContentType(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)Deprecated.Sets the content type of the response, defaulting todefaultContentTypeif not overriden.-
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPut, doTrace, getLastModified, service, service
-
-
-
-
Field Detail
-
REQUEST
public static final java.lang.String REQUEST
Deprecated.The context key for the HTTP request object.- See Also:
- Constant Field Values
-
RESPONSE
public static final java.lang.String RESPONSE
Deprecated.The context key for the HTTP response object.- See Also:
- Constant Field Values
-
CONTENT_TYPE
public static final java.lang.String CONTENT_TYPE
Deprecated.The HTTP content type context key.- See Also:
- Constant Field Values
-
DEFAULT_CONTENT_TYPE
public static final java.lang.String DEFAULT_CONTENT_TYPE
Deprecated.The default content type for the response- See Also:
- Constant Field Values
-
DEFAULT_OUTPUT_ENCODING
public static final java.lang.String DEFAULT_OUTPUT_ENCODING
Deprecated.Encoding for the output stream- See Also:
- Constant Field Values
-
INIT_PROPS_KEY
protected static final java.lang.String INIT_PROPS_KEY
Deprecated.This is the string that is looked for when getInitParameter is called (org.apache.velocity.properties).- See Also:
- Constant Field Values
-
-
Method Detail
-
init
public void init(javax.servlet.ServletConfig config) throws javax.servlet.ServletExceptionDeprecated.Performs initialization of this servlet. Called by the servlet container on loading.- Specified by:
initin interfacejavax.servlet.Servlet- Overrides:
initin classjavax.servlet.GenericServlet- Parameters:
config- The servlet configuration to apply.- Throws:
javax.servlet.ServletException
-
initVelocity
protected void initVelocity(javax.servlet.ServletConfig config) throws javax.servlet.ServletExceptionDeprecated.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.- Parameters:
config-- Throws:
javax.servlet.ServletException
-
loadConfiguration
protected java.util.Properties loadConfiguration(javax.servlet.ServletConfig config) throws java.io.IOException, java.io.FileNotFoundExceptionDeprecated.Use VelocityViewServlet from the Velocity Tools library instead.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.- 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:
java.io.FileNotFoundException- if a specified file is not found.java.io.IOException- I/O problem accessing the specified file, if specified.
-
doGet
public void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOExceptionDeprecated.Handles HTTPGETrequests by callingdoRequest(HttpServletRequest, HttpServletResponse).- Overrides:
doGetin classjavax.servlet.http.HttpServlet- Parameters:
request-response-- Throws:
javax.servlet.ServletExceptionjava.io.IOException
-
doPost
public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOExceptionDeprecated.Handles HTTPPOSTrequests by callingdoRequest(HttpServletRequest, HttpServletResponse).- Overrides:
doPostin classjavax.servlet.http.HttpServlet- Parameters:
request-response-- Throws:
javax.servlet.ServletExceptionjava.io.IOException
-
doRequest
protected void doRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOExceptionDeprecated.Handles all requests (by default).- Parameters:
request- HttpServletRequest object containing client requestresponse- HttpServletResponse object for the response- Throws:
javax.servlet.ServletExceptionjava.io.IOException
-
requestCleanup
protected void requestCleanup(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Context context)Deprecated.A cleanup routine which is called at the end of thedoRequest(HttpServletRequest, HttpServletResponse)processing sequence, allowing a derived class to do resource cleanup or other end of process cycle tasks.- Parameters:
request- servlet request from clientresponse- servlet reponsecontext- context created by the createContext() method
-
mergeTemplate
protected void mergeTemplate(Template template, Context context, javax.servlet.http.HttpServletResponse response) throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, java.io.IOException, java.io.UnsupportedEncodingException, java.lang.Exception
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 :)- Parameters:
template- template object returned by the handleRequest() methodcontext- context created by the createContext() methodresponse- servlet reponse (use this to get the output stream or Writer- Throws:
ResourceNotFoundExceptionParseErrorExceptionMethodInvocationExceptionjava.io.IOExceptionjava.io.UnsupportedEncodingExceptionjava.lang.Exception
-
setContentType
protected void setContentType(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)Deprecated.Sets the content type of the response, defaulting todefaultContentTypeif not overriden. Delegates tochooseCharacterEncoding(HttpServletRequest)to select the appropriate character encoding.- Parameters:
request- The servlet request from the client.response- The servlet reponse to the client.
-
chooseCharacterEncoding
protected java.lang.String chooseCharacterEncoding(javax.servlet.http.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 byresponse.getCharacterEncoding()). Called bysetContentType(HttpServletRequest, HttpServletResponse)if an encoding isn't already specified by Content-Type. By default, chooses the value of RuntimeSingleton'soutput.encodingproperty.- Parameters:
request- The servlet request from the client.- Returns:
- The chosen character encoding.
-
createContext
protected Context createContext(javax.servlet.http.HttpServletRequest request, javax.servlet.http.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.- Parameters:
request- servlet request from clientresponse- servlet reponse to client- Returns:
- context
-
getTemplate
public Template getTemplate(java.lang.String name) throws ResourceNotFoundException, ParseErrorException, java.lang.Exception
Deprecated.Retrieves the requested template.- 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.java.lang.Exception- if an error occurs in template initialization
-
getTemplate
public Template getTemplate(java.lang.String name, java.lang.String encoding) throws ResourceNotFoundException, ParseErrorException, java.lang.Exception
Deprecated.Retrieves the requested template with the specified character encoding.- 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.java.lang.Exception- if an error occurs in template initialization- Since:
- Velocity v1.1
-
handleRequest
protected Template handleRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Context ctx) throws java.lang.Exception
Deprecated.Implement this method to add your application data to the context, calling thegetTemplate()method to produce your return value.
In the event of a problem, you may handle the request directly and returnnullor throw a more meaningful exception for the error handler to catch.- Parameters:
request- servlet request from clientresponse- servlet reponsectx- The context to add your data to.- Returns:
- The template to merge with your context or null, indicating that you handled the processing.
- Throws:
java.lang.Exception- Since:
- Velocity v1.1
-
handleRequest
protected Template handleRequest(Context ctx) throws java.lang.Exception
Deprecated.Implement this method to add your application data to the context, calling thegetTemplate()method to produce your return value.
In the event of a problem, you may simple returnnullor throw a more meaningful exception.- Parameters:
ctx- The context to add your data to.- Returns:
- The template to merge with your context.
- Throws:
java.lang.Exception
-
error
protected void error(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.Exception cause) throws javax.servlet.ServletException, java.io.IOExceptionDeprecated.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.response- HttpServletResponse object from servlet container.cause- Exception that was thrown by some other part of process.- Throws:
javax.servlet.ServletExceptionjava.io.IOException
-
-