Friday, November 12, 2010

Jeans - Running Java Servlet and JSP Webapp in ASP.NET/IIS - Part 1

There are two main camps in enterprise server technology market, Java and .Net Framework. Java side has Servlet, JSP, and Java ServerFace. .Net Framework side has ASP.NET (WebForm and MVC). Both sides are not compatible with each other. This article will describe how to run JSP in IIS natively without connecting to a Java web container.

Mr. Jeroen Frijters has implemented an JVM in .NET, IKVM.NET. It seems to be a good start.

Jetty vs. Tomcat
The first attempt was compiling Jetty to execute web applications directly. Digging into the source code and after a few trials, I found that Jetty is using Jasper library, which is Tomcat's JSP engine, to run JSP. Given that .Net does not have the concept of class file, it is unlikely .NET is able to load the create new class and load it into memory in runtime.

JSP Precompiler
Jasper not only is the runtime engine of JSP, it also includes a JSP precompiler to compile JSP into Servlet. Using it is not hard but type the Java command is tedious. It makes sense to use IKVM to create a .Net executable. The Jasper library from Tomcat 5.5 is used:

  • ant.jar
  • catalina-ant.jar
  • commons-el.jar
  • commons-logging-api-1.1.1.jar
  • jasper-compiler-jdt.jar
  • jasper-compiler.jar
  • jasper-runtime.jar
  • jsp-api.jar
  • naming-factory-dbcp.jar
  • naming-factory.jar
  • naming-resources.jar
  • servlet-api.jar

Then compile the precompiler with this command:
ikvmc -classloader:ikvm.runtime.ClassPathAssemblyClassLoader -target:exe -main:org.apache.jasper.JspC -out:bin\jspc.exe *.jar

JSP Compilation
The command-line arguments of JspC is the same as the Java version. The following command can be used (%1 is the directory of the target web application):
jspc -uriroot "%1" -d out -compile -v -webxml "%1\WEB-INF\jspweb.xml" -trimSpaces -source 1.5 -target 1.5

The first JSP is a simple Hello World page to test out the JSTL EL:
Hello ${}!

Web Application Compilation
Everything are still in JARs and classes. However .Net Framework needs DLLs. The following command is executed to get everything in .Net assembly:
ikvmc -classloader:ikvm.runtime.ClassPathAssemblyClassLoader -recurse:%1\WEB-INF\classes -out:%1\bin\jsp.dll -reference:%1\bin\jspc.exe %1\WEB-INF\lib\*.jar

Servlet API, in .Net
Before running the DLL, the following classes are necessary in the Servlet API to run the JSP:
  • HttpServletRequest/Response
  • ServletInputStream/OutputStream
  • ServletRequestDispatcher
  • ServletContext
  • ServletConfig
and a few more adapters to implement Java I/O.

ASP.NET IHttpModule and IHttpHandler
The final bit is the ASP.NET module to initiate the configuration and the handler to execute the servlets/JSP.

I have made the source code available so you don't need to go through the whole process. Please visit

1 comment:

Jeroen Frijters said...

This is really cool. Thanks.