Sunday, March 8, 2009

Running JSP/Servlets under .NET in C#

Hi there,

Recently I've been amazed by the things you can accomplish by using the IKVM library. I took me quite a while to realize the actual potential that hides behind this kind of integration that IKVM provides (which is kind of stupid as I'm working in both technologies, just not at the same time).

Let's get to the point!

Here's a line that will compile jetty (tried with version 6.1.11) into a .NET assembly with everything required to start writing and hosting JSP and servlets:

ikvmc -target:library jetty-6.1.11.jar jetty-util-6.1.11.jar servlet-api-2.5-6.1.11.jar jsp-2.1\core-3.1.1.jar jsp-2.1\ant-1.6.5.jar jsp-2.1\jsp-2.1.jar jsp-2.1\jsp-api-2.1.jar

That produces a .NET assembly called jetty-6.1.11.dll which you should include along with IKVM.OpenJDK.ClassLibrary.dll and IKVM.Runtime.dll to make it work.

And now for the fun stuff! Here's a code snippet (actually it's a complete application) that starts up jetty, creates 2 contexts (one for the servlet under /servlet and one for the example web application in JSP under /jsp) and starts the server:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using java.io;
using java.lang;
using javax.servlet.http;
using org.mortbay.jetty;
using org.mortbay.jetty.servlet;
using org.mortbay.jetty.webapp;

namespace JettyServletAndJSPExample {
class Program {
static void Main(string[] args) {
Server server = new Server(8080);

// see: http://localhost:8080/servlet/whatever
Context servletContext = new Context(server, "/servlet", Context.SESSIONS);
servletContext.addServlet(new ServletHolder(new TestServlet()), "/*");

// see: http://localhost:8080/jsp/hello.jsp
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/jsp");
webapp.setWar("../../webapps/test");
webapp.setDefaultsDescriptor("../../etc/webdefault.xml");
server.addHandler(webapp);

server.start();
}
}

class TestServlet : HttpServlet {
protected override void doGet(HttpServletRequest req, HttpServletResponse resp) {
resp.setContentType("text/html");
PrintWriter o = resp.getWriter();
o.println("<h1>hello, world!</h1>");
o.close();
}
}
}


After that you have a full fledged, java-like (very much alike) server that can hosts servlets written in C# for .NET platform and at the same time to host applications written in pure JSP.

The obvious next step will be to extract the rendering engine alone to be used with ASP.NET MVC framework thus enriching the seamless integration between Java and the .NET world.

If you're lazy enough you can download the whole package here and start exploring it immediately.

Have fun!

6 comments:

Jonathan said...

Did you ever experiment further with extracting the rendering engine? It would be cool to run more java/asp/etc apps together on IIS. Also you could run PHP on IIS with Quercus in your setup.

Matthias Hryniszak said...

The point I was trying to make here was that it is possible to run Jetty without Java (so to speak) and not to provide .NET nor IIS with the JSP capabilities. However...
Recently I've found myself in a position that such a hybrid would be more than welcome. I've started experimenting with Jasper but with no luck this far.

Yasin Hasan Karanfil said...

Link to zip package is not working, can you update?

Matthias Hryniszak said...

The zip file with ready to compile example has been restored :)

Yasin Hasan Karanfil said...

Thanks Matthias, I tried servlets it is working, but not successful with war directory yet.Assuming i successfully run with war directory, do you think there is a way to convert this project to iis compatible project to run under shared iis hosting servers.

Matthias Hryniszak said...

Well... I didn't think about it so far. But if you're interested in doing so please try it out and share the results. Maybe we'll render the actual JVM obsolete :D