Execution of Arbitrary Code
An arbitrary code example and associated parameters can be submitted to a Zamples server for
possible compilation and execution. Each parameter and every component of the code example is
sent in a different HTML-encoded field in an HTTP GET or POST to the
URI /JspExplorer/zamples/run. Due to the length of the parameters, we recommend a GET
be used.
As with all Zamples REST calls, the common Zamples REST parameters
must be specified.
The result is returned in the HTTP response.
Some of the parameters specific to this REST call are optional (shown like this),
and some are mandatory (shown like this):
| Field Name |
Purpose |
| cmd |
For most sandbox types, this field contains the code to be executed.
The code might describe a complete program/class, or might only be a fragment
from a mainmethod body. For Applet sandboxes, this field
contains the HTML necessary to display the applet. |
| applet |
This field is only used to contain
applet Java code at present. Default value is the empty string. |
| closed |
Boolean indication if this is an open issue (help request) or a closed (resolved)
issue. Default value is false. |
| sandbox |
Contains the sandbox code for the code example.
This tells the Zamples server how to handle the code example. Default value is jsp.
Sandbox discovery informs you of all available sandboxes. |
| imports |
This field either contains command line parameters (for sandboxes that support
complete programs/classes, or include/import statements that
reference libraries that are necessary to compile and run the code example.
Default value is the empty string. |
| pre |
If set to true or 1, the result returned by the Zamples
server will be enclosed in <pre></pre> tags. Default value is false. |
| ranges |
Specifies lines of code to display to the user.
Default is to display all lines. |
| stylesheet |
Set to 1, 0, true or false depending if the stylesheet
is to be applied to the code example's output. Not necessary if your client
does not use HTML to display results. Default value is the empty string. |
The results are returned as a text/html page.
Running a simple code example
Remember that an HTML form submits a code example to Zamples for compilation and execution.
Here is a simple code example that just adds 1 + 1 in a JSP:
For submitting the form for compilation and execution, the action URI may either be /JspExplorer
or /JspExplorer/index.jsp - doesn't matter which.
If the Zamples server is on another system be sure to prepend the domain so that a fully qualified URL is specified,
like http://zamples.com/JspExplorer/.
If you click on the Try It! button above, the Zamplizer page appears. In the Zamplizer page, click on the HTML button,
and then you will see the HTML source that created the Try It! button.
Each of the hidden fields must be transmitted to the Zamples server in order to execute the code fragment. To do the same thing using the REST interface you must also specify Zamples user id and password, and submit to /JspExplorer/zamples/run instead of /JspExplorer.
Let's use Zamples to demonstrate this in an HTML form;
the following example will accomplish the same addition, but using the REST interface. Click on the Try It! button below
and then substitute your Zamples user id and password for myUid and myPwd. Next run the program and note that the answer is still 2.
|
<form action='/JspExplorer/zamples/run' class='zform' method='post'>
<input type=hidden name=cmd value='<%= 1+1 %>'>
<input type=hidden name=imports value=''>
<input type=hidden name=applet value=''>
<input type=hidden name=format value='0'>
<input type=hidden name=pre value='0'>
<input type=hidden name=stylesheet value='0'>
<input type=hidden name=clientUser value='myUid'>
<input type=hidden name=clientPassword value='myPwd'>
<input type=submit value='Try It!' class='zform'>
</form> |
Possible error messages
In addition to the common Zamples REST error status codes,
this Zamples REST call might generate the following error statuses and messages:
| 506 Unspecified failure |
An unknown error occured when compiling or executing the code example |
|