What are Lua Server Pages?
When creating a webinterface, static HTML pages are often not sufficient. To be able to communicate with applications running on Screvle, the microWebServer implements "Lua Server Pages".
Lua Server Pages are generic HTML pages, but can have Lua code embedded inside between special tags:
<?lua ... ?>
All text within the tags, called a Lua Server Page Code Block, is considered Lua code and is executed by the Lua interpreter.
Lua Server Page Code Blocks ("<?lua ... ?>") are executed as a standalone pieces of code and must be a valid Lua application on its own. Local variables are not accessible by subsequent Lua Server Page Code Blocks.
All files ending with extension ".lsp" are considered Lua Server Pages and are parsed and executed by the Lua Server Page handler.
Data provided to Lua
A special lsp object is created before calling the Lua code inside Lua Server Pages. This allows the Lua Server Page code to access data contained in the HTTP Request and manipulate some parts of the HTTP Reply.
The lsp object has the following fields:
Field | Type | Description | Read/Write |
---|---|---|---|
method | String | The HTTP method used, either "GET" or "POST" | Read Only |
data | String | Data received as URL parameters in case of GET or body data in case of POST | Read Only |
mime | String | Change to set the mime-type of the HTTP Reply. Defaults to "text/html" | Write Only |
response | Number | Change to set the response code of the HTTP Reply. Defaults to 200 (OK) | Write Only |
lsp.mime and lsp.response can only be set before the first call to lsp.write. All subsequent changes are not taken into account.
Writing data in the HTTP Response
The Lua code executed as part of a Lua Server Page can write (part of) the response data. This is done by calling the lsp.write( string ) function.
The string parameter of lsp.write is embedded inline into the content of the page.
Examples
<html> <head><title>Lua Server Page</title></head> <body> Today's date is: <?lua time = system.getTime() months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" } lsp.write( time.weekday .. ", " .. time.day .. " " .. months[ time.month ] .. " " .. time.year .. "." ) ?> </body> </html>
Result