Many web servers today support the Java Servlet standard from JavaSoft. This standard allows the efficient execution of server-side actions. In addition to the built-in Web server support (see module WebServer), WebL also supports the servlet standard directly and transparently. In fact, the WebL Servlet integration is so transparent that no new functions need to be introduced. The description how to use servlets provided below assumes a fair knowledge about Java and servlets; it is thus advisable to study the servlet documention before continuing.
The class weblx.servlet.Servlet implements the WebL servlet. By placing this class (or the jar file it is in, namely WebL.jar) on a servlet enabled web-server, it becomes possible to execute WebL code directly on the server. Web surfers may access your WebL servlet by accessing a URL of (typically) the following form:
http://www.host.com/servlet/weblx.servlet.Servlet/
modulename_variablename?arguments
In case your web server supports aliases, you can alias "weblx.servlet.Servlet" as "webl", which allows access from the following URL:
http://www.host.com/servlet/webl/
modulename_variablename?arguments
In both cases, modulename identifies the WebL module that contains the WebL servlet script, and variablename identifies an exported variable in that module. The value type of this variable must be a function with two formal arguments. The module will be loaded automatically the first time the URL is accessed (this happens only once; afterwards the module is cached).
See Format of the Servlet request parameter object and See Format of the Servlet response parameter object show the format of the two arguments of the function. The first is the request object, and the second is the response object. The explanation for the field names and values is found in the Java servlet specification available from Javasoft.
You may modify your WebL servlets while being used. WebL checks before each servlet access whether the WebL module has changed or not (using the file lastmodified date). If the modified date is different from the modified date when the module was loaded first, the module is auotmatically reloaded.
The following WebL servlet shows how to set and retrieve a variable on your Web server.
var theval = nil; // the variable
// To set the variable to "hello", access:
// http://www.host.com/servlet
// /webl/Example1_SetVal?x=hello
export var SetVal = fun(req, res)
res.result = "Set val to " + theval;
// To retrieve the value, access:
// http://www.host.com/servlet/webl/Example1_GetVal
export var GetVal = fun(req, res)
res.result = "Val is " + theval;
Note how the x parameter in the URL is accessed with req.param.x. Note, that in the case of multiple parameters with the same name, the particular parameter field will have a list of strings as value. Programmers should thus be aware of the fact that the value type of parameters is either a string, or a list of strings, depending on the number of parameters.
Often during servlet development you will need to snoop the servlet request headers. The following example shows how this is done with a WebL servlet:
s = s + ToString(field) + ": "
+ ToString(req[field]) + "\n";
// http://www.host.com/servlet/webl/Example2_Snoop
export var Snoop = fun(req, res)
Servlets typically use HTTP cookies to keep track of client state. The following WebL servlet maintains a visit counter inside the client's cookie:
// http://www.host.com/servlet/webl/Example3_Count
export var Count = fun(req, res)
// Retrieve the cookie named "cc"
var count = ToInt(req.cookies.cc) ?
begin // executed if no such cookie exists
res.result = res.result + "No cookie\n";
res.result = res.result + "Count = " + count;
Servlet setup can be complicated. First make sure that you can access the demo servlets that come with your web server. Only then continue with this checklist:
Put WebL.jar is the CLASSPATH of your web server (server dependent).
Add a configuration parameter for the WebL servlet to your Web server (server dependent):
Parameter name: webl.path
Parameter value: (directory search path for WebL scripts)
Restart your web server for changes to take affect.
Place WebL scripts in the directory of the search path, as indicated by 2.