Module Servlet

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.

Servlet access

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 first time the URL is accessed, the module will be loaded automatically (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.

Examples

The following WebL servlet shows how to set and retrieve a variable on your Web server.

// File: Example1.webl

 

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)

theval = req.param.x;

res.mimetype = "text/plain";

res.result = "Set val to " + theval;

end;

 

// To retrieve the value, access:

// http://www.host.com/servlet/webl/Example1_GetVal

 

export var GetVal = fun(req, res)

res.mimetype = "text/plain";

res.result = "Val is " + theval;

end;

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:

// File: Example2.webl

var Decode = fun(req)

var s = "Header snoop:\n\n";

every field in req do

s = s + ToString(field) + ": "

+ ToString(req[field]) + "\n";

end;

s;

end;

 

// Access the following URL:

// http://www.host.com/servlet/webl/Example2_Snoop

//

export var Snoop = fun(req, res)

res.mimetype = "text/plain";

res.result = Decode(req);

end;

 

Servlets typically use HTTP cookies to keep track of client state. The following WebL servlet maintains a visit counter inside the client's cookie:

// File: Example3.webl

 

// Access the counter with:

// http://www.host.com/servlet/webl/Example3_Count

 

export var Count = fun(req, res)

res.result = "Cookie test\n";

 

// 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";

0

end;

 

res.result = res.result + "Count = " + count;

res.mimetype = "text/plain";

 

// set the new cookie

res.cookies = [.

cc = [. domain="www.myhost.com", path="/",

value=count + 1, comment="",

maxage=-1, version=0 .]

.]

end;

 

Server setup

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.

 

Format of the Servlet request parameter object

 

[.

method: string,

requestURI: string,

servletpath: string,

pathinfo: string,

pathtranslated: string,

querystring: string,

remoteuser: string,

authtype: string,

remoteaddr: string,

remotehost: string,

scheme: string,

servername: string,

serverport: string,

protocol: string,

contenttype: string,

 

header: object,

param: object,

cookies: object]

.]

 

 

Format of the Servlet response parameter object

 

[.

statuscode: int,

statusmsg: string,

result: string,

mimetype: string

header: [. name=val, name=val, ... .],

cookies : [.

cookiename = [.

comment: string,

domain: string,

maxage: int,

path: string,

secure: bool,

value: string,

version: int

.],

cookiename = ...,

cookiename = ...,

...

.]

.]


Up Previous Next