groovy.servlet
public class: GroovyServlet [javadoc |
source]
java.lang.Object
javax.servlet.GenericServlet
javax.servlet.http.HttpServlet
groovy.servlet.AbstractHttpServlet
groovy.servlet.GroovyServlet
All Implemented Interfaces:
ResourceConnector, Serializable, Servlet, ServletConfig
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".
Also see:
- groovy.servlet.ServletBinding
- author:
Sam - Pullara
- author:
Mark - Turansky (markturansky at hotmail.com)
- author:
Guillaume - Laforge
- author:
Christian - Stein
- author:
Marcel - Overdijk
| Fields inherited from groovy.servlet.AbstractHttpServlet: |
|---|
| CONTENT_TYPE_TEXT_HTML, INC_PATH_INFO, INC_REQUEST_URI, INC_SERVLET_PATH, servletContext, resourceNameMatcher, resourceNameReplacement, resourceNameReplaceAll, verbose, encoding, reflection |
| Methods from javax.servlet.http.HttpServlet: |
|---|
|
service |
| Methods from javax.servlet.GenericServlet: |
|---|
|
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, init, log, log, service |
| Method from groovy.servlet.GroovyServlet Detail: |
protected GroovyScriptEngine createGroovyScriptEngine() {
return new GroovyScriptEngine(this);
}
Hook method to setup the GroovyScriptEngine to use.
Subclasses may override this method to provide a custom
engine. |
public void init(ServletConfig config) throws ServletException {
super.init(config);
// Set up the scripting engine
gse = createGroovyScriptEngine();
servletContext.log("Groovy servlet initialized on " + gse + ".");
}
Initialize the GroovyServlet. |
public void service(HttpServletRequest request,
HttpServletResponse response) throws IOException {
// Get the script path from the request - include aware (GROOVY-815)
final String scriptUri = getScriptUri(request);
// Set it to HTML by default
response.setContentType("text/html; charset="+encoding);
// Set up the script context
final ServletBinding binding = new ServletBinding(request, response, servletContext);
setVariables(binding);
// Run the script
try {
Closure closure = new Closure(gse) {
public Object call() {
try {
return ((GroovyScriptEngine) getDelegate()).run(scriptUri, binding);
} catch (ResourceException e) {
throw new RuntimeException(e);
} catch (ScriptException e) {
throw new RuntimeException(e);
}
}
};
GroovyCategorySupport.use(ServletCategory.class, closure);
/*
* Set reponse code 200.
*/
response.setStatus(HttpServletResponse.SC_OK);
} catch (RuntimeException runtimeException) {
StringBuffer error = new StringBuffer("GroovyServlet Error: ");
error.append(" script: '");
error.append(scriptUri);
error.append("': ");
Throwable e = runtimeException.getCause();
/*
* Null cause?!
*/
if (e == null) {
error.append(" Script processing failed.");
error.append(runtimeException.getMessage());
if (runtimeException.getStackTrace().length > 0)
error.append(runtimeException.getStackTrace()[0].toString());
servletContext.log(error.toString());
System.err.println(error.toString());
runtimeException.printStackTrace(System.err);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, error.toString());
return;
}
/*
* Resource not found.
*/
if (e instanceof ResourceException) {
error.append(" Script not found, sending 404.");
servletContext.log(error.toString());
System.err.println(error.toString());
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
/*
* Other internal error. Perhaps syntax?!
*/
servletContext.log("An error occurred processing the request", runtimeException);
error.append(e.getMessage());
if (e.getStackTrace().length > 0)
error.append(e.getStackTrace()[0].toString());
servletContext.log(e.toString());
System.err.println(e.toString());
runtimeException.printStackTrace(System.err);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
} finally {
/*
* Finally, flush the response buffer.
*/
response.flushBuffer();
// servletContext.log("Flushed response buffer.");
}
}
Handle web requests to the GroovyServlet |