Back to article index

Using Winstone servlet container with Eclipse

2007-05-10anonymous


Background and motivation

When it comes to simple web application development with Eclipse, I have been a keen user of Sysdeo’s Tomcat Eclipse Plugin. However, I must admit, that there are some things that could be easier. The installation procedure can be a little too complicated for webapp-beginners .

So, I have been looking for a simpler setup that I could use in developing and running my web applications. That is how I came across Winstone – a lightweight servlet container. Ok, this wasn’t the first server being “lightweight” and “simple to use” and that could be “easy to embed”, but these were the features I had been searching for so I decided to give it a try.

Running servlets

It looked very promising when I first started the IT Mill Toolkit demo war and it ran out-of-the-box:

C:\TEMP>java -jar winstone-0.9.8.jar itmill-toolkit.war

[Winstone 2007/04/27 17:48:54] - Beginning extraction from war file
[Winstone 2007/04/27 17:49:12] - No webapp classes folder found - C:\Temp\winstone\itmill-toolkit.war\WEB-INF\classes
[Winstone 2007/04/27 17:49:12] - Winstone Servlet Engine v0.9.8 running: controlPort=disabled
[Winstone 2007/04/27 17:49:13] - AJP13 Listener started: port=8009
[Winstone 2007/04/27 17:49:13] - HTTP Listener started: port=8080

 

Also, it worked nicely with the extracted war-archives. 

Running the Eclipse project using Ant

We have been using the following structure for a Java web application: 

  • WEB-INF/

    • src/

    • classes/

    • lib/

      • itmill-toolkit-4.0.1.jar

      • tmill-toolkit-src-4.0.1.jar
      • itmill-toolkit-themes-4.0.1.jar
    • web.xml
    • itmill-toolkit-license.xml
    <static html pages go here>

Mainly standard servlet stuff, with the exception that the src folder is also within the WEB-INF folder. This is something inherited from the Tomcat plugin, but it has proven to be very convenient.

As this is exactly what a servlet container expects, it is very seamless to run it. I added the winstone.jar file into the project together with a simple Ant build script:

  • server

    • winstone-0.9.8.jar

  • build.xml

In build.xml I created a target that ran Winstone and used the project as a web root folder:

<target name="run">
<java jar="server/winstone-0.9.8.jar" fork="true">
<arg value="--webroot=${basedir}"/>
</java>
</target>

 

This seemed to work pretty well, but there were still some problems:

  • Stopping the server was complicated. The server remained in the background and would not stop without contacting the Task Manager.

  • I was not able to debug my Java application as I’ve grown used to with Tomcat.

  • And as the debug did not work, nor did the hot code replace feature of JVM.


Something had to be done.


Running an embedded Java servlet container

So, I decided to create a small wrapper application around the Winstone and start it directly. This should enable the debugging and hot code replace feature of JVM.

Starting the server seemed simple enough:

// Assign launcher arguments
Map serverArgs = new HashMap();
serverArgs.put("httpPort", “8080”);
serverArgs.put("webroot", “c:/workspace/testproject”);
serverArgs.put("ajp13Port", “-1”);
serverArgs.put("controlPort", “-1”);
serverArgs.put("httpListenAddress", “127.0.0.1”);

try {
Launcher.initLogger(serverArgs);
new Launcher(serverArgs);
} catch (IOException e) {
e.printStackTrace();
}

And it worked just as I hoped it would! I was able to start the project and debug it using Eclipse JDT. Only some finishing touches like automatic webroot detection, cleaner packaging and automatically launching browser to correct address were missing.

Tadaa!

The final project structure looks like this:

  • WEB-INF/

    • src/

      • <Application source code go here>

    • classes/

      • <Eclipse automatically compiles here>

    • lib/

      • itmill-toolkit-4.0.1.jar

      • itmill-toolkit-src-4.0.1.jar

      • itmill-toolkit-themes-4.0.1.jar

    • web.xml

    • itmill-toolkit-license.xml

  • server

    • src/

      • com.itmill.toolkit.ServerLauncher.java

      • com.itmill.toolkit.BrowserControl.java

    • winstone-0.9.8.jar

Both the server/src folder and the winstone.jar have to be in the project build path so you can run the project as normal Java application.

Resources

 





Back to article index


Add Your Comment

Comments, corrections and suggestions about the content of this article are always welcome.