Introduction to LSP

  • LSP is an acronym for Lua Server Pages
  • LSP scripts are executed on the server
  • LSP is a combination of HTML and Lua scripts
  • The Lua scripting engine is embeddable, small, and fast

LSP is a programming language which combines HTML and Lua and that can do all sorts of things: evaluate form data sent from a browser, build custom web content to serve the browser, talk to a database, and so on.

How the interactive examples work

The examples are loaded from the server using AJAX and inserted into a fully functional web based LSP editor. You can edit any of the examples, and the example will be saved when you click the run button. The run button also executes the LSP page on the server and the result pops up below in an iframe. Try clicking the run button below and the iframe should show the result.

The revert button reverts the example back to the original code and hides the iframe. Click line 2 in the above editor, modify the text 'Hello World' to 'LSP Rocks', and click the Run button. You can then try the Revert button and revert the example back to its original form.

LSP Tags

Lua code can be written right into your HTML like this:

Your Lua code goes inside the <?lsp and ?> delimiters. Here we use the function print to output "Hello". We also output the text "world!" by using the expression tag.

Lua script can be freely intermixed with HTML or XML. LSP tags are XML compliant <?lsp    ?>. Code between these tags is executed as Lua code. Expressions are provided by the <?lsp=    ?> tag. The result of an expression is emitted as HTML/XML.

Application server v.s. web server

An LSP page is automatically converted to a Lua script and then executed on the server when the page is accessed by a client such as a browser. The response from the LSP page is shown in your browser window.

"So what is the big deal?" You might say. "I can do that with JavaScript.", but that is incorrect. JavaScript generally runs in the browser or client. This means it only really knows what's going on in your browser, plus whatever information it gets from the server you're connecting to. In addition JavaScript's knowledge is limited.

LSP, on the other hand, runs on the same computer as the website you're visiting, which is known as the server. This means that it has access to all the information and files on that machine, which allows it to construct custom HTML pages to send to your browser, handle cookies, and run tasks or perform calculations with data from that website.

The following example shows how an LSP page runs on the server. The example produces no visual response in the browser, but is instead printing a message to the console window where the server is running. Reposition the server's console window next to your browser window, and click the run button. You should see a printout in the server's console window.

Line 4 in the above example sets variable 'print' to the global variable print, thus hiding the default print function in the LSP page. Lua provides scoping and environments. You will learn more about how this works in the Lua Scope tutorials. Delete line 4 above and re-run the script. You should now see the Hello World message being printed in the browser window and not in the server's console window.

In addition to using function print for sending response data, the Barracuda App Server also provides a method in the response object for sending response data. The method response:write is faster than function print since it does not deal with the specialties in the standard Lua print function.

The following example shows how to use response:write.

Next Tutorial