使用部署在 Apache Tomcat 上的 Clojure 创建的 WAR 不运行 Servlet

发布于 2024-08-16 07:23:20 字数 1472 浏览 12 评论 0原文

我创建了 Compojure 入门页面 中给出的示例 WAR 并进行了部署到 Apache Tomcat 6.0.2 wepapps 文件夹。我使用的 Web.xml 如下:

<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>

访问 URL 会引发以下错误

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)

我已将 Clojure、Clojure contrib jar 包含在 WEB-INF/lib 文件夹中。

有没有人在 Apache Tomcat 上遇到过 Clojure WAR 的类似问题?

我尝试运行的 Servlet 是:

 ;; 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)

当我替换 (defservicegreeter) 时,

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

我能够运行它并从浏览器访问 URL。

然而,当我从 Apache Tomcat 运行它时,我仍然面临同样的问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万劫不复 2024-08-23 07:23:20

1)你真的定义了路线吗?听起来是一个明显的起点。确保您的路由表存在并且定义正确。您需要类似的东西:

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

2)尝试在 Jetty 中启动您的应用程序。如果在 Jetty 下也不起作用,并出现同样的错误,那么你的问题不在于 WAR 部署,而在于应用程序。

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

如果这些都不能解决问题,请为我们提供更多信息,例如您的路由表。

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.

旧城烟雨 2024-08-23 07:23:20

该问题恰好与 defroutes 中使用 "/" 而不是 "/*" 有关。如果我将 defroute 定义为:

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

它适用于 Apache Tomcat 6。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文