rds :: graphikos

[ home | photos | gallery | scheme | notes | blog | facebook | twitter | youtube | last.fm | myspace | del.icio.us | EverythingHerePlus ]

Kawa Servlets Using Tomcat on Mac OS X 10.3.* Client

Example Code and Tutorials Home

Example of running servlets created in Kawa on Mac OS X 10.3.* client. Currently this article doesn't do much more than reproduce the Kawa documentation for running servlets with the exception of OS X specific caveats.

Kawa Functionality Utilized
12.1 Writing web-server-side Kawa scripts
12.2 Installing Kawa programs as Servlets

Starting Tomcat on Mac OS X 10.3.* Client

As far as I know, Tomcat is not installed by default on 10.3.* Client. You must install it using the 'Mac OS X Xcode Tools' CD-ROM. Check the 'Java Application Servers Development' box under the custom install options during installation, or launch the 'ApplicationsServerDev.pkg' directly to install Tomcat.

Default install locations is a major reason for this document. OS X places the entire Tomcat installation in /Library/Tomcat rather than a more traditional unix-like path. Before starting Tomcat, make sure that Apache is running. See 'Personal Web Sharing' in the 'Sharing' pane of 'System Preferences.' Starting Tomcat and verifying the correct port is relatively simple:

[pismo:~] rds% /Library/Tomcat/bin/startup.sh
Using CATALINA_BASE:   /Library/Tomcat
Using CATALINA_HOME:   /Library/Tomcat
Using CATALINA_TMPDIR: /Library/Tomcat/temp
Using JAVA_HOME:       /Library/Java/Home

Like all things Java, it might take little bit to be up and running. Determining the port that Tomcat is running on from the terminal:

[pismo:~] rds% cat /Library/Tomcat/logs/catalina.out | grep 'on port' | tail -1
INFO: Starting Coyote HTTP/1.1 on port 9006

Tomcat is running on 9006 on my machine. To view the default Tomcat home page I type http://localhost:9006/ in my W3 client or http://pismo.local:9006/ to access it from any machine on my home network (Don't forget to add this port entry to OS X's Firewall). If this doesn't work, check the /Library/Tomcat/logs/catalina.out log to see what is wrong. If you are running the Kawa repl on OS X, the following will open the page discussed above.

#|kawa:1|# (define-namespace afm "class:com.apple.eio.FileManager")
#|kawa:2|# (afm:openURL "http://localhost:9006/")

Create and compile a web-server-side Kawa script as shown in the Kawa Manual §12.1. The hello.scm example works fine. Installation as discussed in §12.2, only differs in terms of path names. Copy or link Kawa to Tomcat's lib directory. Since I haven't fully read the Tomcat documentation for creating end user applications, I currently use its servlet example directory for testing Kawa servlets.

[pismo:~] rds% sudo ln -s /Users/Shared/share/java/kawa.jar /Library/Tomcat/shared/lib/
[pismo:~] rds% sudo cp hello.class /Library/Tomcat/webapps/examples/WEB-INF/classes/

The servlet is now accessible at either http://localhost:9006/examples/servlet/hello or http://pismo.local:9006/examples/servlet/hello. Future additions to this document will include determining a way to pretty print the generated HTML output without explicitly having #\newline after each line. This will be essential for debugging any non-trivial output. Also, the correct directory structure for an application so that it could feasibly be packaged up Web Application Archive format. Following is another trivial example and the output as seen by lynx.

(require 'http)
(require 'xml)
(response-content-type 'text/html)

(make-element 'html 
  (make-element 'head (make-element 'title "test"))
    (make-element 'body 
    (make-element 'p "The request URL was: " (request-url))
    (make-element 'p "The time is now: " (make <java.util.Date>))
    (make-element 'p
      (make-attribute 'align "center")
      (let ((query (request-query-string)))
        (if query
          (values-append "The query string was: " query)
          "There was no query string.")))
    (make-element 'p "this: " (list 'a 'b '()))
    (make-element 'ul
      (make-element 'li "current-servlet: " (current-servlet))
      (make-element 'li "current-servlet-context: " (current-servlet-context))
      (make-element 'li "current-servlet-config: " (current-servlet-config))
      (make-element 'li "servlet-context-realpath: " (servlet-context-realpath)))

   The request URL was: http://pismo.local:9006/examples/servlet/hello          
   The time is now: Thu Feb 26 09:27:51 PST 2004                                
                         There was no query string.                             
   this: a b                                                                    
     * current-servlet: hello@59990e                                            
     * current-servlet-context:                                                 
     * current-servlet-config:                                                  
     * servlet-context-realpath: /Library/Tomcat/webapps/examples