Assembling your own Servlet Container by embedding Jetty

While JEE Application Servers definately serves its purpose there are times where a simple Servlet Container will do the job just fine. In fact for a very large proportion of JEE Java applications being developed today, a Servlet Container is all that is needed.

Once your application hits production features such as easy deployment, quick startup times, migration between development, system/functional testing, acceptance testing, performance testing, regression testing and finally through to production becomes more important. This migration needs to be as pain free and automated as possible!

Add to that the aspect of controlling the startup, shutdown and the available features of the container and the result might just be writing an application with an embedded Servlet Container.

Benefits of embedding Jetty

In an embedded configuration, your application is started through a simple public static void main(String[] args) method. This method is responsible for setting up the environment variables (everything from the applications port number to other webservice endpoint URLS) before starting the Servlet Container (Jetty's Server class).

As hard as that sounds it can be a rather trivial tasks if you use the tools available in the Java Toolbox:

  • Maven 2 or 3
  • Jetty 7
  • Maven Appassembler Plugin
  • Maven Assembly Plugin
  • Spring 2

Maven project structure

Normally with a development project based on Maven the source code is split into multiple modules (Maven Projects). A general modularization of project would be to split the application into a data access layer, a domain layer and a Web (GUI) layer.

((Schetch))

The Appassembler plugin will need its own Maven Project to adhere to Mavens goal of "One artifact per POM".

Dissecting the parts before assembly

I will try to go through each part of the assembly process in just enough details to be useful for this articles purpose before going through the actual assembly process. I will assume you are already familiar with Maven and Spring.

Appassembler

Appassembler is a maven plugin that will assemble your applications dependencies as well as generating scripts to execute the application in Windows, Unix, Linux and Macintosh environments. The dependencies are included in a separate "repo" directory that is structured much in the same way as the central maven repository. When building the application, Appassembler will put all necessary files inside the "target/appassembler" directory.

Assembly Plugin

This Maven plugin is used to generate a .zip file distribution (other formats are supported as well) file that contains the output from Appassembler.

Spring

I will assume basic Spring knowledge throughout this article, but I will go through how to configure Spring to utilize Java Properties to configure and set up the applications environment.

Jetty

Jetty will be used as the Servlet Container. One of Jettys major strengths is its great embeddability support. So, instead of having to deploy the finished application to an Application Server or a Servlet Containter, its possible to gain full control of the execution of the application.

"Some assembly Required"

Post Assembly - Configuration through jetty.xml and config.properties

s

Added by Joachim Haagen Skeie on 18 May @ 2:32 PM
 0 comments

Searching Random Java Blog

About Random Java Blog

Random Java is a Java-related blog where anything java related might come about.

Page & Feed Options
Random Java Blog News Feed View a printable version of the current page.
Recently Updated
by Joachim Haagen Skeie (30 May)
Assembling your own Servlet Container by embedding Jetty (Random Java Blog)
by Joachim Haagen Skeie (18 May)
Random Java Blog
by Joachim Haagen Skeie (18 May)
Side Panel Part Six (Random Java Blog)
by Joachim Haagen Skeie (18 May)
About (Random Java Blog)
by Joachim Haagen Skeie (18 May)
Home (Random Java Blog)

Popular Labels

No labels match these criteria.

Featured Articles

Please create a page named Featured Articles. It will serve the purpose of grouping featured articles as its children.

If you leave it empty a default Featured Article pane will be shown here, if you do provide content it will be shown here in full instead, or as an excerpt only if you mark it up appropriately with {excerpt} wiki tags.

Alternatively, you can create a page Side Panel Part Eight and it will be shown here in full, replacing this text and the Featured Articles link entirely.

Archive Links