Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WAR created using Clojure deployed on Apache Tomcat does not run Servlet

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.

like image 814
Arun R Avatar asked Dec 30 '22 07:12

Arun R


2 Answers

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.

like image 133
Paul Legato Avatar answered Dec 31 '22 21:12

Paul Legato


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.

like image 34
Arun R Avatar answered Dec 31 '22 21:12

Arun R