|
Embedded Server Pages are HTML pages with embedded JavaScript logic. When a client requests an ESP page, the web page is compiled into pure JavaScript that is then interpreted at run-time. Although slower than pure C code, the ESP JavaScript calls C routines for many functions so the overall result is fast page generation and response to client requests.
|
| Line | @@i |
Accessing VariablesToday's temperature is <% write(temp); %>
Today's temperature is <% =temp; %>
Today's temperatore is @@temperature
Scripted HTML<% for (i = 0; i < 3; i++) { %>
Line @@i
<% } %>
The AppWeb ESP implementation also supports server side includes. To include another document use the directive:
Server Side Includes (SSI)
<% include fileName.esp %>
This will replace the ESP script with the content of the nominated filename The included file is assumed to be an ESP page that will be parsed at the point of the include directive. It may contain further ESP tags and include directives.
You can also include pure Embedded JavaScript code using the JavaScript include procedure:
<% include("myLib.js"); %>
In this case, the included file must contain pure JavaScript and must not contain ESP directives.
Embedded JavaScript Overview
Embedded JavaScript (EJS) is a subset of the ECMA-262 standard which describes ECMAScript. EJS implements the key lanaguage features and conforms to the ECMAScript language syntax specifications. A strict subset of ECMAScript is implemented, sufficient to enable simple and powerful scripting by Embedded Server Pages, yet small enough to ensure a small memory footprint.
For those unfamiliar with JavaScript, the initial syntax closely resembles the C language but variables and function arguments are typeless.
JavaScript also supports objects, object methods, associative arrays, and a for/in iterator statement. These features make it much closer to Perl or other scripting languages than C.Embedded JavaScript is multithreaded and will support multiple simultaneous instances. When used by ESP, each JavaScript session has its own local variable store and instances can share data via the session[] and application[] data store.
Embedded JavaScript implements the following JavaScript language elements.
- Comments (C and C++ style)
- Arrays and associative arrays
- Objects
- Identifiers
- Data types including booleans, integers, 64-bit integers, floating point and strings
- Expressions
- If/else
- for and for/in
- JavaScript functions
- return
- C/C++ bound functions
The supported operators are:
- < <= == != > >=
- + - / %
- << >>
- ++ --
The supported conditional operators are:
- && ||
- !
The following language elements are not implemented in ESP 2.0:
- Exceptions
- Labeled statements
- These control flow statements: break, case, continue, default, do/while, export, import, switch, var, while, with
- Regular expressions
ESP web pages can embed JavaScript directly in the web page or they can include libraries of Embedded JavaScript (EJS) code. EJS is a powerful language, however, it is normally best to keep scripting to a minimum and put complex code in a language such as C or C++.
See the Embedded Server Pages JavaScript API Reference for more detailed JavaScript documentation.
You may use your favorite HTML editor to create Embedded Server Pages. Dreamweaver is perhaps one of the best, but there are other fine choices. If your HTML editor supports PHP/ASP script editing, you may be able to use this feature as ASP uses the same <% %> delimiters as ESP. Otherwise, create your page using the page layout tool and then switch to the HTML code view to insert the ESP scripting at the relevant locations.
Page Creation Tools
AppWeb is configured by default to use ESP, so you should have to do nothing to use ESP. The AppWeb configuration file has the following directives to load the ESP module and to add the ESP handler:
Configuring ESP in AppWeb
LoadModule esp libespModule
AddHandler espHandler .esp .asp
These configure AppWeb to use the ESP handler for all requests that have a ".esp" or ".asp" extension.
AppWeb also configures a Location block for ESP. This instructs AppWeb to send all requests that begin with "/esp/" to the ESP handler:
SetHandler espHandler
Lastly, the directory to store uploaded files is configured with the FileUploadDir directive.
FileUploadDir /tmp
When using ESP with WebServer, make sure you have a version later than version 2.5 downloaded from https://mbedthis.com/webServer. ESP is configured via the config.h header file.
Configuring ESP in WebServer