Servlets.xhtml 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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>Installing web page scripts as Servlets</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="CGI-scripts.xhtml" title="Installing Kawa programs as CGI scripts"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Installing web page scripts as Servlets" epub:type="subchapter" id="Servlets">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Installing web page scripts as Servlets</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <p>You can compile a Kawa program to a <a class="ulink" href="http://en.wikipedia.org/wiki/Java_Servlet" target="_top">Servlet</a>, and run it
  23. in a servlet engine (a Servlet-aware web server).
  24. One or more servlets are installed together as a web application.
  25. This section includes specific information for
  26. the Tomcat and Glassfish web servers.
  27. </p>
  28. <section class="sect2" title="Creating a web application" epub:type="division" id="idm139667870369168">
  29. <div class="titlepage">
  30. <div>
  31. <div>
  32. <h3 class="title">Creating a web application</h3>
  33. </div>
  34. </div>
  35. </div>
  36. <p>A <em class="firstterm">web application</em> is a group of data, servlets, and
  37. configuration files to handle a related set of URLs.
  38. The <a class="ulink" href="http://jcp.org/aboutJava/communityprocess/final/jsr315/index.html" target="_top">servlet specification</a>
  39. specifies the directory structure of a web application.
  40. </p>
  41. <p>Assume the web application is called <code class="literal">myapp</code>, and lives in a
  42. directory with the same name. The application normally handles
  43. requests for URLs that start with <code class="literal">http://example.com/myapp</code>.
  44. Most files in the application directory are used to handle
  45. requests with corresponding URL. For example,
  46. a file <code class="literal">myapp/list/help.html</code> would be the response
  47. to the request <code class="literal">http://example.com/myapp/list/help.html</code>.
  48. </p>
  49. <p>The directory <code class="literal">WEB-INF</code> is special. It contains configuration
  50. files, library code, and other server data.
  51. </p>
  52. <p>So to create the <code class="literal">myapp</code> application, start with:
  53. </p>
  54. <pre class="screen">mkdir myapp
  55. cd myapp
  56. mkdir WEB-INF WEB-INF/lib WEB-INF/classes
  57. </pre>
  58. <p>Copy the Kawa jar from the <code class="literal">lib</code> direcory.
  59. (You can also use a “hard” link, but symbolic links may not
  60. work, for security systems.)
  61. </p>
  62. <pre class="screen">cp <em class="replaceable"><code>kawa-home</code></em>/kawa-2.92_invoke.jar WEB-INF/lib/kawa.jar
  63. </pre>
  64. <p>If you build Kawa from source, you must specify the
  65. <code class="literal">--with-servlet</code> <a class="link" href="Source-distribution.xhtml#configure-options">configure option</a>.
  66. </p>
  67. <p>You should also create the file <code class="literal">WEB-INF/web.xml</code>.
  68. For now, this is is just a place-holder:
  69. </p>
  70. <pre class="screen">&lt;web-app&gt;
  71. &lt;display-name&gt;My Application&lt;/display-name&gt;
  72. &lt;/web-app&gt;
  73. </pre>
  74. </section>
  75. <section class="sect2" title="Compiling a web page script to a servlet" epub:type="division" id="idm139667870357024">
  76. <div class="titlepage">
  77. <div>
  78. <div>
  79. <h3 class="title">Compiling a web page script to a servlet</h3>
  80. </div>
  81. </div>
  82. </div>
  83. <p>Assume for simplicity that the source files
  84. are in the <code class="literal">WEB-INF/classes</code> directory, and make that the
  85. current directory:
  86. </p>
  87. <pre class="screen">cd .../myapp/WEB-INF/classes
  88. </pre>
  89. <p>Depending on the source language, you compile your script
  90. sing the <code class="literal">--servlet</code> switch:
  91. </p>
  92. <pre class="screen">kawa --servlet -C hello.scm
  93. </pre>
  94. <p>or:
  95. </p>
  96. <pre class="screen">kawa --servlet --krl -C hello.krl
  97. </pre>
  98. <p>or:
  99. </p>
  100. <pre class="screen">kawa --servlet --xquery -C hello.xql
  101. </pre>
  102. <p>This lets the web-application find the compiled servlets.
  103. Finally, you just need to add the new servlet to
  104. the <code class="literal">WEB-INF/web.xml</code> file:
  105. </p>
  106. <pre class="screen">&lt;web-app&gt;
  107. &lt;display-name&gt;My Application&lt;/display-name&gt;
  108. &lt;servlet&gt;
  109. &lt;servlet-name&gt;MyHello&lt;/servlet-name&gt;
  110. &lt;servlet-class&gt;hello&lt;/servlet-class&gt;
  111. &lt;/servlet&gt;
  112. &lt;servlet-mapping&gt;
  113. &lt;servlet-name&gt;MyHello&lt;/servlet-name&gt;
  114. &lt;url-pattern&gt;/hello&lt;/url-pattern&gt;
  115. &lt;/servlet-mapping&gt;
  116. &lt;/web-app&gt;
  117. </pre>
  118. <p>The <code class="literal">&lt;servlet&gt;</code> clause says that the servlet named
  119. <code class="literal">MyHello</code> is implemented by the Java class <code class="literal">hello</code>.
  120. The <code class="literal">&lt;servlet-mapping&gt;</code> clause says that a request
  121. URL <code class="literal">/hello</code> should be handled by the servlet named <code class="literal">MyHello</code>.
  122. The URL is relative to the application context path,
  123. so the actual URL would be <code class="literal">http://example.com/myapp/hello</code>.
  124. </p>
  125. </section>
  126. <section class="sect2" title="Installing a servlet under Tomcat" epub:type="division" id="idm139667870346784">
  127. <div class="titlepage">
  128. <div>
  129. <div>
  130. <h3 class="title">Installing a servlet under Tomcat</h3>
  131. </div>
  132. </div>
  133. </div>
  134. <a id="idm139667870345824" class="indexterm"/>
  135. <p>Apache’s <a class="ulink" href="http://tomcat.apache.org/" target="_top">Tomcat</a> is an open-source
  136. implementation of the servlet specifications.
  137. After you <a class="ulink" href="http://tomcat.apache.org/download-60.cgi" target="_top">download it</a>,
  138. uncompress it in some convenient location,
  139. which is commonly referred to as <code class="literal">$CATALINA_HOME</code>.
  140. </p>
  141. <p>To install your web application, copy/move its directory
  142. to be in the <code class="literal">$CATALINA_HOME/webapps</code> directory.
  143. Thus for the example above you would have
  144. a <code class="literal">$CATALINA_HOME/webapps/myapp</code> directory.
  145. </p>
  146. <p>To start or stop Tomcat use the scripts in <code class="literal">$CATALINA_HOME/bin</code>.
  147. For example to start Tomcat on a GNU/Linux system run
  148. <code class="literal">$CATALINA_HOME/bin/startup.sh</code>. This will start a web server
  149. that listens on the default port of 8080,
  150. so you can browse the above example at <code class="literal">http://localhost:8080/myapp/hello</code>.
  151. </p>
  152. <p>If you’re running Fedora GNU/Linux, you can use the <code class="literal">tomcat6</code> package:
  153. </p>
  154. <pre class="screen"># yum install tomcat6
  155. # export CATALINA_HOME=/usr/share/tomcat6
  156. </pre>
  157. <p>You can the manage Tomcat like other system services.
  158. You can install webapps under <code class="literal">$CATALINA_HOME/webapps</code>.
  159. </p>
  160. </section>
  161. <section class="sect2" title="Installing a servlet under Glassfish" epub:type="division" id="idm139667870337024">
  162. <div class="titlepage">
  163. <div>
  164. <div>
  165. <h3 class="title">Installing a servlet under Glassfish</h3>
  166. </div>
  167. </div>
  168. </div>
  169. <a id="idm139667870336064" class="indexterm"/>
  170. <p><a class="ulink" href="https://glassfish.dev.java.net/" target="_top">Glassfish</a> from Oracle/Sun
  171. is a open-source “application server” that implements
  172. Java EE 6, including the 3.0 servlet specification.
  173. After you <a class="ulink" href="https://glassfish.dev.java.net/downloads/3.0.1-final.html" target="_top">download it</a>, uncompress it in some convenient location.
  174. This location is called <em class="replaceable"><code>as-install-parent</code></em> in the
  175. <a class="ulink" href="http://docs.sun.com/app/docs/doc/820-7689/aboaa?a=view" target="_top">Quick Start Guide</a>.
  176. The commands you will use is most in <code class="literal"><em class="replaceable"><code>as-install</code></em>/bin</code>,
  177. where <em class="replaceable"><code>as-install</code></em> is <code class="literal"><em class="replaceable"><code>as-install</code></em>/glassfish</code>.
  178. </p>
  179. <p>To start the server, do:
  180. </p>
  181. <pre class="screen"><em class="replaceable"><code>as-install</code></em>/bin/startserv
  182. </pre>
  183. <p>or under under Windows:
  184. </p>
  185. <pre class="screen"><em class="replaceable"><code>as-install</code></em>\bin\startserv.bat
  186. </pre>
  187. <p>The default post to listen to is <code class="literal">8080</code>;
  188. you can the port (and lots of other properties)
  189. using the adminstration console at port <code class="literal">4848</code>.
  190. </p>
  191. <p>A web application does not need to be any particular
  192. location, instead you just install it with this command:
  193. </p>
  194. <pre class="screen"><em class="replaceable"><code>as-install</code></em>/bin/adadmin deploy <em class="replaceable"><code>appdir</code></em>
  195. </pre>
  196. <p>where <em class="replaceable"><code>appdir</code></em> is the application directory - <code class="literal">myapp</code> in the example.
  197. (Use <code class="literal">asadmin.bat</code> under Windows.)
  198. </p>
  199. </section>
  200. <section class="sect2" title="Servlet-specific script functions" epub:type="division" id="idm139667870323664">
  201. <div class="titlepage">
  202. <div>
  203. <div>
  204. <h3 class="title">Servlet-specific script functions</h3>
  205. </div>
  206. </div>
  207. </div>
  208. <p>The following functions only work within a servlet container.
  209. To use these functions, first do:
  210. </p>
  211. <pre class="screen">(require 'servlets)
  212. </pre>
  213. <p>You can conditionalize your code to check at compile-time for servlets,
  214. like this:
  215. </p>
  216. <pre class="screen">(cond-expand
  217. (in-servlet
  218. (require 'servlets)
  219. (format "[servlet-context: ~s]" (current-servlet-context)))
  220. (else
  221. "[Not in a servlet]"))
  222. </pre>
  223. <p>For a run-time check you can test if <code class="literal">(current-servlet)</code> is
  224. non-<code class="literal">#!null</code>.
  225. </p>
  226. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870319328" class="indexterm"/> <code class="function">current-servlet</code></p>
  227. <div class="blockquote">
  228. <blockquote class="blockquote">
  229. <p>When called from a Kawa servlet handler, returns the
  230. actual <code class="literal">javax.servlet.http.HttpServlet</code> instance.
  231. Returns <code class="literal">#!null</code> if the current context is not that of
  232. <code class="literal">KawaServlet</code>.
  233. (Hence this function also returns <code class="literal">#!null</code> if you compile a servlet
  234. “by hand” rather that using the <code class="literal">--servet</code> option.)
  235. </p>
  236. </blockquote>
  237. </div>
  238. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870313888" class="indexterm"/> <code class="function">current-servlet-context</code></p>
  239. <div class="blockquote">
  240. <blockquote class="blockquote">
  241. <p>Returns the context of the currently executing servlet,
  242. as an instance of <code class="literal">javax.servlet.ServletContext</code>.
  243. </p>
  244. </blockquote>
  245. </div>
  246. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870310432" class="indexterm"/> <code class="function">current-servlet-config</code></p>
  247. <div class="blockquote">
  248. <blockquote class="blockquote">
  249. <p>Returns the <code class="literal">ServletConfig</code> of the currently executing servlet.
  250. </p>
  251. </blockquote>
  252. </div>
  253. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870307008" class="indexterm"/> <code class="function">get-request</code></p>
  254. <div class="blockquote">
  255. <blockquote class="blockquote">
  256. <p>Return the current servlet request, as an instance of
  257. <code class="literal">javax.servlet.http.HttpServletRequest</code>.
  258. </p>
  259. </blockquote>
  260. </div>
  261. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870303584" class="indexterm"/> <code class="function">get-response</code></p>
  262. <div class="blockquote">
  263. <blockquote class="blockquote">
  264. <p>Return the current servlet response, as an instance of
  265. <code class="literal">javax.servlet.http.HttpServletResponse</code>.
  266. </p>
  267. </blockquote>
  268. </div>
  269. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870300160" class="indexterm"/> <code class="function">request-servlet-path</code></p>
  270. <div class="blockquote">
  271. <blockquote class="blockquote">
  272. <p>Get the servlet path of the current request.
  273. Similar to <code class="literal">request-script-path</code>, but not always the same,
  274. depending on configuration, and does <span class="emphasis"><em>not</em></span> end with a <code class="literal">"/"</code>.
  275. </p>
  276. </blockquote>
  277. </div>
  278. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870295824" class="indexterm"/> <code class="function">request-path-info</code></p>
  279. <div class="blockquote">
  280. <blockquote class="blockquote">
  281. <p>Get the path info of the current request.
  282. Corresponds to the CGI variable <code class="literal">PATH_INFO</code>.
  283. </p>
  284. </blockquote>
  285. </div>
  286. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870292384" class="indexterm"/> <code class="function">servlet-context-realpath</code> [<em class="replaceable"><code>path</code></em>]</p>
  287. <div class="blockquote">
  288. <blockquote class="blockquote">
  289. <p>Returns the file path of the current servlet’s "Web application".
  290. </p>
  291. </blockquote>
  292. </div>
  293. </section>
  294. </section>
  295. <footer>
  296. <div class="navfooter">
  297. <ul>
  298. <li>
  299. <b class="toc">
  300. <a href="Servlets.xhtml#idm139667870369168">Creating a web application</a>
  301. </b>
  302. </li>
  303. <li>
  304. <b class="toc">
  305. <a href="Servlets.xhtml#idm139667870357024">Compiling a web page script to a servlet</a>
  306. </b>
  307. </li>
  308. <li>
  309. <b class="toc">
  310. <a href="Servlets.xhtml#idm139667870346784">Installing a servlet under Tomcat</a>
  311. </b>
  312. </li>
  313. <li>
  314. <b class="toc">
  315. <a href="Servlets.xhtml#idm139667870337024">Installing a servlet under Glassfish</a>
  316. </b>
  317. </li>
  318. <li>
  319. <b class="toc">
  320. <a href="Servlets.xhtml#idm139667870323664">Servlet-specific script functions</a>
  321. </b>
  322. </li>
  323. </ul>
  324. <p>
  325. Up: <a accesskey="u" href="XML-tools.xhtml">Working with XML and HTML</a></p>
  326. <p>
  327. Previous: <a accesskey="p" href="Self-configuring-page-scripts.xhtml">Self-configuring web page scripts</a></p>
  328. <p>
  329. Next: <a accesskey="n" href="CGI-scripts.xhtml">Installing Kawa programs as CGI scripts</a></p>
  330. </div>
  331. </footer>
  332. </body>
  333. </html>