Winstone: The Smaller, Faster, Lighter Servlet Container

Mar 10 2010

A few years ago Bruce Tate and Justin Gehtland wrote a book entitled Better, Faster, Lighter Java.  In this book they argue that sometimes a simpler solution is the right answer.   As developers we often become attached to our "Golden Hammer" and try to use it for every purpose.  Large Java EE containers such as Weblogic, JBoss and WebSphere have a place in our toolbox, but sometimes what you need is a simple container which starts quickly and has a small footprint. 

Introducing Winstone

The Winstone project was born out of a desire for a servlet container without all of the bloat of a typical Java EE container.  The project is free and open source under a dual LGPL and CDDL license.  Winstone provides an extremely basic, and lightweight servlet container which starts up very quickly.   Weighing in at a paltry 167 KB for the "lite" version and 320 KB for the "full featured" version, it is one of the lightest weight servlet containers out there.  

With such a small footprint it is helpful to understand what Winstone can and can't do:

Winstone can:

  • Execute Servlets (JSR 154 / Spec 2.5 compliant)
  • Optionally compile and run JSPs (more on this later)
  • Be connected to Apache web server via an AJP13 connector
  • Provide SSL Connectivity
  • Be clustered (using Session Sharing)
  • Support limited Datasource definition.
  • Support two different embedding models.
  • Support Authentication Realms

Winstone does not:

  • Support the full range of Java EE specifications including JMS, EJB, and JavaMail.
  • Replace heavier weight appservers and servlet containers. 

Running a Java web applications is as simple as:

# WAR file:
java -jar winstone.jar --warfile=mywarfile.war

# Exploded WAR:
java -jar winstone.jar --webroot=/path/to/my/app

# Multiple apps:
java -jar winstone.jar --webappsDir=/path/to/webapps

JSP Support

JSP files ultimately get compiled into servlets, so pre-compiled JSPs will obviously work with no problem in Winstone.  More often than not, java web applications are deployed with JSPs which are compiled at runtime.  Winstone does not support JSP compilation out of the box, but it has provided hooks for using the Apache Jasper JSP compiler.    You must make the Jasper libraries available to Winstone.   To obtain the Jasper libraries, download a copy of Tomcat, and extract the necessary files.  Which version of Tomcat depends on which version of the JSP spec you wish to use.  The documentation suggests that you will need: jasper-compiler.jar, jasper-runtime.jar, ant.jar, jsp-aip.jar, commons-logging-api.jar and commons-el.jar.    

Using the Tomcat 6.0 binaries I was able to get it work with just: el-api.jar, jasper.jar, jasper-el.jar, jasper-jdt.jar, jsp-api.jar, and tomcat-juli.jar.   

Whichever version you use, place the files into a single folder.  Winstone will look in ./lib by default, but you can override this by using the 

--commonLibFolder commandline option. 

Finally execute Winstone with the --useJasper commandline option.  For example:

java -jar winstone.jar --warfile=mywarfile.war --commonLibFolder=path/to/my/libs --useJasper

Admittedly this adds a little weight to the Winstone "footprint" (2.1 MB for the above configuration), but it is still far lighter than many other solutions and can be avoided if your JSPs are pre-compiled. 

"Lite" vs "Full Featured"

The "Lite" version (167 KB) contains only the core servlet feature set.  If all you need is basic Servlet / JSP features, this is the way to go.  If you need features like clustering, AJP13, or Authentication you will want to use the full featured winstone jar (320 KB).

Applying Winstone

I've found Winstone useful for quick testing of applications, particularly Spring based webservices.  It starts up faster than most web browsers.  When I asked Rick Knowles, Winstone's author, about how his software was being used he told me that he's aware of people using it in the mobile space.  He also mentioned that Winstone's small memory footprint can be useful in virtualized environments when the hypervisor doesn't like to grant large blocks of contiguous memory.     

I encourage you to check out the project for yourself the next time you need a quick and simple way to execute your Java Web app.  I think you will find it a useful tool in your toolbox right there alongside your golden hammer. 

In a future blog post I will discuss methods for creating executable web applications through the two different embedding models that Winstone supports.  It's in this area that Winstone really shines.

Resources

Project Website: http://winstone.sourceforge.net/

About the Author

Macon Pegram's picture

Macon is a Senior Consultant with CapTech who has experience in a broad range of Java and Java EE technologies including Agile Processes, Architecture, Continuous Integration, Drools, Hibernate, Portal, Spring, Search, and Webservices.

Disclaimer

The words and opinions expressed here are those of each article's respective author, and do not necessarily represent the views of CapTech Ventures.