views:

456

answers:

2

Hi,

I created the sample WAR as given at the Compojure Getting Started Page and deployed it to Apache Tomcat 6.0.2 wepapps folder. The Web.xml I used is as below:

<web-app>
 <servlet>
   <servlet-name>myservlet</servlet-name>
   <servlet-class>myapp.MyServlet</servlet-class>
 </servlet>
 <servlet-mapping>
   <servlet-name>myservlet</servlet-name>
   <url-pattern>/</url-pattern>
 </servlet-mapping>
</web-app>

Accessing the URL throws the following error

java.lang.NullPointerException: Handler returned nil (maybe no routes matched URI)
    compojure.http.servlet$request_handler__72.invoke(servlet.clj:110)
    myapp.MyServlet$_service__108.invoke(MyServlet.clj:11)
    myapp.MyServlet.service(Unknown Source)

I have included the Clojure, Clojure contrib jars in the WEB-INF/lib folder.

Has anyone encountered similar issue with Clojure WARs on Apache Tomcat ?

The Servlet I am trying to run is :

 ;; src/myapp/MyServlet.clj
(ns myapp.MyServlet
    (:use compojure)
    (:gen-class
        :extends javax.servlet.http.HttpServlet))

(defroutes greeter
    (GET "/"
        (html [:h1 "Hello World"])))

(defservice greeter)

When I replaced the (defservice greeter) with

(run-server {:port 8080}
  "/*" (servlet greeter))

I am able to run this and access URL from browser.

However, when I run this from Apache Tomcat, I still face the same issue.

+3  A: 

1) Did you actually define routes? Sounds like an obvious place to start. Make sure your routing table exists and is correctly defined. You need something like:

(defroutes webservice
  (GET "/some-route/"
    some-function-name))) ;; more complicated variations are possible, of course

2) Try starting your app in Jetty. If it doesn't work under Jetty either, with the same error, then your problem is not in the WAR deployment but in the app.

(defserver webserver
             {:port 8080}
             "/*" (servlet webservice))
(start main-server) ;; starts a Jetty webserver on 8080

If neither of these fixes it, post some more information for us, such as your routing table.

Paul Legato
I added the following to test my Servlet and this runs fine.(run-server {:port 8090} "/*" (servlet greeter))
Icarus
+4  A: 

The problem happened to be related to the use of "/" in the defroutes instead of "/*". If I define the defroutes as :

(defroutes greeter
    (GET "/*"
        (html [:h1 "Hello World"])))

It works on Apache Tomcat 6.

Icarus