I'm a bad, bad man
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Java Servlets are the JEE way of doing anything web based. They are pretty easy to write. Here's short example to read a query parameter, and display it.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ExampleServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, 
	 HttpServletResponse response)
        throws ServletException, IOException
    PrintWriter out = response.getWriter();
    String DATA = request.getParameter("DATA");

    if(DATA != null){
    } else {
      out.println("No text entered.");

And don't forget to set it up in web.xml:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4"


All pretty straight forward. What if we could harness this technique in the Ruby world? Rack is a minimal framework to make talking to a Ruby webserver nicer, and is pretty flexible and great to build frameworks on top of. Imagine if you would, an API like Java Servlets... but implemented on rack....

I present to you... racklets! Let's start by making a new ruby file, and putting it in a well known load path: WEB-INF/lib. So, we'll make WEB-INF/lib/example_racklet.rb :

require 'ragamuffin'
class ExampleRacklet < Ragamuffin::Racklet
  def do_get(request, response)
    data = request.params['DATA']
    if data
      response.write data
      response.write 'No data'

Next we need a web.xml to tell racklets what to load, and what urls to map it. This is kept in WEB-INF/web.xml:



Since we're on rack, you'll want a config.ru to run our ragamuffin web application.

require 'ragamuffin'
run Ragamuffin::WebApplication.new

This handles loading up the web.xml and updating $LOAD_PATH to include the WEB-INF/lib directory. So now we're in business to something like shotgun to run our server:

$ shotgun 
== Shotgun starting Rack::Handler::Mongrel on localhost:9393

Alternatively, you can use the included ragamuffin script to run it without a config.ru file.

$ bin/ragamuffin 
== Ragamuffin starting Rack::Handler::Mongrel on localhost:9393

Now go ahead and browse to http://localhost:9393/example and you'll see:

No Data

Give it the DATA parameter, and you'll see it displayed, ie http://localhost:9393/example?DATA=Hello%20World

Hello World