Home » Enterprise Java » jetty » How to Install Jetty Application Server

About Ibrahim Tasyurt

Ibrahim Tasyurt
Ibrahim is a Senior Software Engineer residing in Ankara,Turkey. He holds BSc and MS degrees in Computer Engineering from Middle East Technical University(METU). Throughout his professional carrier, he has worked in Enterprise Web Application projects for public sector and telecommunications domains. Java EE, Web Services and Enterprise Application Integration are the areas he is primarily involved with.

How to Install Jetty Application Server

Jetty is an open-source Servlet container and Application Server which is known to be lightweight, portable, robust, flexible, extensible and providing support for various technologies like SPDY, WebSocket, OSGi, JMX, JNDI, and JAAS. Jetty is very convenient for development and also widely used in production environments.

In this post, we are going to detail how to install and configure a Jetty Server. We are first going to describe how to setup and run a standalone Jetty. Thereafter we will mention some configuration options and skim through the modular architecture of Jetty.

Jetty presents Standalone, Embedded and Jetty Maven Plugin modes of operation. In this post we are going to use standalone Jetty.

1. Environment

In this post, we are going to use following environment:

  • Java 8
  • Jetty 9.3.2.v20150730

However, it should be noted that; the material presented in this post is applicable for any Java versions later than Java 5, and any Jetty installations of version 9.x.y. Jetty does not require any 3rd party libraries except having Java installed in your PATH.

2. Downloading Jetty

Jetty binaries can be downloaded from the Jetty Homepage. Binaries are available in zip and tgz formats. Jetty is fully cross-platform so same binaries are valid for both Java and Unix environments.

3. Running Jetty

After downloading the binaries, having your Jetty server up and running is really easy. First you have to extract the zip (or tgz) archive to a convenient directory. After extracting the binaries, you have to navigate to the directory (jetty-distribution-9.3.2.v20150730 in this example). We will call it JETTY_HOME from now on in this post.

In JETTY_HOME you have to run the following shell command in order to start Server:

java -jar start.jar

This command yields to following output:

2015-08-30 20:57:07.486:INFO::main: Logging initialized @361ms
2015-08-30 20:57:07.541:WARN:oejs.HomeBaseWarning:main: This instance of Jetty is not running from a separate {jetty.base} directory, this is not recommended. See documentation at http:www.eclipse.orgjettydocumentationcurrentstartup.html
2015-08-30 20:57:07.688:INFO:oejs.Server:main: jetty-9.3.2.v20150730
2015-08-30 20:57:07.705:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:Usersibrahimjcgexamplesjettyjetty-distribution-9.3.2.v20150730webapps] at interval 1
2015-08-30 20:57:07.729:INFO:oejs.ServerConnector:main: Started ServerConnector@7a1d7e18{HTTP1.1,[http1.1]}{0.0.0.0:8080}
2015-08-30 20:57:07.730:INFO:oejs.Server:main: Started @606ms

Seeing the last line like 2015-08-30 20:57:07.730:INFO:oejs.Server:main: Started @606ms means that: you have successfully started Jetty. In order to verify successful start you can navigate to http://localhost:8080/ and see the following response:

Jetty Server Response

Jetty Server Response

4. Running Web Applications In Jetty

It is very to install your web applications(WARs) in Jetty. All you have to do is dropping the WAR file under JETTY_HOME/webapps. You do not even need to restart Jetty. webapps directory is monitored periodically and new applications are deployed automatically.

5. Anatomy of the JETTY_HOME Directory

When you examine the content of the JETTY_HOME  you will see following directories:

  • bin
  • demo-base
  • etc
  • lib
  • logs
  • modules
  • resources
  • start.d
  • webapps

Some of these directories are worth mentioning:

  • etc: The XML configuration of Jetty modules defined in this directory.
  • lib: As in most Java applications, JAR files are stored in lib directory.
  • logs: When logging is enabled, log files are created under this directory.
  • modules: Jetty modules are defined under modules directory
  • resources: The external configuration resources (like logging configuration resources) are usually placed under this directory.
  • start.d: The modules activated through command line are configured through this directory.
  • webapps: The web applications (WAR files) running in our Jetty Server are dropped in this directory.

In JETTY_HOME directory, start.ini and start.jar files exist start.ini is the configuration file where most Jetty configuration is performed. start.jar is the initial executable file that initiates startup of the server.

6. Basic Configuration

6.1 Changing the Jetty Port

By default, Jetty runs on 8080. In order to change it to 7070 or some other port, you have to do the following:

  1. Open start.ini under JETTY_HOME.
  2. Add this line jetty.port=7070
  3. Save and close the file.

When you start the Jetty again it will run on port 7070.

6.2 Changing the webapps Directory

JETTY_HOME/webapps is the default directory to deploy your applications. If you need to change it for some reason, the steps to be followed are as follows:

  1. Open the start.ini.
  2. Remove the comment before the parameter jetty.deploy.monitoredDirName
  3. Change this parameter as you wish( E.g: jetty.deploy.monitoredDirName=../webapps2 )
  4. Save and close the file.

Now we can put our WARs in the webapps2 directory, which is at the same level as our JETTY_HOME

7. Modular Architecture of Jetty

Jetty runs on a modular architecture which means that many facilities and integrations are presented as modules. HTTP, HTTPS, SSL, logging, JMX, JNDI, WebSockets and many other features are implemented as separate modules. Some common modules such as HTTP, JSP and WebSocket are activated by default. The others (such as HTTPS, JMX etc.) have to be activated manually.

7.1 Anatomy of a Single Module

The modules are listed under JETTY_HOME/modules directory as mod files. mod files state the required JAR files to be activated (which are under JETTY_HOME/lib directory) and XML configuration files (which are under JETTY_HOME/etc directory) and other resources to be activated as the module is activated.

For example, when you view JETTY_HOME/modules/logging.mod content of, you will see something like the following:

[ xml]

etc/jetty-logging.xml
[files]
logs/
[lib]
lib/logging/**.jar
resources/

The configuration states that logging is configured through etc/jetty-logging.xml; and required JARs are under lib/logging. In addition to these, logs directory is required for this module.

7.2 Activating Modules through Command Line

There are two ways to activate Jetty modules. The first way is activating through command line:

java -jar start.jar --add-to-startd=logging

The command above creates the file logging.ini  under JETTY_HOME. Related configuration can be found in this file. After configuring logging, you can start Jetty again and observe that logging is active.

7.3 Activating Modules through start.ini

The second way to activate a module is adding the module to the start.ini:

--module=logging

This is equivalent to the command line presented in the first alternative with a subtle difference. This time, nothing is created under start.d ; so all further configuration should be done in this same start.ini file.

Personally, I would prefer the second alternative since all active modules are listed in a single file (start.ini) however there is no problem with the first approach either.

7.4 Configuring the Modules

As mentioned above, mod files tell us about the relevant XML configuration files, which are under JETTY_HOME/etc, for the module. Jetty modules are configured through these XML files.

For example logging module states jetty-logging.xml is relevant for logging configuration. One can alter this file to modify logging configuration.

8. Conclusion

In this post, we have defined the related steps to install and configure a standalone Jetty server. Further information can be obtained through the official documentation of Jetty.

(No Ratings Yet)
1 Comment Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
surya narayan subudhi Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
surya narayan subudhi
Guest
surya narayan subudhi

How to enable JMX for jetty