Monday, June 30, 2008

JRuby and Jetty

This guy's brilliant: http://kfahlgren.com/blog/2007/06/06/jruby-jetty/ so, I'm going to copy his code. Here are the steps to starting the Jetty app-server from a JRuby application. Before running the JRuby code, set the PATH and JRUBY_HOME environment variables and add jetty.jar, jetty-util.jar and the servlet-api.jar to your classpath as shown here.

set PATH=%PATH%;C:\dev\bin\jruby-1.0.2\bin

set JRUBY_HOME=c:\dev\bin\jruby-1.0.2

set CLASSPATH=%CLASSPATH%;C:\dev\bin\jetty-6.1.11\lib\jetty-6.1.11.jar;C:\dev\bin\jetty-6.1.11
\lib\jetty-util-6.1.11.jar;C:\dev\bin\jetty-6.1.11\lib\servlet-api-2.5-6.1.11.jar;

Create the JRuby script, jetty_example.jrb:

require 'java'
include_class 'javax.servlet.ServletException'
include_class 'javax.servlet.http.HttpServlet'
include_class 'javax.servlet.http.HttpServletRequest'
include_class 'javax.servlet.http.HttpServletResponse'

include_class 'org.mortbay.jetty.Server'
include_class 'org.mortbay.jetty.servlet.Context'
include_class 'org.mortbay.jetty.servlet.ServletHolder'
include_class 'org.mortbay.jetty.handler.AbstractHandler'

class SimpleHandler < AbstractHandler
   def handle(target, request, response, dispatch)
      response.setContentType("text/html")
      response.setStatus(HttpServletResponse::SC_OK)
      response.getWriter().println("<h1>Goodbye, cruel monoglot world!</h1>")
      request.setHandled(true)
   end
end

handler = SimpleHandler.new
server = Server.new(3000)
server.setHandler(handler)
server.start()

Run the script

jruby jetty_example.jrb


Open a browser to this URL: http://localhost:3000/

useful links Download Jetty Download JRuby