GWT and ASP.NET Can Work Together: a Proxy To Connect .NET and Java

GWT is primarily used in the Java world for obvious reasons: apart from being an excellent tool for creating RIAs (indeed it's my favorite one), it allows to develop both the server and the client part using a single language and a single IDE, and it even allows to share classes.

But GWT can be used with any server technology, and although those advantages do not apply anymore, it is an alternative that deserves serious consideration from developers of any server platform. I happen to be a GWT fan that works in a company that uses Microsoft products, so it seems obvious to me to try to combine both. After all, should we use Flex for example, we would still need to use two languages and two IDEs, so who cares. Same applies to all JavaScript toolkits and frameworks (please don't tell me that Visual Studio is a good JavaScript IDE: it even misses matching braces highlighting).

The main issue with an ASP.NET+GWT setup (as for any non-Java server part) is that one must run two servers while writing the application, for example the GWT development server and the ASP.NET development server. And when GWT makes a request to the server, it can only call the GWT server, due to the same origin policy that restricts JavaScript connections. That restriction does not apply to the compiled GWT code, that is pure JavaScript served by the ASP.NET server. But the GWT dev mode is essential, so a proxy is needed to redirect all requests to the ASP.NET server. After that proxy is in place, the setup works well, and since the server is outside GWT, you never need to restart the embedded Jetty (changes to server classes will trigger a restart of the ASP.NET server as usual).

Unfortunately, there's no easy solution to class sharing between ASP.NET and GWT, although it should be quite easy to write a code generator that creates POCOs and POJOs with the same properties and keep them synchronized. Alternatively, JSON can be used to transfer data.

I created a simple project that can be used as skeleton to develop an ASP.NET application that uses GWT. It is not limited to GWT, since regular Web Forms can be used too. After all, GWT is just JavaScript, so any combination will work. The project contains a proxy servlet, that can be used with any server, not only with ASP.NET. This project can be run using Visual Studio for the ASP.NET part and Ant, Eclipse or NetBeans for the Java part.

In Visual Studio, open the PROJECT_HOME/war as a web site. In Eclipse, import the whole project (you must install the Google Plugin for Eclipse).

To use Ant or NetBeans, download the GWT SDK and unpack it into a "gwt" folder inside the project, so that its JARS are available at PROJECT_HOME/gwt/*.jar. If you have GWT elsewhere, customize build.xml or the NetBeans project to adjust paths accordingly.

Before starting the GWT development mode, start the ASP.NET development server and modify web.xml to match your setup (you can also use IIS). Then run the development mode and connect to: http://127.0.0.1:8888/default.aspx?gwt.codesvr=127.0.0.1:9997.

Let me highlight some features of this setup:

  • the whole thing must be seen as a Java project, while the war folder can be opened as a web site in Visual Studio;
  • you can customize any aspect of the war folder as a regular Visual Studio web site project;
  • when you want to publish your application to the server, you must GWT-compile your Java code, then publish the war folder only;
  • the Java proxy allows to redirect GET and POST calls, preserving all headers, hence cookies, hence sessions (this is shown in the sample project);
  • GWT can be included in aspx pages, so GWT host pages can be generated dinamically;
  • if you get a java.net.ConnectException: Connection refused: connect when running GWT, it means that the ASP.NET server has stopped, or the web.xml has not been customized correctly (see the included README).

Let me know if this setup works for you. I'll show it to my colleagues and I hope to convince them to use GWT in some serious application. There is a project that should be start soon and that would be a perfect chance, and that's why I explored this opportunity.

Downloads

Archive gwtaspnet.zip
Mar 10, 2010 1.3MB

Update: I uploaded a video on YouTube to show how the sample project is run.

Yoon

Apr 23, 2010 7:14 PM

I just started this and it works fine with me. I want to do some real work with this.

Peerawat

May 3, 2010 8:11 AM

Oh! thank God finally I found the person who have same idea. I already pick and run your project. Its fine but in the second page that you ask us to fill in a GWT field and press the button "Get Server info" I already did but nothing happen. Is it a problem or this project is just a sample for us to get an idea? (personally this just an example I think)a

However thank you very much for your sample project. It is a very good illustration :)

Luciano

May 3, 2010 9:35 AM

Hi Peerawat,

it should work: when you hit "Get Server Info", you should get a popup. Please check that you modified the web.xml since you must match the port used by the ASP.NET development server.

Peerawat

May 7, 2010 9:03 AM

Well, now I already fix web.xml to my corresponse port which auto generate by visual studio and then I run the project in eclipse but it not work and it give me this error instead

HTTP ERROR: 500

http://localhost:1588/default.aspx?gwt.codesvr=127.0.0.1:9997

RequestURI=/default.aspx

Luciano

May 7, 2010 12:54 PM

Hi,

first of all make sure that your Visual Studio dev server is running (you can also use IIS if you want), then connect to the *Java* server, not to the VS one. If you are getting a 500 response from the Java server, you must look at its log to see what caused it.

Andreas Saurwein

Jun 3, 2010 2:48 AM

I actually did something deeper. Replaced the GWT-RPC with JSON-RPC and am now interfacing with a .net library on the IIS7 side with the GWT client.
This method requires zero changes to the GWT client and no forwarding at all.

Luciano Vernaschi

Jun 3, 2010 8:50 AM

I think that you should still need some forwaring during development (you never need it in production). My example uses JSON too, so I can't see the difference.

eguardiola

Oct 27, 2010 11:56 AM

I'm developing with .net and gwt for two years or more.

I'm using IIS. My gwt host page is server with IIS and i tell gwt dev mode to open that host page served by IIS (no server gwt option).

I'm using JSON RPC. On client using JSON support from GWT and JayRock on server.

I find easy to dev gwt with .net. No proxy, no problems, etc... only happy coding.

eguardiola

Oct 27, 2010 12:06 PM

I think the trick is that you must compile GWT module ONCE before to read it with IIS. This way the host page can load the gwt loader.

Luciano Vernaschi

Oct 27, 2010 5:25 PM

I think that the proxy is even more "transparent": you use Visual Studio and Eclipse just like you're used to. It's just a class that works behind the scenes and allows you to work without any trick.

eguardiola

Sep 22, 2011 11:26 PM
Comments are closed.

Search