Creating Embedded Server Pages™

Embedded Server Pages (ESP) are a collection of technologies developed by Mbedthis to provide embeddable, standards-based dynamic web page generation. Similar in its ultimate goal to Active Server Pages (ASP) and Java Server Pages (JSP), Embedded Server Pages enables the easy creation of dynamic web pages by embedding JavaScript into HTML pages. Embedded Server Pages was designed exclusively to be suitable for embedding in applications and devices. It provides a close binding between your application and the web page to be displayed making it very easy to generate dynamic data.

Embedded Server Pages is designed to be a simple and convenient way to access dynamic data and script the generation of HTML as examples below demonstrate. ESP also allows the use of Post-Back where a single ESP page can operate as both the displayed HTML form that prompts for user input and also as the script that processes and accepts the user input. Post-Back is a very natural methodology that facilitates the placement of your application logic in one place (one web page).

Embedded Server Pages are compiled into pure JavaScript that are interpreted at run-time. Although slower than pure C code, the ESP JavaScript calls C routines for many functions so the overall result is very fast page generation.

The AppWeb ESP architecture also allows multiple scripting engines to be used at the same time. AppWeb is provided with server Embedded JavaScript, but other scripting engines such as TCL, Perl or Python may easily leverage this open scripting architecture in the future.

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. ESP tags delimit the JavaScript using the delimiters <% and %> to bracket the scripting code.

<% Script Code Here %>

This script code is executed and replaced with the script output on the server side. The client never sees the script logic. The following example demonstrates just a few of the constructs available using ESP.

Simple ESP Test Page



The HTML query string for this page is @@QUERY_STRING





<% for (i = 0; i < 3; i++) { %>

<% } %>
Line@@i



<% displayCurrentSecurityStats("myDb", 1); %>

<%
// Inside JavaScript blocks, we can put any server-side
// JavaScript we like

i = 2;
write("i is " + i); 
%>
 



As you can see, the page is standard HTML with ESP tag delimited JavaScript. 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.

How to Use ESP in HTML Pages

ESP pages typically have a ".esp" extension and are processed by the ESP AppWeb handler. ESP pages look like typical HTML pages with the exception that scripts may be specified within the page.

The ESP handler compiles ESP pages into pure JavaScript procedures that are executed when the client requests the page. When executed, the JavaScript procedures run and can display the relevant dynamic data.

Don't confuse ESP with client side JavaScript. A single page may have both. ESP JavaScript is between <% and %> tags and is replaced by the AppWeb server before it is ever sent to the client. Once the page reaches the clients browser, it will execute any client side JavaScript which is typically between tags.

Embedded JavaScript Procedures

JavaScript procedures look like C functions with the exception that the arguments are typeless. The procedures can return a result which can be assigned to a JavaScript variable.

Generally it is best to create general ESP procedures that can be used to access a range of dynamic data depending on the parameters provided to the script procedure. For example, it is better to create a dbRead procedure that reads specified database tables, columns and rows, rather than a dbReadFirstRowInCustomerTable.

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 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.

Standard ESP Procedures

AppWeb supplies some useful Embedded JavaScript procedures with the product. These may be called from within an ESP page without any C or C++ coding required.

Script Name
Syntax
Examples
Description
include
include(filename);
include("myLib.js");
Server side inclusion of a JavaScript library. The nominated file is read and parsed inline when the page is compiled by AppWeb.
write
write(args, ...);
write("Hello World");
write("

Today is ", date);

Write the given arguments back to the client (browser) in the place of the ESP script. Multiple calls to write and its derivatives may be used.
redirect
redirect(url, [code]);
redirect("notFound.html", 302);
redirect("pageTwo.esp");

Redirect the client's browser to a new page.
trace
trace(message);
trace(level, message);
trace(2, "Message at level 2");
Write a message to the AppWeb log.

Server Side Includes

The AppWeb ESP implementation also supports server side includes. To include another document use the directive:

<% include fileName %>

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 include JavaScript procedure:

<% include("myLib.js"); %>

In this case, the included file must contain pure JavaScript and NO ESP.

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.

Today's temperature is <% write(temp) %>



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:

Today's temperature is <% =temp; %>



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:

Today's temperatore is @@temperature



Post Back Forms

Post-back allows a single web page to be both the displayed HTML form and the script processing the user input.

The following HTML fragment demonstrates Post-back. It displays a simple form prompting the user for a name and address. If the user clicks Ok, then the page is re-run using the POST method. In that case, the processUserData() procedure is called with the entered name and address.

<%  language=javascript;

if (REQUEST_METHOD == "POST") {
if (ok == "Cancel") {
redirect(prev);

} else if (ok == "Ok") {
processUserData(name, address);
}

} else {
var name = "Your Name";
var address = "Your Address";
}
%>











Name
Address





Sessions and Cookies

ESP supports the automatic creation of user-sessions to store user application and state data on a per session basis. The session mechanism stores the session data locally in AppWeb and indexes the sessions using a session ID that is also stored in the client's browser using cookies. Once a session is created, subsequent requests from the client's browser will include the Session ID cookie which is then used by AppWeb to  locate and access the session data for requests from that client. AppWeb ensures that session data accesses are as fast as possible by storing session data at the server and by using a fast hashing index.

Session data stores are maintained for each user session and are automatically destroyed after a configurable timeout period of inactivity (usually 30 minutes). The SessionTimeout configuration directive may be used to modify this timeout period. A session data store corresponds to a unique browser instance. If a user creates multiple windows or browsing tabs, these may actually share a session.

Sessions may be explicitly manipulated, but are normally created automatically by storing a data item in the session store by using the setSessionData JavaScript procedure. The getSessionData procedure may be used to access a session data variable.

The following example demonstrates automatically creating the session data store, the session ID and storing values in the store.

<%  language=javascript;

if (REQUEST_METHOD == "POST") {
if (ok == "Cancel") {
redirect(prev);

} else if (ok == "Ok") {
setSessionData("name", name);
setSessionData("address", address);
}
} else {
var name = "Your name";
var address = "Your address";
trace("Session ID" + getSessionId());
}
%>


Session API
Example
Description
createSession
createSession();
Allocate a new session ID and create a new session data store.
destroySession
destroySession();
Destroy any existing session data store.
getSessionData
name = getSessionData("name", "default name");
Retrieve the value of a variable in the session data store.
getSessionId
id = getSessionId();
Get the session ID for this session.
setSessionData
setSessionData("name", "Joe Bloggs");
Set a variable's value in the session data store.
testSessionData
if (testSessionData("name")) {
    trace("name exists");
}
Test if a variable is defined in the session data store.
unsetSessionData
unsetSessionData("name");
Unsets and deletes the session data variable.

How to Create ESP Procedures

You can easily create Embedded Server Page procedures in both C and C++ languages.  See the ESP section of the Programmer's Guide for more details.


© Mbedthis Software LLC, 2003-2204. All rights reserved. Mbedthis is a trademark of Mbedthis Software LLC.