Zamples, Inc. logo
 Home   Search   Solutions   My Zamples   FAQ   News   Contact 
Zamples ID:
Password:
   
0 anonymous users;
20 users logged in.

Creation of an HTML "Try It!" Button

This REST URI allows you to programmatically create an HTML table containing HTML button that invokes the Zamplizer page, and to display the code that will appear in the Zamplizer next to the button.  You can either display the rendered HTML on a web page, or HTML encode it such that the HTML source is visible to the end user.

To create a rendered HTML Try It! button, pass the same parameters as for execution of arbitrary code in an HTTP POST to the URI /JspExplorer/zamples/tryit.  As with all Zamples REST calls, the common Zamples REST parameters must be specified. The result is returned in the HTTP response.  You can also pass the following additional parameters, described in greater detail below.

Parameter Name Purpose
encode HTML encodes the resulting output
zserver Specifies the Zamples server
ranges Specifies lines of code to display to the user.  (Not yet implemented for this REST call)  Default is to display all lines.

This example is rather recursive in nature.  The code shown creates a Try It! button when run.  The Try It! button itself creates another Zamplizer, which executes the code buried inside.  You must substitute a valid Zamples user id and password for myUser and myPassword, respectively, before the example will work.  As previously mentioned, parameters may either be specified as URL parameters or as hidden POST parameters.  You can also specify a different value for zserver:

<% String zserver = java.net.URLEncoder.encode(request.getScheme() + "://" + 
                       request.getServerName() + ":" + request.getServerPort()); %>
<form action='/JspExplorer/zamples/tryit?clientUser=myUid&clientPassword=myPwd&zserver=<%=zserver%>' 
      method='post' class='zform' target='zamplizer'>
   <input type=hidden name=cmd value='echo&#32;&quot;I&#32;love&#32;zamples!!!!&quot;'>
   <input type=hidden name=imports value='#&#32;Command&#32;line&#32;arguments&#32;go&#32;here'>
   <input type=hidden name=applet value=''>
   <input type=hidden name=format value='2560'>
   <input type=hidden name=pre value='false'>
   <input type=hidden name=stylesheet value='/StyleSheet.css'>
   <input type=submit value='Try It!' class='zform'>
</form>

The results are returned as a text/plain page.  Here is a typical example response, displayed as a rendered HTML button, from the Zamples server (but with an invalid clientUser and clientPassword combination, so it won't run):

The table and its contents have embedded CSS class elements, which you can define in your HTML page or a CSS stylesheet that is referenced from your rendered HTML page.  The definitions for these classes on the public Zamples server are:

   .ztable {
      margin-left:30px;
      border-width:thin;
      border-color:#cccccc;
      border-spacing:0;
      border-style:outset;
      border-collapse: separate;
   }
   .zcell { 
      border-width:thin;
      padding:8px;
      border-style:outset;
      border-color:#cccccc;
   }
   .zform {
      font-size:8pt;
      font-family:Verdana, Arial, Helvetica, helv, sans, sanserif;
      color:black;
   }
   .zcode { }

Here is a convenient online tool for tweaking HTML table CSS.  You may also want to redefine how standard HTML tags like <pre> are rendered in your CSS stylesheet.

By default, the HTML button points to http://zamples.com.  Should you wish the button to point to a different Zamples server, pass in the zserver parameter and set the value to the appropriate server.  Both the form action and the stylesheet parameter will point to the Zamples server that you specify.  You can specify the protocol (e.g. http or https), the domain (e.g. domain.com) and the port (e.g. 81).  It does not matter if you add a trailing slash or not.  If you specify the empty string, the generated code will point to the same server that the HTML is displayed on. Here are some sample values of the zserver parameter:

http://zamples.com           default
https://javazamples.com:81   port is specified
https://javazamples.com:81/  trailing slash is specified (doesn't matter)

To cause the HTML source to displayed instead of being renderable, add the encode parameter and set its value to true.  This causes the HTML to be HTML (re)encoded, which is useful when you want to display the HTML necessary to create an HTML button on an HTML page.

Just as with the HTTP POST for execution of arbitrary code, the parameters can be specified in the POST body and as URL parameters.  Thus you could URL-encode the zserver and encode parameters and pass them as URL parameters, while passing the HTML-encoded code example parameters in the body of the POST.

Possible error messages

In addition to the common Zamples REST error codes, this Zamples REST call might generate the following error messages:

506 Unspecified failure An unknown error occured when performing this operation


Listing Arbitrary Code Zamples Client REST Interface Specification Querying Code Examples in a Knowledge Base