Skip to content

Instantly share code, notes, and snippets.

@nicksieger
Created January 12, 2009 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nicksieger/46079 to your computer and use it in GitHub Desktop.
Save nicksieger/46079 to your computer and use it in GitHub Desktop.

Java JSP/Servlet integration scenarios with JRuby-Rack

  1. Forward from Rails to Servlet/JSP. See demo_controller.rb #index and attributes.jsp.
  2. Include Rails response in JSP output. See demo.jsp.
  3. Arbitrary rendering with servlet response from Rails. See demo_controller #not_found.
  4. Forwarding from servlets to Rails should work fine, but I haven't tried it.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Rails forwarding demo</title>
<link href="stylesheets/application.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body id="attributes" style="padding:10pt">
<h2>Redmine demo JSP: Request and session attributes</h2>
<dl>
<dt><tt>servlet_request["hello"] | request.getAttribute("hello")</tt></dt>
<dd><%= request.getAttribute("hello") %></dd>
<dt><tt>session["rails"] | session.getAttribute("rails")</tt></dt>
<dd><%= session.getAttribute("rails") %></dd>
</dl>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ taglib uri="urn:org.jruby.rack" prefix="jruby-rack" %>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Rails tag demo</title>
<link href="stylesheets/application.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body id="demo" style="padding:10pt">
<h2>Redmine demo JSP: Project activity</h2>
<jruby-rack:rails path="/projects/activity" params="layout=none"/>
</body>
</html>
class DemoController < ApplicationController
def index
servlet_request["hello"] = "world!"
session["rails"] = "Visible to java!"
forward_to "/attributes.jsp"
end
def not_found
render_with_servlet_response do |resp|
resp.sendError 404, "Sorry, couldn't find it"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment