Skip to content

Instantly share code, notes, and snippets.

@bchristenson
Last active December 10, 2015 12:38
Show Gist options
  • Select an option

  • Save bchristenson/4436025 to your computer and use it in GitHub Desktop.

Select an option

Save bchristenson/4436025 to your computer and use it in GitHub Desktop.
package test;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jruby.CompatVersion;
import org.jruby.embed.LocalContextScope;
import org.jruby.embed.ScriptingContainer;
public class JRubyEncodingServlet extends HttpServlet {
public static void main(String[] args) {
// Initialize the scripting container
ScriptingContainer container = new ScriptingContainer(LocalContextScope.SINGLETHREAD);
// Set the compatibility version to 1.9, for proper UTF-8 support
container.setCompatVersion(CompatVersion.RUBY1_9);
container.runScriptlet("# encoding: utf-8\nputs 'π'");
container.runScriptlet("# encoding: utf-8\nputs 'π'.bytes.to_a.inspect");
// Prints:
// π
// [207, 128]
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Initialize the scripting container
ScriptingContainer container = new ScriptingContainer(LocalContextScope.SINGLETHREAD);
// Set the compatibility version to 1.9, for proper UTF-8 support
container.setCompatVersion(CompatVersion.RUBY1_9);
container.runScriptlet("# encoding: utf-8\nputs 'π'");
container.runScriptlet("# encoding: utf-8\nputs 'π'.bytes.to_a.inspect");
// Prints:
// ?
// [63]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment