Getting Started With a Jetty Java Server
With components that are open source and available for commercial use, Jetty is a popular choice for Java development.
In this blog, we take a closer look at Jetty, how it works, what it’s used for, as well as provide tutorials for three use cases.
What Is Jetty Java?
Jetty is an open source Java web server, as well as a servlet container, that provides an application with the features required to launch and run an application servlet or API.
As we will discuss in this blog, Jetty has some unique deployment methods that can provide many benefits to development teams. The Jetty server is also maintained and sponsored by the Eclipse Foundation
How Does Jetty Server Work?
Earlier, I talked about Jetty having some unique deployment methods. What does that entail? Jetty has the ability to run an application like a traditional application server like Tomcat or Wildfly in what is called Standalone deployment.
This isn't really anything special, however, Jetty also has the functionality to operate as a servlet that is embedded in your existing application. This allows you to not run an application in Jetty but to run Jetty in your application.
Why would you ever run Jetty in your application as an embedded system? There are some distinct advantages, including better self-contained applications, the ability to test against server like application dependencies, more control of custom filters, headers and caching, and single object deployment. Essentially, these advantages improve application development time specifically in development as developers are able to simplify their manual testing.
That simplification can decrease the amount of time an application can take to start and run because the application is better contained and help promote a more microservice distributed architecture. Not only does the self-contained services simplify the deployment but plugins like Maven even further streamline their process because they can hard code, their configurations into their builds.
What Is Jetty Java Used For?
Jetty is used in a variety of different ways, from local development all the way to true enterprise deployment. Jetty is lightweight free server with a low memory footprint that prides its self on excellent scalability, which helps development teams scale their application throughout their software development lifecycle. Therefore, teams that want to leverage development into the cloud or use a healthy amount of microservices without changing their application architecture in deployment into production lean toward Jetty.
Not only is Jetty going to provide simplified configuration and fully contained services that add to the increased scalability, but Jetty’s low memory footprint further increases the scalability. That low memory footprint lowers unwanted overhead on an application. This allows a developer’s restart time to decrease significantly in local development, sometimes up to 16x faster, meaning a developer can now test more frequently without using up as much time waiting for services to start.
On top of more testing, a developer’s testing instantly becomes more relevant to production environment because of a more contained service, and also because the applications load is similar to that of the fully deployed application.
Can Jetty Java Run Real Applications?
As Jetty itself is largely just a container, it misses many components that would needed when running more serious apps. There is a Jetty distribution called “Hightide” that provides components and features that are needed to host apps requiring JNDI, JMX, annotations or Java EE integration.
Jetty 9 does not yet offer a distribution for Hightide, but for those who want to use Hightide, there is a distribution based on Jetty 8 available now. Jetty Hightide includes JNDI, JMX, annotations, and also JEE integrations. Even with all those awesome features, it remains lightweight — Hightide is only 24 megabytes in size. As with the regular Jetty installation, Hightide also provides numerous usage and how-to examples to help you understand and use its Java EE integration and features.Back to top
Jetty Tutorial #1: Setting Up Jetty as an Embedded Container
Now that we understand what Jetty is and that we can run real applications with Jetty, let’s look into actually how to do that. The below examples illustrate how easy it is to embed Jetty into an application.
Downloading and Installing Jetty
I downloaded the latest Jetty 9 stable build and discovered that it is only 8MB in size! What a blessing that it doesn’t come packed full of hundreds of megabytes of stuff that I don’t need, want, or care about. The process is easy after that:
- Download the installation package.
- Extract the archive.
- You are ready to go!
Throughout all versions, Jetty has preserved its simplicity in starting up:
java -jar start.jar
This command starts your Jetty Java app up using default settings, which are fine for the majority who want to quickly get their webapp up and running without fuss.
It is also possible to install Jetty on Linux using APT or YUM, but keep in mind that versions from repositories will always be a couple of levels behind the latest stable build.
Deploying Your Application
Deploying an application is relatively easy as well — just copy your .war under the webapps folder and you are done! Jetty finds the app and deploys it automatically.
You can also provide your own context XML files you may already be familiar with, using other app servers.
Jetty also provides a startup script called “jetty.sh” for *nix systems under the “bin” folder, but you can also live without it. That’s another beauty of Jetty Java — no magical startup scripts are actually needed. In this way, it is also easy to attach any javaagent to your application without messing around with environment variables or deep startup script changes.Back to top
Jetty Tutorial #2: Custom Jetty Server Configuration
Need a custom configuration? No worries at all — you can make your own XML based configuration file and pass it along with the startup command:
java -jar start.jar /path/to/your-jetty-conf.xml
One thing that I really love about Jetty is its simplicity in setting the HTTP port. If you want to use something else than 8080, then just put “jetty.port” inside the jetty.xml
Or, for those who are impatient, just start the instance with:
java -jar start.jar -Djetty.port=80
Easy, right? Also, it is possible to pass multiple configuration files to Jetty like configuration for HTTP and HTTPS, which is very useful for sharing snippets of config around a team.
java -jar start.jar /path/to/jetty.xml /path/to/jetty-ssl.xml
Jetty’s configuration is reflection-based. This means that all options in XML are actually corresponding to the Java class fields. You can just open the javadoc and start creating your own configuration with whatever options you need. You can screw something totally up or invent your own thread pool or a customized session handler.
As you can see below, the
Configure element uses
org.eclipse.jetty.server.Server as it’s class and will define a
threadpool as its constructor argument:
10 200 60000 false
Check out the javadoc for the Server class.
This way of extensive configuration actually provides you endless possibilities. This is probably the reason why Jetty is used in Google App Engine and Heroku (they probably need their own session and transaction management).
The biggest downside with this reflection-based, not very well-documented, approach is that you need to understand how Jetty works under the covers and learn some of its internals. But, if you know your server well, you’ll never run into anomalies and won’t let the server get out of control, right?Back to top
Jetty Tutorial #3: Using the Jetty Plugin for Maven
I’d say that Jetty’s tooling is quite well developed. It’s well integrated with Ant, Maven or can be integrated with the tool you like. Jetty has a very nice and convenient plugin for Maven (my own sample conf):
org.eclipse.jetty jetty-maven-plugin 10 src/main/resources/web-dev.xml /myapp
It is possible to configure your development environment very closely to the one that you are running in production. The maven plugin for Jetty allows you to configure almost everything in the pom.xml file.
Finally, run the following command to start the Jetty server.
Jetty has an Eclipse WTP plugin that enables you to run web apps directly from Eclipse like you may have seen with other appservers. However, the Eclipse Jetty WTP project is currently unmaintained.Back to top
Jetty is very developer friendly, but less DevOps/operations friendly. As mentioned, third party products are available that plug into containers to build manageable development and production environments at the same time, but this does not come out-of-the-box with the Jetty Java app.
On the plus side, developers are not getting lost with 500MB installation packages. In addition, DevOps can still view key metrics to understand what is going on, what is running where, and how healthy the application is.
As mentioned earlier, Jetty is not equipped with advanced management console and tools, making it hard to scale and configure in bigger environments. Operations teams may get frustrated and create inconsistencies when dealing with changes to some XML or property files on multiple servers.
Jetty still needs some better tooling, both on the management and development sides. But, after all, it is very handy server for developers and suitable for websites with higher performance and configuration demands.
Speed Up Your Development
Discover how JRebel can speed up development for your Jetty application. Start your 14-day free trial today.
Want to learn more about what technologies Java developers are using today? Our recent webinar examines the results of our 2020 Java Developer Productivity Report, with insights from our Java experts.
Not sure what application server you need to use on your next development project? We cover some of the most widely used application servers in the blogs below.
- Blog – Java Basics: What Is Apache Tomcat?
- Solutions – Need Tomcat Support? Talk to an expert!
- Blog – Java Basics: What Is WebLogic?
- Blog – Java Basics: What Is Wildfly?