Sophie

Sophie

distrib > Fedora > 14 > x86_64 > media > updates > by-pkgid > 71d40963b505df4524269198e237b3e3 > files > 836

virtuoso-opensource-doc-6.1.4-2.fc14.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
 <head profile="http://internetalchemy.org/2003/02/profile">
  <link rel="foaf" type="application/rdf+xml" title="FOAF" href="http://www.openlinksw.com/dataspace/uda/about.rdf" />
  <link rel="schema.dc" href="http://purl.org/dc/elements/1.1/" />
  <meta name="dc.title" content="3. Quick Start &amp; Tours" />
  <meta name="dc.subject" content="3. Quick Start &amp; Tours" />
  <meta name="dc.creator" content="OpenLink Software Documentation Team ;&#10;" />
  <meta name="dc.copyright" content="OpenLink Software, 1999 - 2009" />
  <link rel="top" href="index.html" title="OpenLink Virtuoso Universal Server: Documentation" />
  <link rel="search" href="/doc/adv_search.vspx" title="Search OpenLink Virtuoso Universal Server: Documentation" />
  <link rel="parent" href="quicktours.html" title="Chapter Contents" />
  <link rel="prev" href="vspquickstart.html" title="Dynamic Web Pages" />
  <link rel="next" href="qshostingplugs.html" title="Third-Party Runtime Typing, Hosting &amp; User Defined Types" />
  <link rel="shortcut icon" href="../images/misc/favicon.ico" type="image/x-icon" />
  <link rel="stylesheet" type="text/css" href="doc.css" />
  <link rel="stylesheet" type="text/css" href="/doc/translation.css" />
  <title>3. Quick Start &amp; Tours</title>
  <meta http-equiv="Content-Type" content="text/xhtml; charset=UTF-8" />
  <meta name="author" content="OpenLink Software Documentation Team ;&#10;" />
  <meta name="copyright" content="OpenLink Software, 1999 - 2009" />
  <meta name="keywords" content="" />
  <meta name="GENERATOR" content="OpenLink XSLT Team" />
 </head>
 <body>
  <div id="header">
    <a name="qsvspexamples" />
    <img src="../images/misc/logo.jpg" alt="" />
    <h1>3. Quick Start &amp; Tours</h1>
  </div>
  <div id="navbartop">
   <div>
      <a class="link" href="quicktours.html">Chapter Contents</a> | <a class="link" href="vspquickstart.html" title="Dynamic Web Pages">Prev</a> | <a class="link" href="qshostingplugs.html" title="Third-Party Runtime Typing, Hosting &amp; User Defined Types">Next</a>
   </div>
  </div>
  <div id="currenttoc">
   <form method="post" action="/doc/adv_search.vspx">
    <div class="search">Keyword Search: <br />
        <input type="text" name="q" /> <input type="submit" name="go" value="Go" />
    </div>
   </form>
   <div>
      <a href="http://www.openlinksw.com/">www.openlinksw.com</a>
   </div>
   <div>
      <a href="http://docs.openlinksw.com/">docs.openlinksw.com</a>
   </div>
    <br />
   <div>
      <a href="index.html">Book Home</a>
   </div>
    <br />
   <div>
      <a href="contents.html">Contents</a>
   </div>
   <div>
      <a href="preface.html">Preface</a>
   </div>
    <br />
   <div class="selected">
      <a href="quicktours.html">Quick Start &amp; Tours</a>
   </div>
    <br />
   <div>
      <a href="newadminui.html">Where to Start</a>
   </div>
   <div>
      <a href="qsclientcon.html">Client Connections</a>
   </div>
   <div>
      <a href="qsvdbsrv.html">Virtual Database Server</a>
   </div>
   <div>
      <a href="qswebserver.html">Web Server</a>
   </div>
   <div>
      <a href="qswebdav.html">WebDAV</a>
   </div>
   <div>
      <a href="qswebservices.html">Web Services</a>
   </div>
   <div>
      <a href="qstexpwsmodules.html">Exposing Persistent Stored Modules as Web Services</a>
   </div>
   <div>
      <a href="qsvsmx.html">VSMX - Virtuoso Service Module for XML</a>
   </div>
   <div>
      <a href="qssqltoxml.html">SQL to XML</a>
   </div>
   <div>
      <a href="qsnntp.html">NNTP</a>
   </div>
   <div>
      <a href="vspquickstart.html">Dynamic Web Pages</a>
   </div>
   <div class="selected">
      <a href="qsvspexamples.html">VSP Examples</a>
    <div>
        <a href="#simpleforms" title="Simple HTML FORM usage">Simple HTML FORM usage</a>
        <a href="#vspdbinout" title="Manipulating Database Data in VSP">Manipulating Database Data in VSP</a>
        <a href="#vspequi" title="Simple Tutorial">Simple Tutorial</a>
    </div>
   </div>
   <div>
      <a href="qshostingplugs.html">Third-Party Runtime Typing, Hosting &amp; User Defined Types</a>
   </div>
   <div>
      <a href="troutips.html">Troubleshooting Tips</a>
   </div>
    <br />
  </div>
  <div id="text">
    <a name="qsvspexamples" />
    <h2>3.12. VSP Examples</h2>
		
			<a name="simpleforms" />
    <h3>3.12.1. Simple HTML FORM usage</h3>

			<p>
We will start with a small example that shows a page that constitutes a FORM with data from the
user being sent when a submit button is pressed and then examine the elements and attributes
that are important to us at this stage.
</p>
			<p>
Consider the following piece of HTML:
</p>

<div>
      <pre class="programlisting">
&lt;HTML&gt;
  &lt;HEAD&gt;
    &lt;TITLE&gt;Simple FORM demo&lt;/TITLE&gt;
  &lt;/HEAD&gt;
  &lt;BODY&gt;
  &lt;FORM METHOD=&quot;POST&quot; ACTION=&quot;formdemo_receiver.vsp&quot;&gt;
    &lt;P&gt;Test form, type some info and click Submit&lt;/P&gt;
    &lt;INPUT TYPE=&quot;TEXT&quot; NAME=&quot;myInput&quot; /&gt;
    &lt;INPUT TYPE=&quot;SUBMIT&quot; NAME=&quot;submit&quot; VALUE=&quot;Submit&quot; /&gt;
  &lt;/FORM&gt;
  &lt;/BODY&gt;
&lt;/HTML&gt;
</pre>
    </div>

<p>
All elements contained in the FORM tag are associated with that form.
This is how the data and the submit button
know what to do next, and which data to send, by the attributes of the FORM tag.
</p>
			<p>
The METHOD attribute of the FORM TAG can be either GET or POST.  The GET method allows the form submission to
be contained completely in a URL which can be advantageous in that it permits bookmarking in browsers,
but it also prevents form data from containing non-ASCII characters and restricts the amount of form
data that can be handled.  The get method is limited by the maximum length of the URL that the
server and browser can process. To be safe, any form whose input might contain non-ASCII
characters or more than 100 characters should use METHOD=&quot;POST&quot;.
</p>
			<p>
With the POST method, the form input is submitted as an HTTP POST request with the form data sent in the
body of the request.  Most current browsers are unable to bookmark POST requests,
but POST does not entail the character encoding and length restrictions imposed by GET.
</p>
			<p>
The ACTION attribute of FORM specifies the URI of the form handler.  This will usually be another web page that
performs some action based on the data that is sent from the originating form.  The URI could point to the same page
as the data originated and for pages that perform a well defined small set of functions it usually does.
When a page needs to manage multiple states there needs to be some flow control that can determine how the page
was reached whether it arrived as a result of someone clicking on the submit button or it is the first time the page has
been visited.
</p>

			<p>
Now we will add some VSP to check the values of the parameters in the form.  VSP markup is typically
contained in &lt;?vsp ... VSP ... ?&gt; blocks.</p>

<div>
      <pre class="programlisting">
&lt;HTML&gt;
  &lt;HEAD&gt;
    &lt;TITLE&gt;Simple FORM demo&lt;/TITLE&gt;
  &lt;/HEAD&gt;
  &lt;BODY&gt;
  &lt;P&gt;Last value sent:&lt;/P&gt;

  &lt;?vsp
    http(get_keyword(&#39;myInput&#39;, params, &#39;no value&#39;));
   ?&gt;

  &lt;FORM METHOD=&quot;POST&quot; ACTION=&quot;formdemo.vsp&quot;&gt;
    &lt;P&gt;Test form, type some info and click Submit&lt;/P&gt;
    &lt;INPUT TYPE=&quot;TEXT&quot; NAME=&quot;myInput&quot; /&gt;
    &lt;INPUT TYPE=&quot;SUBMIT&quot; NAME=&quot;submit&quot; VALUE=&quot;Submit&quot; /&gt;
  &lt;/FORM&gt;
  &lt;/BODY&gt;
&lt;/HTML&gt;
</pre>
    </div>

			<p>
This is the same example as above that now uses the same page for the form handler and displays the parameters each time.
The first time you click the button will take you to the same page and will display whatever you typed in the field last time.
</p>
			<p>
The VSP block uses two functions nested.  The http function allows you to send data to the HTTP client, the browser.
What we send to the browser is the result of the get_keyword function.  The get_keyword function has 3 parameters, it searches
for the keyword-value pair (keyword=value) where keyword matches the first parameter (in this case &#39;myInput&#39;) in array passed in
the second parameter, and returns the value if one is found otherwise will return the 3rd parameter in the function &#39;no value&#39;.
The params array is a special array that contains all page parameters.
</p>
			<p>
Now we will extend this further to add some conditional control so that if a value was entered we can respond directly to it.
We will also use a variable this time, which must be declared first.
</p>

<div>
      <pre class="programlisting">
&lt;HTML&gt;
  &lt;HEAD&gt;
    &lt;TITLE&gt;Simple FORM demo&lt;/TITLE&gt;
  &lt;/HEAD&gt;
  &lt;BODY&gt;

  &lt;?vsp
	declare _myInput varchar;

	_myInput := get_keyword(&#39;myInput&#39;, params, &#39;no value&#39;);

    if (_myInput &lt;&gt; &#39;no value&#39;)
	{	http(&#39;&lt;P&gt;Hello, &#39;);
		http(_myInput);
		http(&#39;&lt;/P&gt;&#39;);
	}
	else
	{	http(&#39;&lt;P&gt;Please enter you name&lt;/P&gt;&#39;);
	}
   ?&gt;

  &lt;FORM METHOD=&quot;POST&quot; ACTION=&quot;formdemo.vsp&quot;&gt;
    &lt;P&gt;Test form, type some info and click Submit&lt;/P&gt;
    &lt;INPUT TYPE=&quot;TEXT&quot; NAME=&quot;myInput&quot; /&gt;
    &lt;INPUT TYPE=&quot;SUBMIT&quot; NAME=&quot;submit&quot; VALUE=&quot;Submit&quot; /&gt;
  &lt;/FORM&gt;
  &lt;/BODY&gt;
&lt;/HTML&gt;
</pre>
    </div>

			<p>
We will now extend this even further to control the whole contents of the page.  In this example we will see that VSP and HTML
can be interleaved.
</p>

<div>
      <pre class="programlisting">
&lt;HTML&gt;
  &lt;HEAD&gt;
    &lt;TITLE&gt;Simple FORM demo&lt;/TITLE&gt;
  &lt;/HEAD&gt;
  &lt;BODY&gt;

  &lt;?vsp
	declare _myInput varchar;
	declare Mode varchar;

	_myInput := get_keyword(&#39;myInput&#39;, params, &#39;no value&#39;);
	Mode := get_keyword(&#39;submit&#39;, params, &#39;&#39;);

    if (Mode = &#39;submit&#39;)
	{
   ?&gt;

    &lt;P&gt;Hello, &lt;?vsp http(_myInput); ?&gt;
	&lt;/P&gt;

  &lt;?vsp
	}
	  else
	{
   ?&gt;

  &lt;P&gt;Please enter you name&lt;/P&gt;
  &lt;FORM METHOD=&quot;POST&quot; ACTION=&quot;formdemo.vsp&quot;&gt;
    &lt;INPUT TYPE=&quot;TEXT&quot; NAME=&quot;myInput&quot; /&gt;
    &lt;INPUT TYPE=&quot;SUBMIT&quot; NAME=&quot;submit&quot; VALUE=&quot;Submit&quot; /&gt;
  &lt;/FORM&gt;

  &lt;?vsp
    }
  ?&gt;

  &lt;/BODY&gt;
&lt;/HTML&gt;
</pre>
    </div>

			<p>
</p>
		<br />

		
			<a name="vspdbinout" />
    <h3>3.12.2. Manipulating Database Data in VSP</h3>
			<p>The following example demonstrates a basic page that has form based
flow control, takes input from the user to put into the database and then displays the results.
This simple example can be extended to perform more substantial operations by adding a few more
inputs, buttons and states.
                        </p>
		<p>
Things to look at:
</p>
<ul>
      <li>get_keyword is used to extract parameters from the form that were posted last time</li>
      <li>the current mode is determined by the value of the submit parameter</li>
      <li>straight HTML can be inline with VSP flow control which is how the whole page is contained in an <strong>if</strong> condition.</li>
    </ul>

<div>
      <pre class="programlisting">
&lt;HTML&gt;
&lt;HEAD&gt;&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;form method=&quot;POST&quot; action=&quot;simpletest.vsp&quot;&gt;

&lt;?vsp
-- assumes that you have a table db..test_table(txt varchar(2000))

-- declare variables for use
declare _mode varchar;
declare _theValue varchar;
declare _stmt varchar;

-- get the current mode and continue accordingly
_mode := get_keyword (&#39;submit&#39;, params, &#39;default&#39;);

if (_mode = &#39;Submit&#39;)
{
  -- if a submit was detected then insert the value into the DB
  _theValue := get_keyword(&#39;myTxtBox&#39;, params, &#39;no comment&#39;);
  _stmt := sprintf(&#39;insert into db..test_table(txt) values(\&#39;%s\&#39;)&#39;, _theValue);
  exec (_stmt, &#39;&#39;, &#39;&#39;, &#39;&#39;, &#39;&#39;, &#39;&#39;, &#39;&#39;);

?&gt;
   &lt;P&gt;Thank you for your submission.&lt;/P&gt;
&lt;?vsp
} else {
?&gt;
   &lt;DIV&gt;
   &lt;DIV&gt;Simple test form, enter some text and hit submit.&lt;/DIV&gt;
   &lt;DIV&gt;&lt;textarea name=&quot;other&quot; rows=&quot;3&quot; cols=&quot;64&quot;&gt;&lt;/textarea&gt;&lt;/DIV&gt;
   &lt;/DIV&gt;

   &lt;DIV&gt;&lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot;&gt;&lt;/DIV&gt;

   &lt;HR /&gt;
   &lt;H2&gt;Values currently in table&lt;/H2&gt;
   &lt;TABLE&gt;
   &lt;?vsp
   for (select txt from db..test_table) do
     http(sprintf(&#39;&lt;TR&gt;&lt;TD&gt;%s&lt;/TD&gt;&lt;/TR&gt;&#39;, txt));
   ?&gt;
   &lt;/TABLE&gt;

&lt;?vsp
}
?&gt;
&lt;/form&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;
</pre>
    </div>

     <p>You may wish to offload some of the functionality of the page to a stored procedure
and call that from that page. You may do this to improve readability of the page or there may be
a series of functions that you repeat such as displaying a particular table in some format.
     </p>
     <p>You could used a procedure as follows:
     </p>

<div>
      <pre class="programlisting">
create procedure table_list()
{
   http(&#39;&lt;H2&gt;Values currently in table&lt;/H2&gt;&#39;);
   http(&#39;&lt;TABLE&gt;&#39;);
   for (select txt from db..test_table) do
      http(sprintf(&#39;&lt;TR&gt;&lt;TD&gt;%s&lt;/TD&gt;&lt;/TR&gt;&#39;, txt));
   http(&#39;&lt;/TABLE&gt;&#39;);
};
</pre>
    </div>

<p>
You could then call this in instead of defining the query and table layout as above.
</p>

<p>The aspects of VSP are explained in more detail in the following sections.</p>
<br />
<a name="vspequi" />
    <h3>3.12.3. Simple Tutorial</h3>
<p>The following example prints the result from executing explain:</p>
<div>
      <pre class="programlisting">
&lt;?vsp
  declare meta, data any;
  exec (&#39;explain (?)&#39;, null, null, vector (&#39;select * from sys_users&#39;),  0, meta, data);
  foreach (any row in data) do
  {
     http_value (row[0], &#39;p&#39;);
  }
?&gt;
</pre>
    </div>
<p>The vsp can be also written like this:</p>
<div>
      <pre class="programlisting">
&lt;?vsp
  declare meta, data any;
  exec (&#39;explain (?)&#39;, null, null, vector (&#39;select * from sys_users&#39;), 0, meta, data);
  for (declare i,l int, i := 0, l := length (data); i &lt; l; i := i + 1)
   {
     http_value (data[i][0], &#39;p&#39;);
   }


?&gt;
</pre>
    </div>
<br />

<table border="0" width="90%" id="navbarbottom">
    <tr>
        <td align="left" width="33%">
          <a href="vspquickstart.html" title="Dynamic Web Pages">Previous</a>
          <br />Dynamic Web Pages</td>
     <td align="center" width="34%">
          <a href="quicktours.html">Chapter Contents</a>
     </td>
        <td align="right" width="33%">
          <a href="qshostingplugs.html" title="Third-Party Runtime Typing, Hosting &amp; User Defined Types">Next</a>
          <br />Third-Party Runtime Typing, Hosting &amp; User Defined Types</td>
    </tr>
    </table>
  </div>
  <div id="footer">
    <div>Copyright© 1999 - 2009 OpenLink Software All rights reserved.</div>
   <div id="validation">
    <a href="http://validator.w3.org/check/referer">
        <img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0!" height="31" width="88" />
    </a>
    <a href="http://jigsaw.w3.org/css-validator/">
        <img src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!" height="31" width="88" />
    </a>
   </div>
  </div>
 </body>
</html>