Clojure web application - where do I start?

前端 未结 5 1623
無奈伤痛
無奈伤痛 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 "<html>"
              "<head>"
              "<title>Hello World!</title>"
              "</head>"
              "<body>"
              "<h1>Hello "
              (.getParameter request "Name")
              "</h1>"
              "</body>"
              "</html>"))))
    
    (defn -doPost [_ request response]
      (-doGet nil request response)) 
    

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

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

    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

    0 讨论(0)
  • 2021-01-30 18:45

    I'd recommend you start by learning the Servlet-API, which backs all things related to HTTP-requests and responses in the Java world. HttpServletRequest and HttpServletResponse cover a lot of ground here. Jetty is a nice choice here; there's a good introduction about Clojure and Jetty at http://robert.zubek.net/blog/2008/04/26/clojure-web-server/ (using Jetty 6).

    That being said, Compojure's basic model is pretty low-level too: it just wraps the requests and responses in Clojure-datastructures, but you are still responsible for all routing, generating the right response codes. generating an ETag etc., which is sometimes more low-level stuff than with a LAMP-stack.

    0 讨论(0)
  • 2021-01-30 18:46

    Well you can properly use FastCGI directly from clojure. FastCGI is a pretty simple protocol so it shouldn't be that difficult to write a server in clojure yourself (I doubt there is a library to do this for clojure, but there might well be one for Java).

    0 讨论(0)
  • 2021-01-30 18:48

    One thing to note if you are going to go with FastCGI is java is not like other scripting languages there is a start up time for starting up the JVM unlike say ruby or python. And it is a heavy operation to start JVM for each request.

    If i understand you question correctly you are looking for a native java way for creating applications. If so compojure does exactly that it creates a servlet for you behind the scenes so in the end you can create a clojure web application just like the ones in java and deploy it on to any application server.

    0 讨论(0)
  • 2021-01-30 18:49

    If you don't want to use Compojure or others then You'll either need to have the webserver load and call your JAR, or write a webserver using sockets. In that sense you can follow any of the many guides on the web for setting up, and compile a JAR

    This looks like what you are after.

    0 讨论(0)
提交回复
热议问题