views:

1870

answers:

6

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 absolutely no experience setting up any Java-related web applications. In fact, I don't really have much experience with Java at all. Where do I start? I have lots of experience with Apache and the LAMP stack, and I know on Apache I would just use Fast-CGI in most cases, but I don't know the equivalent in the Java world (if there is one).

Basically, I just need help with setting up the server and getting it started. I understand (somewhat) how to deploy a pure Java application, but what about a pure Clojure application? How does that work? I guess, coming from a world where all web applications are written in scripting languages, this is all new to me.

I'm on a Windows box, with Eclipse as my IDE, so I prefer a plug-in to a command-line script, but I'll live either way. I don't really care which web server I use; I'm trying out Jetty right now just because it's lightweight.

Oh, and by the way, I don't want to use a Clojure framework such as Compojure. That would defeat the learning part of this.

Thanks in advance.

+2  A: 

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.

Timothy Pratley
Aha, the link for compiling a JAR is what I was looking for! I wasn't sure how Clojure was supposed to run on a web server. Like I said, I come from a scripting language background, where you just put the files in a specific directory and you're done. +1 :)
musicfreak
This might be useful also: http://markmail.org/message/mipzbehperyyd67j
Timothy Pratley
+1  A: 

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

tomjen
+1  A: 

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.

Hamza Yerlikaya
Yes, you understood my question correctly. I understand that this is what Compojure does, but *how* does it do it? I'm basically asking how all this works. I know I could just use Compojure, but I'm more trying to figure out how Java-based web applications work.
musicfreak
+12  A: 

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.

pmf
Hmm, okay, I'll take a look at Compojure's source code to get an idea of how this works. If I were seriously writing a full-blown web application, I'd probably use it, but I want to write this layer myself to introduce myself to the way Java servers work. +1
musicfreak
For a nice overview of the mapping between HTTP-response and -request to Clojure's data structures, as it is used in Ring and Compojure, I recommend reading http://github.com/mmcgrana/ring/blob/master/SPEC
pmf
+8  A: 

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

Yogthos
A: 

one late point FastCGI != CGI

  • CGI each time you get the url you start a process
  • FastCGI you have a back end server that is running your program and it starts once, think of it like apache calling out to tomcat using mod_jk.
ms4720
Sorry, I don't know what this has to do with my question...
musicfreak
I was commenting on the answer at "answered Sep 23 at 9:25" where FastCGI and regular CGI are confused. The jvm hit does not apply with FastCGI, for more details: http://www.fastcgi.com/drupal/, and here is the orignal white paper that explains the differences: http://www.fastcgi.com/drupal/node/6?q=node/15
ms4720