Sheerpower®
A Guide to the Sheerpower Language


Previous Contents Index


Chapter 20
SheerPower Web Scripting

SheerPower 4GL can be used as a very powerful web scripting language. SheerPower allows you to write simple to complex interactive web applications---fast---very fast!

SheerPower web scripting programs produce dynamic webpages, and can be embedded into HTML code, similar to PHP, ASP and Perl languages.

Programs written in other languages can be called from a SheerPower web scripting program (using the PASS Statement Section 10.8), and the database capabilities are extremely powerful (Chapter 15, Data Structure Statements).

The full power and simplicity of the entire SheerPower 4GL language is available for use when web scripting.

SheerPower Web Scripting: Immune to SQL Injection Attacks

SheerPower web scripting is immune to SQL injection attacks. This is because SheerPower does not mix data and code when doing database operations. With SheerPower 4GL, the data and the code are always separate and well defined---eliminating the possibility of SQL injection attack methods.

For more on SQL injection attacks, see http://en.wikipedia.org/wiki/SQL_injection

20.0.1 Chapter Overview

This chapter assumes you have experience using HTML code to develop webpages. The contents of this chapter is in the following structure:

Supported Script Tag Formats

Script and Code Area tags are defined by [[xxx]]. Using the [[xxx]] format allows created HTML files to be run through "pretty" programs and other HTML analysis tools without error.However, SheerPower also supports the <<xxx>> format of these tags as an alternative.

20.1 Running the Sample Script Programs

20.1.1 Sample Web Form Script Program

To see a SheerPower 4GL Web Script program in action, follow the steps below to run the following example script program located in C:\SheerPower\sphandlers\scripts\sp4gl\test.spsrc.

  1. Start the SPINS_WEBSERVER (if not yet started) by clicking on the Windows Start menu and select Run. Type the following path into the "Open" field:


        \sheerpower\sphandlers\spins_webserver.exe 
    


    Then click on the OK button. SPINS_WEBSERVER will start up in a Command Prompt window.

  2. Open the Windows Run program again ("Start" menu, then select "Run") and type the following into the "Open" field:


        http://localhost/test.spsrc 
    

The browser window will display the following form where a visitor can interactively search a database for information relating to a city name.

When the Submit button is pressed, the program retrieves the results and displays them in a table below the form.

20.1.2 Sample Matrix Web Script Program

Run the next sample web scripting program using the following steps:

  1. Start the SPINS_WEBSERVER (if not yet started) by clicking on the Windows Start menu and select Run. Type the following path into the "Open" field:


        \sheerpower\sphandlers\spins_webserver.exe 
    

    Then click on the OK button. SPINS_WEBSERVER will start up in a Command Prompt window.

  2. Open the Windows Run program again ("Start" menu, then select "Run") and type the following into the "Open" field:


        http://localhost/matrix.spsrc 
    

This sample program creates a two identical Multiplication Tables displayed in a web browser window; one table is generated with a SCRIPT AREA, and the other is generated with a CODE AREA.

Next, in the browser address bar, change the URL to be:


http://localhost/matrix.spsrc?highvalue=25 
 

This changes the multiplication tables to display 25 x 25 instead of the default 10 x 10 display. The program file is located in C:\SheerPower\sphandlers\scripts\sp4gl\matrix.spsrc.

20.1.3 How the Matrix Sample Program Works

When you invoke matrix.spsrc with the browser, two identical multiplication tables are generated and displayed. The table on the left is generated by a SCRIPT AREA, and the table on the right is generated by a CODE AREA. We'll first take a look at how the Script Area works.

When writing a SheerPower web scripting program it is important to note that you are always in either a SCRIPT area or a CODE area.

20.1.3.1 Script Area in Matrix.spsrc

In matrix.spsrc, the "Script Area" begins with HTML code to setup the table. Because neither the [[%script]] or [[%spcode]] tags were used to designate the area as script or code, SPINS Webserver sees the HTML code and outputs it directly to the browser, processing it as SCRIPT.


<h1><center>Multiplication Table<br>Written in SCRIPT</center></h1> 
 
<table border=2 cellpadding=5> 
  <tr> 
  <td>&nbsp; 

The next lines illustrate how SheerPower code can be embedded inside a Script Area to process and display in the webpage dynamically.


  [[for j=1 to highvalue]]
    <td bgcolor=yellow align=right>[[j]]
  [[next j]]

To embed the SheerPower code in a Script area, surround the code with the [[ ]] tags. SPINS Webserver sees the code between the [[ ]] tags and processes it accordingly.

The code above says tells SPINS Webserver to take the value of "j", display it inside a table cell, then increment the value of "j" by one and display the new value into the next table cell - until the value of "highvalue" is reached.

The following embedded SheerPower code shows how to display the result of an evaluated expression:


  <td align=right> [[=i*j]]

The "=" in front of the expression causes the result to be displayed in the browser. This is how the values of "i" and "j" are multiplied together to complete the multiplication table.

The complete Script Area used to create the multiplication table on the left side of the browser window is below:

Example 20-1 Matrix.spsrc Script Area

<h1><center>Multiplication Table<br>Written in SCRIPT</center></h1> 
 
<table border=2 cellpadding=5> 
  <tr> 
  <td>&nbsp; 
  [[for j=1 to highvalue]] 
    <td bgcolor=yellow align=right>[[j]] 
  [[next j]] 
  <tr> 
  [[for i= 1 to highvalue]] 
    <tr> 
    <td bgcolor=yellow align=right> [[i]] 
 
    [[for j=1 to highvalue]] 
      <td align=right> [[=i*j]] 
    [[next j]] 
  [[next i]] 
</table> 

20.1.3.2 Code Area in Matrix.spsrc

Next, let's look at the CODE Area used to generate the multiplication table displayed on the right side of the browser window.


[[
print '<td>' 
 
print '<h1><center>Multiplication Table<br>Written in CODE</center></h1>' 

The first line with the short tag "[[" by itself begins the Code Area. This ended the previous Script Area. Note that the tag is on a line all by itself. Alternatively, the [[%spcode]] tag could have been used.

The next lines in the Code Area are standard SheerPower PRINT statements used to send the HTML code directly to the browser.


[[ 
print '<td>' 
 
print '<h1><center>Multiplication Table<br>Written in CODE</center></h1>' 

Code used to perform the calculations required is written in standard SheerPower code format. SPINS Webserver processes it, and the PRINT statements are used to display the results in the browser.


for j=1 to highvalue
  print '<td bgcolor=yellow align=right>'; j; 
next j

After the last PRINT statement, the ending tag ]] is used on a line all by itself to signify the end of the Code Area:


print '</table>' 
]]

The complete Code Area used to generate the multiplication table displayed on the right side of the browser window is below:

Example 20-2 Matrix.spsrc Code Area

[[ 
print '<td>' 
 
print '<h1><center>Multiplication Table<br>Written in CODE</center></h1>' 
 
print '<table border=2 cellpadding=5>' 
print '<tr>' 
print '<td>&nbsp;' 
for j=1 to highvalue 
  print '<td bgcolor=yellow align=right>'; j; 
next j 
print '<tr>' 
for i= 1 to highvalue 
  print '<tr>' 
  print '<td bgcolor=yellow align=right>'; i 
  for j=1 to highvalue 
    print '<td align=right>'; i*j 
  next j 
next i 
print '</table>' 
]] 


Previous Next Contents Index