Class AbstractHttpServlet
- All Implemented Interfaces:
- ResourceConnector,- java.io.Serializable,- Servlet,- ServletConfig
- Direct Known Subclasses:
- GroovyServlet,- TemplateServlet
public abstract class AbstractHttpServlet extends HttpServlet implements ResourceConnector
Resource name mangling (pattern replacement)
 Also implements Groovy's ResourceConnector in a dynamic
 manner. It allows you to modify the resource name that is searched for with a
 replace all operation. See Pattern and
 Matcher for details.
 The servlet init parameter names are:
 
"resource.name.regex" = empty - defaults to null resource.name.replacement = empty - defaults to null resource.name.replace.all = true (default) | false means replaceFirst()Note: If you specify a regex, you have to specify a replacement string too! Otherwise an exception gets raised.
Logging and bug-hunting options
This implementation provides a verbosity flag switching log statements. The servlet init parameter name is:
verbose = false(default) | true
In order to support class-loading-troubles-debugging with Tomcat 4 or higher, you can log the class loader responsible for loading some classes. See GROOVY-861 for details. The servlet init parameter name is:
log.GROOVY861 = false(default) | true
If you experience class-loading-troubles with Tomcat 4 (or higher) or any other servlet container using custom class loader setups, you can fallback to use (slower) reflection in Groovy's MetaClass implementation. Please contact the dev team with your problem! Thanks. The servlet init parameter name is:
reflection = false(default) | true
- See Also:
- Serialized Form
- 
Field SummaryFields Modifier and Type Field Description static java.lang.StringCONTENT_TYPE_TEXT_HTMLContent type of the HTTP response.protected java.lang.StringencodingEncoding to use, becomes charset part of contentType.static java.lang.StringINC_PATH_INFOServlet API include key name: path_infostatic java.lang.StringINC_REQUEST_URIstatic java.lang.StringINC_SERVLET_PATHServlet API include key name: servlet_pathstatic java.lang.StringINIT_PARAM_RESOURCE_NAME_REGEXstatic java.lang.StringINIT_PARAM_RESOURCE_NAME_REGEX_FLAGSprotected java.lang.StringnamePrefixa.fink: it was inremoveNamePrefix(java.lang.String), but was extracted to var for optimizationprotected booleanreflectionMirrors the static value of the reflection flag in MetaClass.protected java.util.regex.PatternresourceNamePatternEithernullor a compiled pattern read from ""resource.name.regex"" and used ingetScriptUri(HttpServletRequest).protected booleanresourceNameReplaceAllThe replace method to use on the matcher.protected java.lang.StringresourceNameReplacementThe replacement used by the resource name matcher.protected ServletContextservletContextServlet (or the web application) context.protected booleanverboseControls almost all log output.
- 
Constructor SummaryConstructors Constructor Description AbstractHttpServlet()Initializes all fields with default values.
- 
Method SummaryModifier and Type Method Description protected java.lang.StringapplyResourceNameMatcher(java.lang.String uri)protected voidgenerateNamePrefixOnce()java.net.URLConnectiongetResourceConnection(java.lang.String name)Interface method for ResourceContainer.protected java.lang.StringgetScriptUri(HttpServletRequest request)Returns the include-aware uri of the script or template file.protected java.io.FilegetScriptUriAsFile(HttpServletRequest request)Parses the http request for the real script or template source file.voidinit(ServletConfig config)Overrides the generic init method to set some debug flags.protected java.lang.StringremoveNamePrefix(java.lang.String name)protected voidsetVariables(ServletBinding binding)Override this method to set your variables to the Groovy binding.Methods inherited from class javax.servlet.http.HttpServletdoDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service, serviceMethods inherited from class javax.servlet.GenericServletdestroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, logMethods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
- 
Field Details- 
INIT_PARAM_RESOURCE_NAME_REGEXpublic static final java.lang.String INIT_PARAM_RESOURCE_NAME_REGEX- See Also:
- Constant Field Values
 
- 
INIT_PARAM_RESOURCE_NAME_REGEX_FLAGSpublic static final java.lang.String INIT_PARAM_RESOURCE_NAME_REGEX_FLAGS- See Also:
- Constant Field Values
 
- 
CONTENT_TYPE_TEXT_HTMLpublic static final java.lang.String CONTENT_TYPE_TEXT_HTMLContent type of the HTTP response.- See Also:
- Constant Field Values
 
- 
INC_PATH_INFOpublic static final java.lang.String INC_PATH_INFOServlet API include key name: path_info- See Also:
- Constant Field Values
 
- 
INC_REQUEST_URIpublic static final java.lang.String INC_REQUEST_URI- See Also:
- Constant Field Values
 
- 
INC_SERVLET_PATHpublic static final java.lang.String INC_SERVLET_PATHServlet API include key name: servlet_path- See Also:
- Constant Field Values
 
- 
servletContextServlet (or the web application) context.
- 
resourceNamePatternprotected java.util.regex.Pattern resourceNamePatternEithernullor a compiled pattern read from ""resource.name.regex"" and used ingetScriptUri(HttpServletRequest).
- 
resourceNameReplacementprotected java.lang.String resourceNameReplacementThe replacement used by the resource name matcher.
- 
resourceNameReplaceAllprotected boolean resourceNameReplaceAllThe replace method to use on the matcher.true - replaceAll(resourceNameReplacement); (default) false - replaceFirst(resourceNameReplacement); 
- 
verboseprotected boolean verboseControls almost all log output.
- 
encodingprotected java.lang.String encodingEncoding to use, becomes charset part of contentType.
- 
reflectionprotected boolean reflectionMirrors the static value of the reflection flag in MetaClass. See AbstractHttpServlet#logGROOVY861
- 
namePrefixprotected java.lang.String namePrefixa.fink: it was inremoveNamePrefix(java.lang.String), but was extracted to var for optimization
 
- 
- 
Constructor Details- 
AbstractHttpServletpublic AbstractHttpServlet()Initializes all fields with default values.
 
- 
- 
Method Details- 
generateNamePrefixOnceprotected void generateNamePrefixOnce()
- 
removeNamePrefix- Throws:
- ResourceException
 
- 
getResourceConnectionpublic java.net.URLConnection getResourceConnection(java.lang.String name) throws ResourceExceptionInterface method for ResourceContainer. This is used by the GroovyScriptEngine.- Specified by:
- getResourceConnectionin interface- ResourceConnector
- Throws:
- ResourceException
 
- 
getScriptUriReturns the include-aware uri of the script or template file.- Parameters:
- request- the http request to analyze
- Returns:
- the include-aware uri either parsed from request attributes or hints provided by the servlet container
 
- 
applyResourceNameMatcherprotected java.lang.String applyResourceNameMatcher(java.lang.String uri)
- 
getScriptUriAsFileParses the http request for the real script or template source file.- Parameters:
- request- the http request to analyze
- Returns:
- a file object using an absolute file path name, or nullif the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).
 
- 
initOverrides the generic init method to set some debug flags.- Specified by:
- initin interface- Servlet
- Overrides:
- initin class- GenericServlet
- Parameters:
- config- the servlet configuration provided by the container
- Throws:
- ServletException- if init() method defined in super class javax.servlet.GenericServlet throws it
 
- 
setVariablesOverride this method to set your variables to the Groovy binding.All variables bound the binding are passed to the template source text, e.g. the HTML file, when the template is merged. The binding provided by TemplateServlet does already include some default variables. As of this writing, they are (copied from ServletBinding):- "request" : HttpServletRequest
- "response" : HttpServletResponse
- "context" : ServletContext
- "application" : ServletContext
- "session" : request.getSession(false)
 And via implicit hard-coded keywords: - "out" : response.getWriter()
- "sout" : response.getOutputStream()
- "html" : new MarkupBuilder(response.getWriter())
 The binding also provides convenient methods: - "forward(String path)" : request.getRequestDispatcher(path).forward(request, response);
- "include(String path)" : request.getRequestDispatcher(path).include(request, response);
- "redirect(String location)" : response.sendRedirect(location);
 Example binding all servlet context variables: class MyServlet extends TemplateServlet { protected void setVariables(ServletBinding binding) { // Bind a simple variable binding.setVariable("answer", new Long(42)); // Bind all servlet context attributes... ServletContext context = (ServletContext) binding.getVariable("context"); Enumeration enumeration = context.getAttributeNames(); while (enumeration.hasMoreElements()) { String name = (String) enumeration.nextElement(); binding.setVariable(name, context.getAttribute(name)); } } }- Parameters:
- binding- to be modified
 
 
-