The SLIME servlet implementation allows Java servlets to be authored in JavaScript.

Scope

The SLIME servlet implementation provides the servlet with various objects and APIs that allow the servlet to interact with its environment, answer requests, and do various computations.

httpd

The httpd property represents APIs available to every servlet.

The following SLIME APIs are provided:
Name Description
$api JavaScript utilities.
httpd.js The js/object API.
httpd.web The js/web API.
httpd.java The jrunscript/host API.
httpd.io The jrunscript/io API.
header Represents an HTTP header. The value of the name property should be treated as case-insensitive.
has properties:
  • name
    string The name of the header.
  • value
    string The value of the header.
pair Represents a name-value pair.
has properties:
  • name
    string
  • value
    string
request An HTTP request from a client.
has properties:
  • source
    has properties:
    • ip
      string The remote IP address of the requestor.
  • scheme
    __TYPE__ __DESCRIPTION__
  • method
    string The HTTP method used for the request, as an uppercase string.
  • path
    string The path used for the request, relative to the webapp. Note that unlike in Java servlets, and many other HTTP server environments, this path does not contain a leading /.
  • query
    (conditional; present if requested URL had a query string)
    has properties:
    • string
      string The query string of the requested URL.
    • form
      __DESCRIPTION__
      Arguments
      Returns
      Array of pair __DESCRIPTION__
  • parameters
    Array of pair Replaced by query.form() The query string parameters provided as part of this request. Note that these parameters do not include form data provided as part of a POST request.
  • headers
    Array of header The headers included with this request. The given array also has the following method:
    has properties:
    • value
      Returns the value of the given header as a string. Multiple values will be comma-delimited, per RFC 2616.
      Arguments
      1. string A (case-insensitive) header name.
      Returns
      string The value of that header, or null if it is not present.
  • user
    Represents a user; see getUserPrincipal().
    has properties:
  • body
    The body of the request.
    has properties:
    • type
      MIME type The MIME type of the request body.
    • stream
      byte input stream A stream from which the request body can be read.
response

A response to be sent to a client in response to a request.

The content of the body must be specified in some way. Using a resource causes the type and content to be specified; note that using a Java resource automatically provides other capabilities (Last-Modified and Content-Length headers).

body
has properties:
  • type
    string -OR- MIME type (optional) The MIME type of the request body.
  • length
    number (optional) The length of the content, in bytes. Used to set the Content-Length header in the response.
  • modified
    Date (optional) The date that the content was last modified. Used to set the Last-Modified header.
  • string
    string (optional) The message body to send to the client, as a string.
  • stream
    binary input stream (optional; ignored if string is present) A stream from which the message body to send to the client may be read.
has properties:
  • status
    The status to use in the response.
    has properties:
    • code
      number The HTTP status code to use in the response to the client.
  • headers
    Array of header (optional) Headers to use in the response.
  • body
    resource -or- body The HTTP request body to send to the client.
Handler A function that can handle requests.
Arguments
  1. request A request.
Returns
response A response.

$loader

The $loader object is a servlet-specific Loader that loads resources relative to the servlet's path. The httpd.loader object loads them from the application path. So if a servlet is located at /WEB-INF/myapp/servlet.js, it can load a JavaScript file at /WEB-INF/myapp/code.js via:

$parameters

The $parameters object contains the set of servlet initialization parameters available to the servlet. These are set via the web.xml file when the servlet is declared. $parameters is a JavaScript object containing a property for each parameter whose name is the name of the parameter and whose value is the value of that parameter.

Servlet Interface

The servlet interacts with its container by assigning properties to its $exports object, provided in the scope. Each servlet must, at a minimum, assign a function to $exports.handle:
$exports.handle = function(request) { return httpd.http.Response.text("Hello, World!"); }
SLIME servlets do not have a separate initialization procedure as Java servlets do; they may perform any initialization in their script.

$exports

has properties:

Deployment

SLIME servlets may be built into ordinary Java web applications, via either the jsh.httpd.tools API or via the rhino/http/servlet/tools/webapp.jsh.js script.
See the documentation for jsh.httpd.tools for how to use the API to build a standard Java webapp.
The webapp.jsh.js script is a jsh script, and takes the following command-line arguments: