Common scripting variables
The basic objects, such as the request and response, are available for most scripting languages (Note the differences for eg. JSP below).
| Name |
Description |
| request |
SlingHttpServletRequest object, providing access to the HTTP request header information - extends the standard HttpServletRequest - and provides access to Sling-specific things like resource, path info, selector, etc. |
| response |
SlingHttpServletResponse object, providing access for the HTTP response that is created by the server. This is currently the same as the HttpServletResponse from which it extends. |
| sling |
SlingScriptHelper , containing convenience methods for scripts, mainly sling.include('/some/other/resource') for including the responses of other resources inside this response (eg. embedding header html snippets) and sling.getService(foo.bar.Service.class) to retrieve OSGi services available in Sling (Class notation depending on scripting language). |
| resource |
Current Resource to handle, depending on the URL of the request. Same as request.getResource(). |
| currentNode |
If the current resource points to a JCR node (which is typically the case in Sling), this gives you direct access to the Node object. Otherwise this object is not defined. |
| reader |
Direct access to the Reader of the request - same as request.getReader(). Use it for reading the data of an HTTP request body. |
| out |
Direct access to the PrintWriter of the response - same as response.getWriter(). Use it for writing to the HTTP response body. |
| flush |
Indicates whether the output used by the script shall be flushed after the script evaluation ended. It is a Boolean, so use TRUE for flushing or FALSE for not flushing, which is the default value. The boolean value notation depends on the scripting language. |
| log |
Provides an SLF4J Logger for logging to the Sling log system from within scripts, eg. log.info("Executing my script"). |
See also the api documentation of the org.apache.sling.api.scripting.SlingBindings.java
which defines the common scripting variables.
JSP
Since JSPs already have a few Java-objects pre-defined, things have to be named differently here. And you will have to explicitly require the variables to be defined with a custom tag <sling:defineObjects />. Your jsp should start with:
<%@ page session="false" %>
<%@ page import="javax.jcr.*,
org.apache.sling.api.resource.Resource"
%>
<%@ taglib prefix="sling" uri="http: %>
<sling:defineObjects />
| slingRequest |
SlingHttpServletRequest object, providing access to the HTTP request header information - extends the standard HttpServletRequest - and provides access to Sling-specific things like resource, path info, selector, etc. |
| slingResponse |
SlingHttpServletResponse object, providing access for the HTTP response that is created by the server. This is currently the same as the HttpServletResponse from which it extends. |
| request |
The standard JSP request object which is a pure HttpServletRequest . |
| response |
The standard JSP response object which is a pure HttpServletResponse . |
| resourceResolver |
Current ResourceResolver . Same as slingRequest.getResourceResolver(). |
| sling |
SlingScriptHelper , containing convenience methods for scripts, mainly sling.include('/some/other/resource') for including the responses of other resources inside this response (eg. embedding header html snippets) and sling.getService(foo.bar.Service.class) to retrieve OSGi services available in Sling (Class notation depending on scripting language). |
| resource |
Current Resource to handle, depending on the URL of the request. Same as slingRequest.getResource(). |
| currentNode |
If the current resource points to a JCR node (which is typically the case in Sling), this gives you direct access to the Node object. Otherwise this object is not defined. |
| log |
Provides an SLF4J Logger for logging to the Sling log system from within scripts, eg. log.info("Executing my script"). |
Resource Inclusion
Instead of sling.include("/path/to/resource") you can use the sling taglib for that:
<sling:include path="/path/to/resource" />
For more options of the sling taglib see the tag lib definition file
.