23

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.

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.

5 Answers 5

17

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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
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 github.com/mmcgrana/ring/blob/master/SPEC
I would defineatily look into Ring, as it provides the Clojure equivalent to a Servlet and helps quite a bit with not needing to know more Java than is necessary. Every Clojure web framework I've seen sits on top of it, using it as a translation layer.
14

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

Comments

3

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.

1 Comment

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 :)
1

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.

1 Comment

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.