Server-side-scripts.xhtml 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  3. <head>
  4. <title>Web page scripts</title>
  5. <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
  6. <link rel="stylesheet" type="text/css" href="kawa.css"/>
  7. <script src="kawa-ebook.js" type="text/javascript"/>
  8. <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
  9. <link rel="prev" href="Overall-Index.xhtml" title="Index"/>
  10. <link rel="next" href="Self-configuring-page-scripts.xhtml" title="Self-configuring web page scripts"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Web page scripts" epub:type="subchapter" id="Server-side-scripts">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Web page scripts</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <p>A Kawa <em class="firstterm">web page script</em> is a Kawa program that is invoked
  23. by a web server because the server received an HTTP request.
  24. The result of evaluating the top-level expressions becomes
  25. the HTTP response that the servlet sends back to the client, usually a browser.
  26. </p>
  27. <p>A web page script may be as simple as:
  28. </p>
  29. <pre class="screen">(format "The time is &lt;~s&gt;." (java.util.Date))
  30. </pre>
  31. <p>This returns a response of consisting of a formatted string
  32. giving the current time.
  33. The string would interpreted as <code class="literal">text/plain</code> content:
  34. The angle brackets are regular characters, and not
  35. HTML tag markers.
  36. </p>
  37. <p>The script can alternatively evaluate to XML/HTML node
  38. values, for example those created by <a class="link" href="XML-literals.xhtml" title="XML literals">XML literals</a>:
  39. </p>
  40. <pre class="screen">#&lt;p&gt;Hello, &lt;b&gt;&amp;(request-remote-host)&lt;/b&gt;!&lt;/p&gt;
  41. </pre>
  42. <p>In this case the response would be <code class="literal">text/html</code> or similar content:
  43. The angle brackets should be interpreted by the browser as HTML tag markers.
  44. The function <code class="literal">request-remote-host</code> is available (automatically)
  45. to web page scripts; it returns the host that made the HTTP request,
  46. which is then interpolated into the response.
  47. </p>
  48. <p>Following sections will go into more details about how
  49. to write web page scripts. You can do so in any supported
  50. Kawa language, including Scheme, BRL, KRL, or XQuery.
  51. </p>
  52. <p>A web server will use a URL mapping to map a request URL
  53. to a specific web page script. This can be done in a
  54. number of different ways:
  55. </p>
  56. <div class="itemizedlist" epub:type="list">
  57. <ul class="itemizedlist" style="list-style-type: disc; ">
  58. <li class="listitem" epub:type="list-item">
  59. <p>The easiest to manage is to use Kawa’s mechanism for
  60. <a class="link" href="Self-configuring-page-scripts.xhtml" title="Self-configuring web page scripts">Self-configuring page scripts</a>. Ths is especially
  61. easy if you the web server built in to JDK 6, since no
  62. configuration files are needed.
  63. You can also use a “servlet engine” like Tomcat or Glassfish.
  64. </p>
  65. </li>
  66. <li class="listitem" epub:type="list-item">
  67. <p>You can explicitly compile the web page script to a servlet,
  68. in the same way Java servlets are compiled.
  69. This can then be installed ("deployed") in a servlet-supporting
  70. web server, such a Tomcat or Glassfish. See <a class="link" href="Servlets.xhtml" title="Installing web page scripts as Servlets">Servlets</a>.
  71. </p>
  72. </li>
  73. <li class="listitem" epub:type="list-item">
  74. <p>You can run the servlet as a <a class="link" href="CGI-scripts.xhtml" title="Installing Kawa programs as CGI scripts">CGI script</a>.
  75. </p>
  76. </li>
  77. </ul>
  78. </div>
  79. <p>For details on how to extract information from the request
  80. see <a class="link" href="HTTP-requests.xhtml" title="Functions for accessing HTTP requests">HTTP requests</a>.
  81. For details on how the response is created see <a class="link" href="HTTP-response.xhtml" title="Generating HTTP responses">Generating HTTP responses</a>.
  82. If the response is HTML or XML, you may want to
  83. read <a class="link" href="Creating-HTML-nodes.xhtml" title="Creating HTML nodes">Creating HTML nodes</a>, or <a class="link" href="Creating-XML-nodes.xhtml" title="Creating XML nodes">Creating XML nodes</a>,
  84. or <a class="link" href="XML-literals.xhtml" title="XML literals">XML literals</a>.
  85. </p>
  86. <p>Here are some examples, starting with a simple <code class="literal">hello.scm</code>:
  87. </p>
  88. <pre class="screen">(response-content-type 'text/html) ; Optional
  89. (html:p
  90. "The request URL was: " (request-url))
  91. (make-element 'p
  92. (let ((query (request-query-string)))
  93. (if query
  94. (values-append "The query string was: " query)
  95. "There was no query string.")))
  96. </pre>
  97. <p>This returns two <code class="literal">&lt;p&gt;</code> (paragraph) elements: One using
  98. <code class="literal">make-element</code> and one using the <code class="literal">html:p</code> constructor.
  99. Or you may prefer to use <a class="link" href="XML-literals.xhtml" title="XML literals">XML literals</a>.
  100. </p>
  101. <p>The same program using KRL:
  102. </p>
  103. <pre class="screen">&lt;p&gt;The request URL was: [(request-url)]&lt;/p&gt;,
  104. &lt;p&gt;[(let ((query (request-query-string)))
  105. (if query
  106. (begin ]The query string was: [query)
  107. ]There was no query string.[))]&lt;/p&gt;
  108. </pre>
  109. <p>You can also use XQuery:
  110. </p>
  111. <pre class="screen">&lt;p&gt;The request URL was: {request-url()}&lt;/p&gt;
  112. &lt;p&gt;{let $query := request-query-string() return
  113. if ($query)
  114. then ("The query string was: ",$query)
  115. else "There was no query string."}&lt;/p&gt;
  116. </pre>
  117. <p>The <code class="literal">+default+</code> script in the <code class="literal">doc</code> directory is
  118. useful for reading the Kawa documentation using a browser.
  119. The script uses the <code class="literal">jar:</code> URL scheme to automatically extract
  120. and uncompress the pages from <code class="literal">doc/kawa-manual.epub</code>,
  121. which is in EPUB3 format. Read the script for usage instructions.
  122. </p>
  123. </section>
  124. <footer>
  125. <div class="navfooter">
  126. <p>
  127. Up: <a accesskey="u" href="XML-tools.xhtml">Working with XML and HTML</a></p>
  128. <p>
  129. Previous: <a accesskey="p" href="XML-literals.xhtml">XML literals</a></p>
  130. <p>
  131. Next: <a accesskey="n" href="Self-configuring-page-scripts.xhtml">Self-configuring web page scripts</a></p>
  132. </div>
  133. </footer>
  134. </body>
  135. </html>