Skip to content


john_brock edited this page Jan 6, 2011 · 1 revision

Table of Contents

This was conceptual work at an early phase of the project.

This has now been superseded by rails-integration which goes a long way towards automating the process.

Java web applications are typically packaged as WAR files in preparation for distribution and deployment to J2EE servers. It will be useful to be able to package Ruby on Rails applications in a similar form, to enable seamless deployment to Java servers.

The entry point for a web application is through a servlet. This will fill the role of Rails dispatcher.rb, and must be responsible for initializing the JRuby environment, and dispatching the request to an appropriate controller. From this point there are at least two approaches:

  1. Fill the environment, and call the usual Rails CGI dispatcher
  2. Implement the Rails request/response interfaces using the Java Servlet API

The first approach has been used successfully by others, however for this I chose to take the second approach, because it allows the existing Java Servlet functionality to be easily leveraged for other uses, such as session handling.

Quick start

The sections below explain how things are setup. To get started quickly, just download the package and follow these steps:

  1. Modfiy pom.xml and set the artifactId (this is also the name of the WAR)
  2. Replace src/main/rails with your Rails app
  3. Modify src/main/webapp/WEB-INF/web.xml, and add a servlet-mapping for each of your routes/controllers
  4. Build with mvn package
  5. Try it out with mvn jetty:run-war, and point your browser to http://localhost:8080/

Project layout

I've used a Maven 2 project layout so that Maven can assembly the WAR, however other options are certainly possible.

Location Description
/myrailsapp/pom.xml the Maven 2 project descriptor
/myrailsapp/src/main/rails this is where your rails application goes
/myrailsapp/src/main/webapp/WEB-INF/web.xml the webapp descriptor

Additionally the dispatcher code has been placed in these two folders. Ideally this code would be either in a separate library or as part of JRuby, so these paths would not be required.

Project descriptor

The project descriptor must assemble the WAR file such that the dispatcher can easily find the Rails code.

We'll require at least the following dependencies:


We need to direct the war plugin to arrange the extra directories correctly.


Only the 2.0.2+ versions of the war plugin support targetPath, so let's include the snapshot repository.

		<name>Maven Plugin Snapshots</name>


A standard rails application can be placed under /myrailsapp/src/main/rails.

To do this, just run "rails myrailsapp/src/main/rails".

After this is created, you may need to ensure that any empty directories (e.g. logs), have at least one file in there. One way is to add a README file describing what the directory is for. This is because the war plugin will ignore empty directories.

Web application descriptor

The web application descriptor (web.xml) so be placed in /myrailsapp/src/main/webapp/WEB-INF/web.xml.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns=""






This dispatcher is where the real integration work occurs. This is a two-step process:

  • RailsServlet, the Java servlet and entry point for the webapp.
  • java_servlet_dispatcher.rb, the Ruby implementation of the Rails dispatcher, request and response classes, which makes use of the Servlet API.


RailsServlet must perform the following actions:

  1. Find the Ruby home directory and Rails application
  2. Setup the initial load paths
  3. Initialize JRuby
  4. Initialize Rails by requiring "rails/config/environment" (not "rails/config/environment.rb")
  5. Create an instance of java_servlet_dispatcher.rb
  6. On every request, delegate to JavaServletDispatcher.dispatch, passing through HttpServletRequest and HttpServletResponse


JavaServletDispatcher must extend the Rails Dispatcher, however, rather than following the normal process of using the CGI request/response objects, it will construct instances of Rails request and response objects which are implemented using the Servlet API. These wrapper classes are called JavaServletRequest and JavaServletResponse.

JavaSession is also included, this is used by JavaServletRequest, and implements Rails session handling using the HttpSession.

Clone this wiki locally