Clojure web application - where do I start?

前端 未结 5 1624
無奈伤痛
無奈伤痛 2021-01-30 18:14

So lately I\'ve been looking into Clojure, and I love the language. I would like to see if I can make a small web application in it, just to challenge myself. However, I have ab

5条回答
  •  抹茶落季
    2021-01-30 18:43

    A really simple way to get started is to make a servlet that runs on Tomcat or similar, for example:

    (ns servlet
    ((:gen-class :extends javax.servlet.http.HttpServlet))
    
    (defn -doGet
      [_ request response]
      (.setContentType response "text/html")
      (let w (.getWriter response)]
          (.println w
            (str ""
              ""
              "Hello World!"
              ""
              ""
              "

    Hello " (.getParameter request "Name") "

    " "" "")))) (defn -doPost [_ request response] (-doGet nil request response))

    then create a web.xml in your WEB-INF folder

    
    
    
    Clojure Servlet
    
    
    hello
    servlet
    
    
    hello
    /hello
    
    
    index.html
    
    
    

    compile and package this into a war, and it'll behave just like a regular Java servlet. To deploy on Tomcat, simply drop the war in the webapps folder and start tomcat.

    A detailed example is available here http://github.com/yogthos/clojure-maven-examples

提交回复
热议问题