- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Introduction to Jetty
This is an introductory Jetty tutorial. The purpose of this tutorial is to get you started with Jetty. The tutorial has been created and tested on Linux. This tutorial covers Jetty version 9.
About Jetty
Jetty is an open source project providing an HTTP server, an HTTP client, and a Java servlet container. The project is a part of the Eclipse Foundation. Jetty is a mature project, which started in 1995. Jetty can be easily embedded in devices, tools, frameworks, application servers, and clusters.
Jetty also supports additional Java technologies including:
- SPDY
- WebSockets
- JNDI
- JAAS
- OSGi
- AJP
- JMX
Installing Jetty
In this tutorial we work on a local computer. Detailed instructions to install Jetty on a remote server are provided in a separate chapter. First, we download Jetty from the Eclipse's download page to a directory of our choice.
$ tar xzvf jetty-distribution-9.2.2.v20140723.tar.gz
We unpack the compressed file.
$ mv jetty-distribution-9.2.2.v20140723/ jetty-9.2.2/ $ cd jetty-9.2.2/
We rename the directory and move into it.
$ export JETTY_HOME=/home/janbodnar/bin/jetty-9.2.2
A Jetty home environment variable is created.
$ ls -F bin/ lib/ modules/ resources/ start.jar demo-base/ license-eplv10-aslv20.html notice.html start.d/ VERSION.txt etc/ logs/ README.TXT start.ini webapps/
We list the contents of the directory. The bin
directory contains utility scripts to help run Jetty on Unix systems. The lib
directory contains all JAR files necessary to run Jetty. The modules
directory has module definitions where a module is a configuration file that includes all the libraries, dependencies, XML and template INI files for a Jetty feature. The resources
directory contains additional resources for the classpath. The start.jar
is used to invoke Jetty.
The demo-base
directory contains demo applications distributed with Jetty. The start.d
directory contains INI files that contain arguments that are added to the command line. (For instance, the port number of the HTTP module on which Jetty listens.) The etc is a directory for Jetty XML configuration files. The logs
is a directory for logs. The start.ini
file contains arguments that are added during command line execution. Finally, the webapps
directory contains web applications that run under the default configuration of Jetty. It is where we deploy our web archives.
Running Jetty
To run the Jetty server, we use the start.jar
file.
$ pwd /home/janbodnar/bin/jetty $ java -jar start.jar
We are inside the Jetty home directory. We run the server passing the start.jar
file to the java
command. The start.jar
builds a classpath and executes a main java class with a classloader built from that classpath. It reads the contents of the start.ini
file and the INI files found in the start.d
directory. By default the start.jar
mechanism is configured to start the Jetty server, but it can be configured to start any Java main class.
We can now navigate a browser at this server at http://localhost:8080 .
$ curl localhost:8080 <HTML> <HEAD> <TITLE>Error 404 - Not Found</TITLE> <BODY> <H2>Error 404 - Not Found.</H2> No context on this server matched or handled this request. ...
We use a command line tool curl
to do the HTTP request. The server returns a 404
error—there is no application in the webapps directory yet. (Earlier versions included some demo applications in the webapps directory.) In order to start some demo applications, we relocate to the demo-base
directory and start the server with the following command:
$ java -jar ../start.jar
Navigating the browser to the localhost at port 8080
shows a Jetty welcome page with several examples. The demo-base
is an example of a Jetty base, which we cover later.
Shutting down Jetty
The Jetty server can be stopped by pressing Ctrl+C at the shell prompt or by killing its process.
It is possible to stop Jetty with the start.jar. Stopping this way must be enabled first.
$ java -jar start.jar STOP.PORT=8090 STOP.KEY=mypasswd
When we start Jetty, we provide a password and a port number on which Jetty listens to stop command.
$ java -jar start.jar STOP.PORT=8090 STOP.KEY=mypasswd --stop
We append the --stop
option to shut down the Jetty server.
Jetty home & base
Starting with Jetty 9.1, it is possible to maintain a separation between the binary installation of the standalone Jetty called Jetty home, and the customizations for a specific environment called Jetty base. Jetty home is the location for the Jetty distribution binaries, default XML configurations, and default module definitions. Jetty base is the location for configurations and customizations to the Jetty distribution.
$ mkdir my-base $ cd my-base/
We create a my-base directory which will be our Jetty base.
$ export JETTY_BASE=/home/janbodnar/prog/jetty/my-base
A JETTY_BASE
environment variable is created. Jetty determines the Jetty home and Jetty base locations either from environment variables or from properties.
Three important items of a Jetty base are the start.d
configuration directory, the start.ini
configuration file and the webapps
directory. We use the start.jar to enable necessary modules of Jetty. The --add-to-start
option enables a module by appending lines to the ${jetty.base}/start.ini
file. The --add-to-startd
enables a module via creation of a module-specific INI file in the ${jetty.base}/start.d/
directory.
$ java -jar $JETTY_HOME/start.jar --add-to-start=deploy
The start.ini
file is created and the deploy module is added to it. Also the webapps
directory is created.
$ java -jar $JETTY_HOME/start.jar --add-to-startd=http
The http module configuration named http.ini
is created in the start.d
directory.
$ tree . ├── start.d │ └── http.ini ├── start.ini └── webapps 2 directories, 2 files
At this moment we have this content in our Jetty base directory. Actually, we have enabled more than two modules—modules may have dependent modules and these were enabled as well. For instance, by enabling the http module we have activated the server module as well.
$ java -jar $JETTY_HOME/start.jar --list-modules ... Jetty Active Module Tree: ------------------------- + Module: server [enabled] + Module: http [enabled] + Module: security [enabled] + Module: servlet [enabled] + Module: webapp [enabled] + Module: deploy [enabled]
The --list-modules
option list all modules, both active and inactive. At the end of the output we have a tree of active modules. So we have now five active modules. The http module enabled the server module and the deploy module activated the webapp module which in turn activated the servlet and security modules.
$ cat start.d/http.ini # # Initialize module http # --module=http ## HTTP Connector Configuration # HTTP port to listen on jetty.port=8080 # HTTP idle timeout in milliseconds http.timeout=30000 # HTTP Socket.soLingerTime in seconds. (-1 to disable) # http.soLingerTime=-1
These are the contents of the http.ini
file located in the start.d
directory. There is a Jetty port configuration option. We change it to 8081
.
$ pwd /home/janbodnar/prog/jetty/my-base $ java -jar $JETTY_HOME/start.jar
It is recommended to sit in the Jetty base directory and to start the server by referencing the start.jar
remotely.

The server is running, but we have not deployed any web application and therefore the server returns the 404
error message.
First web application
We create and deploy our first web application with Jetty. The application will display a simple JSP page. We create a Jetty base from scratch to enable running JSP pages.
$ cd $JETTY_BASE $ java -jar $JETTY_HOME/start.jar --add-to-start=http,deploy,jsp,jstl,annotations
These modules are needed for web application having JSP pages.
$ java -jar $JETTY_HOME/start.jar
The server is started.
Now we are going to create a simple web application.
$ cd .. $ mkdir first $ cd first $ mkdir -p src/web/WEB-INF $ touch src/web/index.jsp $ touch build.xml
We create a welcome index.jsp page and the ANT build.xml file.
index.jsp
<!DOCTYPE html> <html> <body> <p> Today's date: <%= (new java.util.Date()).toLocaleString()%> </p> </body> </html>
The index.jsp
page prints the current date in the localized form.
build.xml
<?xml version="1.0"?> <project name="First" default="archive"> <property name="name" value="first"/> <property name="dist.dir" location="dist"/> <property name="web.dir" location="src/web"/> <property name="jetty.base" location="/home/janbodnar/prog/jetty/my-base"/> <property name="deploy.path" location="${jetty.base}/webapps"/> <target name="init"> <mkdir dir="${dist.dir}"/> </target> <target name="archive" depends="init"> <war destfile="${dist.dir}/${name}.war" needxmlfile="false"> <fileset dir="${web.dir}"/> </war> <echo>Archive created</echo> </target> <target name="deploy" depends="archive"> <copy file="${dist.dir}/${name}.war" overwrite="true" todir="${deploy.path}"/> <echo>Archive deployed</echo> </target> <target name="clean" depends="init"> <delete dir="${dist.dir}"/> <echo>Cleaning completed</echo> </target> </project>
This Ant build file has targets to create a build directory and distribution directory and to clean (delete) them. It creates a web archive (a WAR file) containing a single index.jsp
file. The deploy target deploys the WAR file to the webapps directory of the Jetty base.
$ ant deploy Buildfile: /home/janbodnar/prog/jetty/first/build.xml init: archive: [echo] Archive created deploy: [copy] Copying 1 file to /home/janbodnar/prog/jetty/my-base/webapps [echo] Archive deployed BUILD SUCCESSFUL Total time: 0 seconds
We run Ant with the deploy task. A web archive is created and deployed.
$ curl localhost:8080/first/ <!DOCTYPE html> <html> <body> <p> Today's date: Sep 16, 2014 10:43:45 AM </p> </body> </html>
The web application's context is detemined from the name of the archive file: first.war
. The application successfully returns the current date.
We have enabled a JSP module for our Jetty base but we have not explicitly configured the usage of JSP pages. (For instance in the web.xml
file.) A default configuration is included by enabling the deploy module.
$ java -jar $JETTY_HOME/start.jar --list-config ... Jetty Active XMLs: ------------------ ${jetty.home}/etc/jetty.xml ${jetty.home}/etc/jetty-http.xml ${jetty.home}/etc/jetty-deploy.xml ${jetty.home}/etc/jetty-plus.xml ${jetty.home}/etc/jetty-annotations.xml
The --list-config
option lists the configuration that will be used to start Jetty. At the end of the output, we have XML files that are loaded by the server. For each module an XML file is loaded. The jetty-deploy.xml
is one of them.
<Set name="defaultsDescriptor"><Property name="jetty.home" default="." />/etc/webdefault.xml</Set>
The jetty-deploy.xml
has this line that loads the webdefault.xml
configuration file, which contains the configuration of JSP pages.
<servlet id="jsp"> <servlet-name>jsp</servlet-name> <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class> ... </servlet>
This is an excerpt from the webdefault.xml
file configuring the support of JSP pages.
Sources
Jetty's official reference guide was used to create this tutorial.
This chapter was an introduction to Jetty.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论