Open Source Subnet An independent Open Source community View more

Power Java-based web apps with Jetty application server

While Apache continues to rule the web server roost, a host of lighter and zippier web and application servers may now be a better choice to host your high-performance web-based apps. They focus on delivering maximum performance while efficiently using your system's limited resources. One such web server is Jetty, which is garnering fans for its embedability.

Businesses need to look beyond Apache for hosting Java-based websites for a couple of reasons. First, websites built with Java don't need just a web server, they also need a servlet engine to invoke and run the Java servlet code that renders the site’s front end. Secondly, Apache is poorly suited for hosting modern web apps because of the way it processes incoming requests. Apache and other older web servers spawn a new thread for each request. Each thread then reads the request and calls a script that generates the page. It then hangs around for the script to complete, and performs any other tasks and final writes needed to finish answering the request.

This model isn't suitable for web apps that may be accessed by hundreds or thousands of users simultaneously. To get the best possible performance, a web server should limit the memory overhead of each thread and also use multiple threads to service each request. Web servers that do so are called fully asynchronous web servers. Their design makes them efficient enough to solve the classical C10K optimization problem, which is to say they can handle 10,000 simultaneous connections.

Newer web servers such as lighttpd, nginx, Cherokee, and Jetty have many advantages over their reliable and dominant brethren. Of these four, the most popular is nginx, which, as per Netcraft's December 2012 web server survey, powers more websites than Microsoft's Internet Information Services. However, nginx is typically used more for load-balancing other applications or servers and serving static content rather than as a standalone server. If you are a Java developer and have a Java web application in a .war file, you cannot directly deploy it with nginx. For this you need a Java application server, such as Jetty or Apache Tomcat.

Jetty vs. Tomcat

Although you can use either as a Java application server, Jetty offers some interesting advantages. You can run it as a standalone application server or just as easily embed it in an application, which is a pretty popular approach with web developers.

Also, while Tomcat has been evolving constantly to meet newer web development technologies, it hasn't kept up with how web developers now build their apps. That's primarily because of the reduced role of the servlet engine in many of the Java apps written today. Instead of processing requests themselves, in most modern web apps servlets are used only to route requests to rendering engines such as Google Web Toolkit or template engines such as Freemarker.

Many web developers now think of Tomcat as overkill. They don't need a servlet engine that provides logging, connection pooling, and other services. By contrast, Jetty lets web developers embed the servlet engine directly into their apps. It acts as a service within the app and processes servlet responses and requests.

Think of Jetty as a set of software components that can offer HTTP and servlet container services. You can plug in additional components and extend the server as per your setup. In fact, Google App Engine, which initially used Tomcat as its web server, switched over to Jetty for just this reason. Other tools and frameworks that take advantage of the embeddable nature of Jetty include Grails, Eclipse, Equinox, Felix, Continuum, JRuby, Tapestry, Cocoon, and GWT. Then there's the Jetty-Maven plugin, which which lets developers run web apps without first assembling their components into a .war file.

But Jetty's embeddable nature isn't the only factor that makes it a worthy alternative to Tomcat. As per its developer Greg Wilkins, Jetty has been designed to have a small memory footprint, which makes the server scalable. According to tests, Jetty performs well with many tens of thousands of HTTP connections and hundreds of thousands of simultaneous WebSocket connections. The web server supports the latest markup languages, techniques, and protocols for the Web, including CSS, AJAX, Comet, WebSocket, HTML5, and SPDY.

One obvious advantage of Jetty's approach is that you can develop an app using the same server that will eventually host the app. Also, Jetty is designed with scalability in mind and performs a lot better than Tomcat in situations involving many connections with significant idle times. Tomcat, on the other hand, performs best while handling requests without significant idle times. Furthermore, Jetty has a small memory footprint and is designed to use the New I/O API for handling intensive I/O operations. This allows Jetty to serve a larger number of users than Tomcat on any given amount of available memory. When serving static content, Jetty uses advanced memory-mapped file buffers to deliver them faster without eating up resources.

Running Jetty

While you can embed Jetty inside your web app, you can also use it as a standalone server to test your existing Java web apps. To do so, download the latest version and extract the archive in an appropriate location, such as /opt/jetty. The extracted directory will contain various files and directories, such as bin/, which contains shell scripts to run Jetty; etc/, which houses all configuration files; and lib/, which houses all the JAR files required to run Jetty. Also make sure you have Java installed before you run Jetty.

To start Jetty on the default port 8080, change to /opt/jetty and type java -jar start.jar. You should then be able to access Jetty at http://localhost:8080.

By default, Jetty automatically deploys any .war files under the webapps/ directory, which to start includes a few test apps that you should now be able to run from a web browser. If you place a .war file under the webapps/ directory, it is hot-deployed to the server. Follow this detailed configuration guide to set up the various components of the Jetty server as per your environment. If you need more help to get started, there's plenty of information and documentation on using Jetty on the Web.

If you are a Java developer, Jetty gives you lots of reasons to look beyond Tomcat. It has everything you need in an HTTP server and Java servlet container. It supports the latest Java Servlet API and newer protocols such as SPDY. Jetty developers are involved in the development of new Internet standards by actively participating in the Java Community Process (JCP) program and even with the Internet Engineering Task Force (IETF).

To comment on this article and other Network World content, visit our Facebook page or our Twitter stream.
Must read: Hidden Cause of Slow Internet and how to fix it
Notice to our Readers
We're now using social media to take your comments and feedback. Learn more about this here.