groovy.servlet.GroovyServlet Maven / Gradle / Ivy
Show all versions of groovy-servlet Show documentation
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package groovy.servlet;
import groovy.lang.Closure;
import groovy.util.GroovyScriptEngine;
import groovy.util.ResourceException;
import groovy.util.ScriptException;
import org.codehaus.groovy.runtime.GroovyCategorySupport;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 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:
*
*
* {@code
*
* Groovy
* groovy.servlet.GroovyServlet
*
*
*
* Groovy
* *.groovy
* *.gdo
*
* }
*
*
* 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 groovy.servlet.ServletBinding
*/
public class GroovyServlet extends AbstractHttpServlet {
/**
* The script engine executing the Groovy scripts for this servlet
*/
private GroovyScriptEngine gse;
/**
* Initialize the GroovyServlet.
*
* @throws ServletException
* if this method encountered difficulties
*/
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
// Set up the scripting engine
gse = createGroovyScriptEngine();
servletContext.log("Groovy servlet initialized on " + gse + ".");
}
/**
* Handle web requests to the GroovyServlet
*/
@Override
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