Package: groovy.servlet

## [Java] Class AbstractHttpServlet

- groovy.servlet.AbstractHttpServlet

- All Implemented Interfaces and Traits:
    ResourceConnector

      public abstract class AbstractHttpServlet
      extends jakarta.servlet.http.HttpServlet
      implements ResourceConnector

  A base class dealing with common HTTP servlet API housekeeping aspects.

  #### 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:

       {value:#INIT_PARAM_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 fall back 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

- - ### Field Summary

    Fields
    Modifiers | Name | Description
    --- | --- | ---
    `static String` | `CONTENT_TYPE_TEXT_HTML` | Content type of the HTTP response.
    `static String` | `INC_PATH_INFO` | Servlet API include key name: path_info
    `static String` | `INC_REQUEST_URI` |
    `static String` | `INC_SERVLET_PATH` | Servlet API include key name: servlet_path
    `static String` | `INIT_PARAM_RESOURCE_NAME_REGEX` |
    `static String` | `INIT_PARAM_RESOURCE_NAME_REGEX_FLAGS` |
    `protected String` | `encoding` | Encoding to use, becomes charset part of contentType.
    `protected String` | `namePrefix` | a.fink: it was in removeNamePrefix, but was extracted to var for optimization
    `protected boolean` | `reflection` | Mirrors the static value of the reflection flag in MetaClass.
    `protected Pattern` | `resourceNamePattern` | Either `null` or a compiled pattern read from "{ value: #INIT_PARAM_RESOURCE_NAME_REGEX}" and used in AbstractHttpServlet.getScriptUri.
    `protected boolean` | `resourceNameReplaceAll` | The replace method to use on the matcher.
    `protected String` | `resourceNameReplacement` | The replacement used by the resource name matcher.
    `protected jakarta.servlet.ServletContext` | `servletContext` | Servlet (or the web application) context.
    `protected boolean` | `verbose` | Controls almost all log output.

  - Inherited fields
    Fields inherited from class | Fields
    --- | ---
    `class jakarta.servlet.http.HttpServlet` | `LEGACY_DO_HEAD`

  - ### Constructor Summary

    Constructors
    Constructor and description
    `AbstractHttpServlet()` Initializes all fields with default values.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `protected String` | `applyResourceNameMatcher(String uri)`
    `` | `protected void` | `generateNamePrefixOnce()`
    `` | `public URLConnection` | `getResourceConnection(String name)` Interface method for ResourceContainer.
    `` | `protected String` | `getScriptUri(jakarta.servlet.http.HttpServletRequest request)` Returns the include-aware uri of the script or template file.
    `` | `protected File` | `getScriptUriAsFile(jakarta.servlet.http.HttpServletRequest request)` Parses the http request for the real script or template source file.
    `` | `public void` | `init(jakarta.servlet.ServletConfig config)` Overrides the generic init method to set some debug flags.
    `` | `protected String` | `removeNamePrefix(String name)`
    `` | `protected void` | `setVariables(ServletBinding binding)` Override this method to set your variables to the Groovy binding.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class jakarta.servlet.http.HttpServlet` | `jakarta.servlet.http.HttpServlet#init(jakarta.servlet.ServletConfig), jakarta.servlet.http.HttpServlet#service(jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse), jakarta.servlet.http.HttpServlet#log(java.lang.String, java.lang.Throwable), jakarta.servlet.http.HttpServlet#log(java.lang.String), jakarta.servlet.http.HttpServlet#init(), jakarta.servlet.http.HttpServlet#destroy(), jakarta.servlet.http.HttpServlet#getInitParameter(java.lang.String), jakarta.servlet.http.HttpServlet#getServletContext(), jakarta.servlet.http.HttpServlet#getServletName(), jakarta.servlet.http.HttpServlet#getServletConfig(), jakarta.servlet.http.HttpServlet#getServletInfo(), jakarta.servlet.http.HttpServlet#getInitParameterNames(), jakarta.servlet.http.HttpServlet#wait(long, int), jakarta.servlet.http.HttpServlet#wait(), jakarta.servlet.http.HttpServlet#wait(long), jakarta.servlet.http.HttpServlet#equals(java.lang.Object), jakarta.servlet.http.HttpServlet#toString(), jakarta.servlet.http.HttpServlet#hashCode(), jakarta.servlet.http.HttpServlet#getClass(), jakarta.servlet.http.HttpServlet#notify(), jakarta.servlet.http.HttpServlet#notifyAll()`

- - ### Field Detail

    - #### public static final String CONTENT_TYPE_TEXT_HTML

      Content type of the HTTP response.

    - #### public static final String INC_PATH_INFO

      Servlet API include key name: path_info

    - #### public static final String INC_REQUEST_URI

    - #### public static final String INC_SERVLET_PATH

      Servlet API include key name: servlet_path

    - #### public static final String INIT_PARAM_RESOURCE_NAME_REGEX

    - #### public static final String INIT_PARAM_RESOURCE_NAME_REGEX_FLAGS

    - #### protected String encoding

      Encoding to use, becomes charset part of contentType.

    - #### protected String namePrefix

      a.fink: it was in removeNamePrefix, but was extracted to var for optimization

    - #### protected boolean reflection

      Mirrors the static value of the reflection flag in MetaClass. See AbstractHttpServlet#logGROOVY861

    - #### protected Pattern resourceNamePattern

      Either `null` or a compiled pattern read from "{

      value:
        #INIT_PARAM_RESOURCE_NAME_REGEX}" and used in AbstractHttpServlet.getScriptUri.

    - #### protected boolean resourceNameReplaceAll

      The replace method to use on the matcher.

           true - replaceAll(resourceNameReplacement); (default)
           false - replaceFirst(resourceNameReplacement);

    - #### protected String resourceNameReplacement

      The replacement used by the resource name matcher.

    - #### protected jakarta.servlet.ServletContext servletContext

      Servlet (or the web application) context.

    - #### protected boolean verbose

      Controls almost all log output.

  - ### Constructor Detail

    - #### public AbstractHttpServlet()

      Initializes all fields with default values.

  - ### Method Detail

    - #### protected String applyResourceNameMatcher(String uri)

    - #### protected void generateNamePrefixOnce()

    - #### @Override
      public URLConnection getResourceConnection(String name)

      Interface method for ResourceContainer. This is used by the GroovyScriptEngine.

    - #### protected String getScriptUri(jakarta.servlet.http.HttpServletRequest request)

      Returns 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

    - #### protected File getScriptUriAsFile(jakarta.servlet.http.HttpServletRequest request)

      Parses 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 `null` if 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).

    - #### @Override
      public void init(jakarta.servlet.ServletConfig config)

      Overrides the generic init method to set some debug flags.

      throws:
        ServletException if init() method defined in super class javax.servlet.GenericServlet throws it
      Parameters:
        `config` - the servlet configuration provided by the container

    - #### protected String removeNamePrefix(String name)

    - #### protected void setVariables(ServletBinding binding)

      Override 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