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.
Example ESP Web Page
So what does an ESP HTML web page look like? ESP pages are standard HTML pages with embedded
Javascript code containing scripting logic. You can use all your normal HTML tags. ESP tags
delimit the JavaScript using the delimiters <% and %> to bracket the scripting code.
<body>
<h1>Normal HTML header</h1>
<% Script Code Here %>
</body>
This script code is executed and replaced by the ESP interpreter with the output from the
script code. This all occurs at the server before any of the HTML is sent to the client. The
client never sees the script logic and this greatly enhances the security of your application
or device. Don't confuse ESP with client side JavaScript. A single page may have both server
side and client side JavaScript. ESP JavaScript is placed between <% and %> tags and is
replaced by the web server before it is sent to the client. Once the page reaches the clients
browser, it will execute any client side JavaScript which is typically between <SCRIPT>
and </SCRIPT> tags.
The following example demonstrates just a few of the constructs available using ESP.
<HTML><HEAD><TITLE>Simple ESP Test Page</TITLE></HEAD>
<BODY>
<P>The HTML query string for this page is @@QUERY_STRING</P>
<!-- This quickly generates a very large table -->
<TABLE>
<% for (i = 0; i < 3; i++) { %>
<TR><TD>Line</TD><TD>@@i</TD>
<% } %>
</TABLE>
<!-- Sample ESP procedure calls -->
<% displayCurrentSecurityStats("myDb", 1); %>
<%
// Inside JavaScript blocks, we can put any server-side
// JavaScript we like
i = 2;
write("i is " + i);
%>
</BODY>
</HTML>
As you can see, the page is standard HTML with JavaScript logic inside ESP tags. What makes
this especially interesting, is that you can easily create new Embedded JavaScript procedures
in your application to suite your needs. These can then be called from the ESP web pages. This
close nexus between the HTML page and your application logic is what makes Appweb such an easy
platform to use to create dynamic web pages.
ESP pages typically have a ".esp" extension although they can in some web servers such as
Appweb be configured to match by URL prefix. They are processed by the ESP web server handler.
Accessing Variables
There are three methods to access JavaScript variables
within ESP scripts. You can use the ESP write procedure to output the value of a variable back
to the client. For example, assuming you have the current temperature in a JavaScript variable
called temp.
<P>Today's temperature is <% write(temp); %></P>
As this kind of variable access is a very common occurrence, a shorter form may be more
convenient. Because the JavaScript assignment operators sets the
result, it can be used to return a value to the client. For example:
<P>Today's temperature is <% =temp; %></P>
Even easier is to use the @@ directive which does not require any <%
%> enclosing tags. You use this by prepending the required variable with @@. For example:
<P>Today's temperatore is @@temperature</P>
Scripted HTML
Because the ESP web page is compiled into JavaScript, you can
script normal HTML tags. If a section of HTML is enclosed by a JavaScript for loop, you can output the HTML block each time round the loop. This is an easy
way to generate tables. In the example below, three rows of a table are output.
<% for (i = 0; i < 3; i++) { %>
<TR><TD>Line</TD><TD>@@i</TD>
<% } %>
Server Side Includes (SSI)
The Appweb ESP implementation also supports server side includes. To include another
document use the directive:
<% 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.
Page
Creation Tools
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.
Configuring ESP in Appweb
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:
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:
<Location /esp>
SetHandler espHandler
</Location>
Lastly, the directory to store uploaded files is configured with the FileUploadDir directive.
FileUploadDir /tmp
Configuring ESP in WebServer
When using ESP with WebServer, make sure you
have a version later than version 2.5 downloaded from http://www.mbedthis.com/webServer. ESP is
configured via the config.h header file.