Class GroovyServlet

  • All Implemented Interfaces:
    ResourceConnector, java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

    public class GroovyServlet
    extends AbstractHttpServlet
    This servlet will run Groovy scripts as Groovlets. Groovlets are scripts with these objects implicit in their scope:
    • request - the HttpServletRequest
    • response - the HttpServletResponse
    • application - the ServletContext associated with the servlet
    • session - the HttpSession associated with the HttpServletRequest
    • out - the PrintWriter associated with the ServletRequest

    Your script sources can be placed either in your web application's normal web root (allows for subdirectories) or in /WEB-INF/groovy/* (also allows subdirectories).

    To make your web application more groovy, you must add the GroovyServlet to your application's web.xml configuration using any mapping you like, so long as it follows the pattern *.* (more on this below). Here is the web.xml entry:

        <servlet>
          <servlet-name>Groovy</servlet-name>
          <servlet-class>groovy.servlet.GroovyServlet</servlet-class>
        </servlet>
    
        <servlet-mapping>
          <servlet-name>Groovy</servlet-name>
          <url-pattern>*.groovy</url-pattern>
          <url-pattern>*.gdo</url-pattern>
        </servlet-mapping>
     

    The URL pattern does not require the "*.groovy" mapping. You can, for example, make it more Struts-like but groovy by making your mapping "*.gdo".

    See Also:
    ServletBinding, Serialized Form
    • Constructor Detail

      • GroovyServlet

        public GroovyServlet()
    • Method Detail

      • init

        public void init​(javax.servlet.ServletConfig config)
                  throws javax.servlet.ServletException
        Initialize the GroovyServlet.
        Specified by:
        init in interface javax.servlet.Servlet
        Overrides:
        init in class AbstractHttpServlet
        Parameters:
        config - the servlet configuration provided by the container
        Throws:
        javax.servlet.ServletException - if this method encountered difficulties
      • service

        public void service​(javax.servlet.http.HttpServletRequest request,
                            javax.servlet.http.HttpServletResponse response)
                     throws java.io.IOException
        Handle web requests to the GroovyServlet
        Overrides:
        service in class javax.servlet.http.HttpServlet
        Throws:
        java.io.IOException
      • createGroovyScriptEngine

        protected GroovyScriptEngine createGroovyScriptEngine()
        Hook method to setup the GroovyScriptEngine to use.
        Subclasses may override this method to provide a custom engine.