一个非常简单的入门方法是制作一个在Tomcat或类似网站上运行的servlet,例如:
(ns servlet
((:gen-class :extends javax.servlet.http.HttpServlet))
(defn -doGet
[_ request response]
(.setContentType response "text/html")
(let w (.getWriter response)]
(.println w
(str "<html>"
"<head>"
"<title>Hello World!</title>"
"</head>"
"<body>"
"<h1>Hello "
(.getParameter request "Name")
"</h1>"
"</body>"
"</html>"))))
(defn -doPost [_ request response]
(-doGet nil request response))
然后在 WEB-INF 文件夹中创建一个 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Clojure Servlet</display-name>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
编译并打包成一场战争,它的行为就像一个普通的Java servlet。要在Tomcat上部署,只需将战争放在webapps文件夹中并启动tomcat即可。
此处提供了详细的示例 http://github.com/yogthos/clojure-maven-examples