# Technical Documentation: jetty/jetty.project

> ℹ️ **Provenance:** Multi-Tier Ingestion · [CodeWiki Reference](https://codewiki.google/github.com/jetty/jetty.project)

### Overview (README.md)

:warning: The Eclipse Jetty Project does not use the `master` branch
====================================================================

Please use a version specific branch.

Example:

 * [`jetty-9.4.x`](https://github.com/eclipse/jetty.project/tree/jetty-9.4.x)
 * [`jetty-10.0.x`](https://github.com/eclipse/jetty.project/tree/jetty-10.0.x)

Eclipse Jetty Canonical Repository
----------------------------------

This is the canonical repository for the Jetty project, feel free to fork and contribute now!

Submitting a patch or pull request?

Make sure you have an Eclipse Contributor Agreement (ECA) on file.

- [eclipse.org/legal/ecafaq](https://www.eclipse.org/legal/ecafaq.php)

Project description
-------------------

Jetty is a lightweight highly scalable java based web server and servlet engine.
Our goal is to support web protocols like HTTP, HTTP/2 and WebSocket in a high volume low latency way that provides maximum performance while retaining the ease of use and compatibility with years of servlet development.
Jetty is a modern fully async web server that has a long history as a component oriented technology easily embedded into applications while still offering a solid traditional distribution for webapp deployment.

- [https://projects.eclipse.org/projects/rt.jetty](https://projects.eclipse.org/projects/rt.jetty)

Documentation
-------------

Project documentation is available on the Jetty Eclipse website.

- [https://www.eclipse.org/jetty/documentation](https://www.eclipse.org/jetty/documentation)

---

### Documentation Jetty README (`documentation/jetty/README.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Jetty Documentation

This project is the root of the Jetty documentation.
The content files in this project get sourced by the Antora playbook in the playbook repository that builds the website.

In order to build the documentation locally, you first need to prepare a jetty-home directory by running the following command from the top-level folder of the Jetty project:

 $ mvn install -Dcollector -Pfast -am -pl documentation/jetty

Then you can use the following command from this directory to prepare and run Antora using a preview profile:

 $ mvn antora -N

If you don't run the first command, the Antora build will still succeed, but you will get warnings about missing includes for files taken from jetty-home.

The `antora:antora` goal, which the `antora` lifecycle invokes, takes advantage of the playbook provider feature so the playbook for this branch can be centrally managed in the playbook repository.

Note that this preview profile does not run the jetty blocks, so you will only see the configuration for those runs in the preview site.
If you want to build the full site, use the build in the playbook repository.

---

### Documentation Jetty Modules Operations Guide Nav (`documentation/jetty/modules/operations-guide/nav.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

.xref:operations-guide:index.adoc[]
* xref:begin/index.adoc[]
* xref:features/index.adoc[]
* xref:howtos/index.adoc[]
* xref:arch/index.adoc[]
* xref:start/index.adoc[]
** xref:start/start-jpms.adoc[]
* xref:modules/index.adoc[]
** xref:modules/custom.adoc[]
** xref:modules/standard.adoc[]
* xref:deploy/index.adoc[]
* xref:server/index.adoc[]
* xref:protocols/index.adoc[]
* xref:keystore/index.adoc[]
* xref:session/index.adoc[]
* xref:quickstart/index.adoc[]
* xref:annotations/index.adoc[]
* xref:jsp/index.adoc[]
* xref:jstl/index.adoc[]
* xref:jsf-taglibs/index.adoc[]
* xref:jndi/index.adoc[]
* Jetty Security
** xref:security/configuring-form-size.adoc[]
** xref:security/jaas-support.adoc[]
** xref:security/jaspi-support.adoc[]
** xref:security/openid-support.adoc[]
* xref:jmx/index.adoc[]
* xref:tools/index.adoc[]
* xref:troubleshooting/index.adoc[]
* xref:xml/index.adoc[]
* xref:glossary.adoc[]

---

### Documentation Jetty Modules Programming Guide Nav (`documentation/jetty/modules/programming-guide/nav.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

.xref:programming-guide:index.adoc[]
* xref:client/index.adoc[]
** xref:client/http.adoc[]
** xref:client/http2.adoc[]
** xref:client/http3.adoc[]
** xref:client/websocket.adoc[]
** xref:client/io-arch.adoc[]
** xref:client/threads.adoc[]
* xref:server/index.adoc[]
** xref:server/http.adoc[]
** xref:server/http2.adoc[]
** xref:server/http3.adoc[]
** xref:server/compliance.adoc[]
** xref:server/session.adoc[]
** xref:server/deploy.adoc[]
** xref:server/websocket.adoc[]
** xref:server/fastcgi.adoc[]
** xref:server/io-arch.adoc[]
** xref:server/threads.adoc[]
* Maven and Jetty
** xref:maven-jetty/jetty-maven-helloworld.adoc[]
** xref:maven-jetty/jetty-maven-plugin.adoc[]
** xref:maven-jetty/jetty-jspc-maven-plugin.adoc[]
* Jetty Architecture
** xref:arch/bean.adoc[]
** xref:arch/threads.adoc[]
** xref:arch/io.adoc[]
** xref:arch/listener.adoc[]
** xref:arch/jmx.adoc[]
** xref:arch/json.adoc[]
* xref:troubleshooting/index.adoc[]
** xref:troubleshooting/logging.adoc[]
** xref:troubleshooting/thread-dump.adoc[]
** xref:troubleshooting/state-tracking.adoc[]
** xref:troubleshooting/component-dump.adoc[]
** xref:troubleshooting/debugging.adoc[]
* xref:security/index.adoc[]
** xref:security/siwe-support.adoc[]
** xref:security/configuring-form-size.adoc[]
** xref:security/openid-support.adoc[]
* Migration Guides
** xref:migration/94-to-10.adoc[]
** xref:migration/11-to-12.adoc[]
** xref:migration/12.0-to-12.1.adoc[]

---

### Documentation Jetty Modules ROOT Pages Index (`documentation/jetty/modules/ROOT/pages/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Eclipse Jetty {page-version}

This is the main documentation page for the Eclipse Jetty Project.

Jetty provides a highly scalable and memory-efficient web server and Servlet container, supporting web protocols such as HTTP/1.1, HTTP/2, HTTP/3 and WebSocket.
Furthermore, Jetty offers integrations with many other technologies, such as OSGi, JMX, JNDI, JAAS, CDI, etc. and with the relevant Jakarta EE technologies.

Jetty is open source and are freely available for commercial use and distribution under either the link:https://www.eclipse.org/legal/epl-2.0/[Eclipse Public License v2] or the link:https://www.apache.org/licenses/LICENSE-2.0[Apache License v2].

Jetty can either be used as a standalone server to deploy web applications, or as a library that can be used in your code as a dependency.

.Jetty Versions and Compatibilities
[cols="1a,1a,1a,1a", options="header"]
|===
| Jetty Version | Required Java Version | Jakarta EE Version | Status
| Jetty 12.1.x | Java 17 | Jakarta EE11, EE10, EE9, EE8 | Stable

| Jetty 12.0.x | Java 17 | Jakarta EE10, EE9, EE8 | Stable

| Jetty 11.0.x | Java 11 | Jakarta EE9 | EOL (see link:https://github.com/jetty/jetty.project/issues/13918[#13918])

| Jetty 10.0.x | Java 11 | Jakarta EE8 | EOL (see link:https://github.com/jetty/jetty.project/issues/13918[#13918])

| Jetty 9.4.x | Java 8 | Jakarta EE7 | EOL (see link:https://github.com/jetty/jetty.project/issues/13918[#13918])
|===

== xref:operations-guide:index.adoc[]

The Operations Guide targets sysops, devops, and developers who want to install Jetty as a standalone server to deploy web applications.

== xref:programming-guide:index.adoc[]

The Programming Guide targets developers who want to use the Jetty libraries in their applications, and advanced sysops/devops that want to customize the deployment of web applications.

---

### Documentation Jetty Modules Operations Guide Pages Glossary (`documentation/jetty/modules/operations-guide/pages/glossary.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

[glossary]
= Glossary

This glossary defines common terms used throughout the Jetty documentation.

[glossary]
[[bean]]
Bean::
A component instance added to a `ContainerLifeCycle` container.
Beans can be _managed_ (lifecycle tied to container), _unmanaged_ (independent lifecycle), or _POJO_ (plain objects without lifecycle).
See xref:programming-guide:arch/bean.adoc[] for details.

[[connector]]
Connector::
A component that listens on a network port for incoming connections.
Connectors are associated with one or more `ConnectionFactory` instances that create protocol-specific connections (HTTP/1.1, HTTP/2, HTTP/3).
See xref:server/index.adoc[] for server configuration.

[[container-lifecycle]]
ContainerLifeCycle::
A `LifeCycle` implementation that can contain other components (beans).
It manages the lifecycle of its managed beans, starting and stopping them together with the container.
See xref:programming-guide:arch/bean.adoc[] for the component architecture.

[[context-handler]]
ContextHandler::
A `Handler` that scopes HTTP requests to a specific context path.
It provides the foundation for web application deployment, managing attributes, MIME types, and class loaders.
See xref:deploy/index.adoc[] for deployment information.

[[context-xml]]
Context XML file::
An XML configuration file used to deploy web applications in Jetty.
Context XML files use Jetty's XML configuration syntax to instantiate and configure `ContextHandler` instances.
See xref:xml/index.adoc[] for XML configuration syntax and xref:deploy/index.adoc[] for deployment examples.

[[handler]]
Handler::
The fundamental abstraction for processing HTTP requests in Jetty.
Handlers form a tree structure where requests flow from the server through various handlers.
A handler returns `true` if it handled the request, `false` otherwise.
See xref:programming-guide:server/index.adoc[] for the server programming guide.

[[jetty-base]]
`$JETTY_BASE`::
The directory containing your Jetty server customizations and configurations.
You can have multiple `$JETTY_BASE` directories, each with different configurations, all referencing the same `$JETTY_HOME`.
See xref:arch/index.adoc#jetty-base[Architecture Overview] for details.

[[jetty-home]]
`$JETTY_HOME`::
The directory containing the Jetty distribution binaries and default configuration.
This directory should not be modified; all customizations should go in `$JETTY_BASE`.
See xref:arch/index.adoc#jetty-base[Architecture Overview] for details.

[[lifecycle]]
LifeCycle::
An interface implemented by Jetty components that can be started and stopped.
Components implementing `LifeCycle` participate in the container's lifecycle management.
See xref:programming-guide:arch/bean.adoc#lifecycle[Component Lifecycle] for details.

[[module]]
Module::
A reusable unit that provides one or more Jetty features.
Modules are enabled in `$JETTY_BASE` and declare dependencies on other modules.
Examples include `http`, `https`, `http2`, `deploy`, and `logging`.
See xref:modules/index.adoc[] for the module system documentation.

[[server]]
Server::
The root component of a Jetty server application.
It contains connectors, handlers, and thread pools, forming the top of the component tree.
See xref:server/index.adoc[] for server configuration.

[[server-connector]]
ServerConnector::
The standard TCP/IP connector implementation for accepting network connections.
It supports multiple protocols through `ConnectionFactory` instances.
See xref:server/index.adoc[] for connector configuration.

[[thread-pool]]
ThreadPool::
A component that manages worker threads for processing requests.
The default implementation is `QueuedThreadPool`.
See xref:programming-guide:arch/threads.adoc[] for thread architecture details.

[[web-application]]
Web Application::
A collection of code, resources, and configuration deployed to a Jetty server.
Web applications can be Jakarta EE applications (WAR files), Jetty Core applications (using Handler APIs), or static file applications.
Each web application is deployed to a specific context path.
See xref:deploy/index.adoc[] for deployment options.

[[webapp-context]]
WebAppContext::
A `ContextHandler` subclass specifically for Jakarta EE web applications.
It handles WAR file extraction, `web.xml` parsing, and servlet lifecycle management.
See xref:deploy/index.adoc[] for deployment information.

---

### Documentation Jetty Modules Operations Guide Pages Index (`documentation/jetty/modules/operations-guide/pages/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

[reftext=Operations Guide]
= Jetty {page-version} Operations Guide

The Eclipse Jetty Operations Guide targets sysops, devops, and developers who want to install Eclipse Jetty as a standalone server to deploy web applications.

---

### Documentation Jetty Modules Programming Guide Pages Index (`documentation/jetty/modules/programming-guide/pages/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

[reftext=Programming Guide]
= Jetty {page-version} Programming Guide

The Eclipse Jetty Programming Guide targets developers who want to use the Jetty libraries in their applications.

The Jetty libraries provide the client-side and server-side APIs to work with various web protocols such as HTTP/1.1, HTTP/2, HTTP/3, WebSocket and FastCGI.

You may use the xref:client/index.adoc[Jetty client-side library] in your application to make calls to third party REST services, or to other REST microservices in your system.

Likewise, you may use the xref:server/index.adoc[Jetty server-side library] to quickly create an HTTP or REST service without having to create a full Jakarta EE web application archive file (a `+*.war+` file) and without having to deploy it to a Jetty standalone server that you would have to download and install.

This guide will walk you through the design of the Jetty libraries and how to use its classes to write your applications.

== Code Deprecation Policy

As the Jetty code evolves, classes and/or methods are deprecated using the `@Deprecated` annotation and will be removed in a future Jetty release.

The Jetty release numbering follows this scheme: `<major>.<minor>.<micro>`. For example, 12.0.5 has `major=12`, `minor=0` and `micro=5`.

The policy to remove deprecated code is the following:

* Code deprecated in a Jetty release `x.y.z` is **not** removed in subsequent micro releases `x.y.(z+n)`.
* Code deprecated in a Jetty release `x.y.z` may be removed in the next minor release `x.(y+1).0`, but only if it has been deprecated for more than six micro releases.
* Code deprecated in a Jetty release `x.y.z` may be removed in the next major release `(x+1).0.0` without restrictions.

For example, let's assume that some code `codeA` is deprecated in Jetty 12.0.5, and some other code `codeB` is deprecated in Jetty 12.0.8.
Let's also assume that 12.1.0 (a new minor release) is released after the release of Jetty 12.0.11.

Then, by the rules above:

* Neither `codeA` nor `codeB` are removed from the Jetty 12.0.x releases.
* Code `codeA` is removed from Jetty 12.1.0 because it has been deprecated for more than six micro releases, since Jetty 12.1.0 is released _after_ Jetty 12.0.11.
* Code `codeB` is not removed from Jetty 12.1.0 because it has been deprecated for less than six micro releases.
As `codeB` is now part of Jetty 12.1.0, it will **not** be removed from the Jetty 12.1.x releases.

[NOTE]
====
There could be rare cases where code (possibly not even deprecated) must be removed earlier than specified above to address security vulnerabilities.
====

---

### Documentation Jetty Modules Operations Guide Pages Annotations Index (`documentation/jetty/modules/operations-guide/pages/annotations/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Annotations

Enable the `{ee-all}-annotations` module if your Jakarta EE web application -- or any of its third party libraries -- uses any of the following:

* Annotations:
** `@Resource`
** `@Resources`
** `@PostConstruct`
** `@PreDestroy`
** `@DeclaredRoles`
** `@RunAs`
** `@MultipartConfig`
** `@WebServlet`
** `@WebFilter`
** `@WebListener`
** `@WebInitParam`
** `@ServletSecurity`, `@HttpConstraint`, `@HttpMethodConstraint`
** `@HandlesTypes`
* `javax.servlet.ServletContainerInitializer` or `jakarta.servlet.ServletContainerInitializer`
* JSP


[[scanning]]
== Annotation Scanning

According to more recent versions of the Jakarta Servlet Specification, the `web.xml` file can contain the attribute `metadata-complete`.
If this is set to `true`, then _no_ annotation scanning takes place, and your descriptor must contain the equivalent xml statements of any annotations.

If it is `metadata-complete=false`, or your `web.xml` predates the inclusion of this attribute, annotation scanning is required to take place.

To prevent annotation scanning you can use the `WebAppContext.setConfigurationDiscovered(false)` method.
Here's an example context XML file that calls this method:

[,xml,subs=attributes+]
----
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext"> 
  <Set name="configurationDiscovered">false</Set> 
</Configure>
----
<1> Configures a link:{javadoc-url}/org/eclipse/jetty/{ee-current}/webapp/WebAppContext.html[`WebAppContext`], which is the Jetty component that represents a standard Servlet web application.
<2> Specifies that scanning should not take place.

However, despite `metadata-complete=true`, scanning of classes may _still_ occur because of `ServletContainerInitializer`.
Classes implementing this interface are found by Jetty using the http://docs.oracle.com/javase/6/docs/api/java/util/ServiceLoader.html[javax.util.ServiceLoader] mechanism, and if one is present _and_ it includes the `@HandlesTypes` annotation, then Jetty must scan the class hierarchy of the web application.
This may be very time-consuming if you have many jars.

Jetty can reduce the time taken by limiting the jars that are scanned.

[[og-container-include-jar-pattern]]
=== The container classpath

By default, Jetty will _not_ scan any classes that are on the container's classpath.

Sometimes, you may have third party libraries on the container's classpath that you need to be scanned.
In this case, use the `org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern` context attribute to define which container jars and class directories to scan.
The value of this attribute is a regular expression.

Here's an example from a context XML file that includes any jar whose name starts with `foo-` or `bar-`, or a directory named `classes`:

[,xml,subs=attributes+]
----
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext"> 
    <Call name="setAttribute"> 
      <Arg>org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern</Arg> 
      <Arg>.*/foo-[^/]*\.jar$|.*/bar-[^/]*\.jar$|.*/classes/.*</Arg> 
    </Call>
</Configure>
----
<1> Configures a link:{javadoc-url}/org/eclipse/jetty/{ee-current}/webapp/WebAppContext.html[`WebAppContext`], which is the Jetty component that represents a standard Servlet web application.
<2> Specifies a context attribute.
<3> Specifies the name of the context attribute.
<4> Specifies the value of the context attribute.

Note that the order of the patterns defines the ordering of the scanning of the jars or class directories.

[[og-web-inf-include-jar-pattern]]
=== The Jakarta EE Web Application Class-Path

By default, Jetty will scan __all__ classes from `WEB-INF/classes` and _all_ jars from `WEB-INF/lib` according to the order, if any, established by absolute or relative ordering clauses in `web.xml`.

If your web application contains many jar files that you know do not contain any annotations, you can significantly speed up deployment by omitting them from scanning.
However, be careful if your web application uses a `ServletContainerInitializer` with a `@HandlesTypes` annotation that you don't exclude jars that contain classes matching the annotation.

Use the `org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern` context attribute to define a regular expression for jars and class directories to select for scanning.

Here's an example of a Jetty context XML file that sets a pattern that matches any jar on the web application's class-path that starts with `"spring-"`:

[,xml,subs=attributes+]
----
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext"> 
    <Call name="setAttribute">  
      <Arg>org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern</Arg>  
      <Arg>.*/spring-[^/]*\.jar$</Arg>  
    </Call>
</Configure>
----
<1> Configures a link:{javadoc-url}/org/eclipse/jetty/{ee-current}/webapp/WebAppContext.html[`WebAppContext`], which is the Jetty component that represents a standard Servlet web application.
<2> Specifies a context attribute.
<3> Specifies the name of the context attribute.
<4> Specifies the value of the context attribute.

=== Multi-threading

By default, Jetty performs annotation scanning in a multi-threaded manner in order to complete it in the minimum amount of time.

If you don't want multi-threaded scanning, you can configure Jetty to revert to single-threaded scanning.
There are several options to configure this:

1.  Set the context attribute `org.eclipse.jetty.annotations.multiThreaded` to `false`
2.  Set the `Server` attribute `org.eclipse.jetty.annotations.multiThreaded` to `false`
3.  Set the `System` property `org.eclipse.jetty.annotations.multiThreaded` to `false`

Method 1 will only affect the current webapp.
Method 2 will affect all web applications deployed to the same `Server` instance.
Method 3 will affect all web applications deployed in the same JVM.

By default, Jetty will wait a maximum of 60 seconds for all the scanning threads to complete.
You can set this to a higher or lower number of seconds by doing one of the following:

1.  Set the context attribute `org.eclipse.jetty.annotations.maxWait`
2.  Set the `Server` attribute `org.eclipse.jetty.annotations.maxWait`
3.  Set the `System` property `org.eclipse.jetty.annotations.maxWait`

Method 1 will only affect the current webapp.
Method 2 will affect all web applications deployed to the same `Server` instance.
Method 3 will affect all web applications deployed in the same JVM.

[[scis]]
== ServletContainerInitializers

The `ServletContainerInitializer` class can exist in: the container's classpath, the web application's `WEB-INF/classes` directory, the webapp's `WEB-INF/lib` jars, or any external extraClasspath that you have configured on the webapp.

The Jakarta Servlet Specification does not define any order in which a `ServletContainerInitializer` must be called when the web application starts.
By default, Jetty will call them in the following order:

1.  ServletContainerInitializers from the container's classpath
2.  ServletContainerInitializers from `WEB-INF/classes`
3.  ServletContainerInitializers from `WEB-INF/lib` jars __in the order established in web.xml__, or in the order that the SCI is returned by the http://docs.oracle.com/javase/6/docs/api/java/util/ServiceLoader.html[javax.util.ServiceLoader] if there is _no_ ordering.

=== Exclusions

By default, as according to the Jakarta Servlet Specification, all `ServletContainerInitializer` instances that are discovered are invoked.

Sometimes, depending on your requirements, you may need to prevent some being called at all.

In this case, you can define the `org.eclipse.jetty.containerInitializerExclusionPattern` context attribute.

This is a regular expression that defines http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html[patterns] of classnames that you want to exclude.
Here's an example of setting the context attribute in a context XML file:

[,xml,subs=attributes+]
----
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext"> 
    <Call name="setAttribute">  
      <Arg>org.eclipse.jetty.containerInitializerExclusionPattern</Arg>  
      <Arg>com.acme.*|com.corp.SlowContainerInitializer</Arg>  
    </Call>
</Configure>
----
<1> Configures a link:{javadoc-url}/org/eclipse/jetty/{ee-current}/webapp/WebAppContext.html[`WebAppContext`], which is the Jetty component that represents a standard Servlet web application.
<2> Specifies a context attribute.
<3> Specifies the name of the context attribute.
<4> Specifies the value of the context attribute.

In this example we exclude *all* `ServletContainerInitializer` instances in the `com.acme package`, and the specific class `com.corp.SlowContainerInitializer`.

It is possible to use exclusion and ordering together to control `ServletContainerInitializer` invocation - the exclusions will be applied before the ordering.

=== Ordering

If you need `ServletContainerInitializer` classes called in a specific order, you can use the context attribute `org.eclipse.jetty.containerInitializerOrder`.
Set it to a list of comma separated `ServletContainerInitializer` class names in the order that you want them applied.

You may optionally use the wildcard character `+*+` *once* in the list.
It will match all `ServletContainerInitializer` classes not explicitly named in the list.

Here is an example context XML file that ensures the `com.example.PrioritySCI` will be called first, followed by the `com.acme.FooSCI`, then all other SCIs:

[,xml,subs=attributes+]
----
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext"> 
    <Call name="setAttribute">  
      <Arg>org.eclipse.jetty.containerInitializerOrder</Arg>  
      <Arg>org.eclipse.jetty.websocket.javax.server.JavaxWebSocketServletContainerInitializer, com.acme.FooSCI, *</Arg>  
    </Call>
</Configure>
----
<1> Configures a link:{javadoc-url}/org/eclipse/jetty/{ee-current}/webapp/WebAppContext.html[`WebAppContext`], which is the Jetty component that represents a standard Servlet web application.
<2> Specifies a context attribute.
<3> Specifies the name of the context attribute.
<4> Specifies the value of the context attribute.

---

### Documentation Jetty Modules Operations Guide Pages Arch Index (`documentation/jetty/modules/operations-guide/pages/arch/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Architecture Overview

Jetty is an HTTP server and Servlet Container, and supports deployments of web applications.

The xref:server/index.adoc[Jetty server] listens on one or more network ports using one or more xref:protocols/index.adoc[protocol connectors].

Clients send HTTP requests for specific URIs, such as `+https://host/store/cart+`.

The HTTP requests arrive to the connectors through the network; the Jetty server processes the requests and, based on their URIs, forwards them to the appropriate xref:deploy/index.adoc[deployed web application].

[plantuml]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false
skinparam roundCorner 10

scale 1.25

cloud Internet as internet
rectangle "Jetty Server" as server
rectangle "HTTP/1.1 Connector" as http
rectangle "HTTP/2 Connector" as http2
rectangle "WebApp "Store"" as store
rectangle "WebApp "Catalog"" as catalog

internet -- http
internet -- http2
http -- server
http2 -- server
server -- store
server -- catalog
----

[[concepts]]
== Main Concepts
There are three main concepts on which the Jetty standalone server is based:

* The <<modules,Jetty _module_ system>>, where Jetty modules provides Jetty features.
* The <<jetty-base,`$JETTY_BASE` directory>>, that provides a place where you configure which Jetty modules you want to enable, configure the properties of each enabled module, and therefore configure the features you need for your web applications.
* The <<start,Jetty start mechanism>>, that starts a JVM that runs Jetty with the configuration you specified.

After installing Jetty, you will want to set up a <<jetty-base,`$JETTY_BASE` directory>> where you configure <<modules,Jetty modules>>.

[[modules]]
== Jetty Modules

The Jetty standalone server is made of Java components that are assembled together, configured and started to provide different features.

A Jetty _module_ provides one or more components that work together to provide typically one feature, although they may provide more than one feature.

A Jetty module is nothing more than Jetty components assembled together like you would do using Java APIs, just done in a declarative way using configuration files.
What you can do in Java code to assemble Jetty components can be done using Jetty modules.

A Jetty module may be dependent on other Jetty modules: for example, the `http` Jetty module depends on the `server` Jetty module which in turn depends on the `threadpool` and `logging` Jetty modules.

Every feature in a Jetty server is enabled by enabling the corresponding Jetty module(s).

For example, if you enable only the `http` Jetty module, then your Jetty standalone server will only be able to listen to a network port for clear-text HTTP requests.
It will not be able to process secure HTTP (i.e. `https`) requests, it will not be able to process WebSocket, or HTTP/2, or HTTP/3 or any other protocol because the correspondent modules have not been enabled.

You can even start a Jetty server _without_ listening on a network port -- for example because you have enabled a custom module you wrote that provides the features you need.

This allows the Jetty standalone server to be as small as necessary: modules that are not enabled are not loaded, don't waste memory, and you don't risk a client using a module that you did not know was even there.

For more detailed information about the Jetty module system, see xref:modules/index.adoc[this section].

[[jetty-base]]
== `$JETTY_HOME` and `$JETTY_BASE`

Instead of managing multiple Jetty distributions out of many locations, it is possible to maintain a separation between the binary installation of the standalone Jetty, known as `$JETTY_HOME`, and the customizations for your specific environment(s), known as `$JETTY_BASE`.

This separation between the binary installation directory and the specific configuration directory allows managing multiple, different, server configurations, and allows for quick, drop-in upgrades of Jetty.

There should always only be *one* `$JETTY_HOME` (per version of Jetty), but there can be many `$JETTY_BASE` directories that reference it.

This separation between `$JETTY_HOME` and `$JETTY_BASE` allows Jetty upgrades without affecting your web applications.
`$JETTY_HOME` contains the Jetty runtime and libraries and the default configuration, while a `$JETTY_BASE` contains your web applications and any override of the default configuration.

For example, with the `$JETTY_HOME` installation the default value for the network port for clear-text HTTP is `8080`.
However, you may want that port to be `6060`, because xref:protocols/index.adoc#proxy[Jetty is behind a load balancer] that is configured to forward to the backend on port `6060`.
In this case, you configure the clear-text HTTP port in `$JETTY_BASE`, not in `$JETTY_HOME`.
When you upgrade Jetty, you will upgrade only the files in `$JETTY_HOME`, and all the configuration in `$JETTY_BASE` will remain unchanged, keeping your clear-text HTTP port at `6060`.

Installing the Jetty runtime and libraries in `$JETTY_HOME` also allows you to leverage file system permissions: `$JETTY_HOME` may be owned by an administrator user (so that only administrators can upgrade it), while `$JETTY_BASE` directories may be owned by a less privileged user.

If you had changed the default configuration in `$JETTY_HOME`, when you upgrade Jetty, say from version `10.0.0` to version `10.0.1`, your changes would be lost.
Maintaining all the changes in `$JETTY_HOME`, and having to reconfigure these with each upgrade results in a massive commitment of time and effort.

To recap:

`$JETTY_HOME`::
This is the location for the Jetty binaries.
`$JETTY_BASE`::
This is the location for your configurations and customizations to the Jetty binaries.

[[start]]
== Start Mechanism

The Jetty start mechanism provides two features:

* The mean to configure your `$JETTY_BASE` by enabling the desired modules, and to display the configuration of your `$JETTY_BASE`.
* The mean to start Jetty itself, by starting a JVM that reads the Jetty configuration in `$JETTY_BASE`, which is then executed to assemble and start the Jetty components.

The Jetty start mechanism is invoked by executing `$JETTY_HOME/start.jar` from within your `$JETTY_BASE`, and you can think of it as the Jetty command line program, similar to many Unix/Windows command line programs.

For example, you can ask for help:

----
$ java -jar $JETTY_HOME/start.jar --help
----

Or you can list all available modules (or only those with a specific tag):

----
# List all the modules.
$ java -jar $JETTY_HOME/start.jar --list-modules=*

# List all the modules tagged as "demo".
$ java -jar $JETTY_HOME/start.jar --list-modules=demo
----

You can enable a module, for example the `http` module:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=http
----

Once you have one or more module enabled, you can display the current configuration, to verify that the configuration is correct:

----
$ java -jar $JETTY_HOME/start.jar --list-config
----

You can enable a Jetty demo module, which will deploy a demo web application:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=demo-simple
----

Finally, you can start Jetty:

----
$ java -jar $JETTY_HOME/start.jar
----

Read more information at the xref:start/index.adoc[Jetty start mechanism section].

---

### Documentation Jetty Modules Operations Guide Pages Begin Index (`documentation/jetty/modules/operations-guide/pages/begin/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Getting Started

If you are new to Eclipse Jetty, read on to download, install, start and deploy web applications to Jetty.

== Quick Setup

Jetty is distributed in an artifact that expands in a directory called `$JETTY_HOME`, which should not be modified.

Configuration for Jetty is typically done in a directory called `$JETTY_BASE`.
There may be more than one `$JETTY_BASE` directories with different configurations.

Jetty supports the deployment of Jakarta {ee-all-caps} web applications, as well as the deployment of Jetty specific web applications, as discussed in xref:deploy/index.adoc[this section].

For example, the following commands can be used to set up a `$JETTY_BASE` directory that supports deployment of Jakarta {ee-current-caps} `+*.war+` files and a clear-text HTTP connector:

----
$ export JETTY_HOME=/path/to/jetty-home
$ mkdir /path/to/jetty-base
$ cd /path/to/jetty-base
$ java -jar $JETTY_HOME/start.jar --add-modules=http,ee11-deploy
----

The last command creates a `$JETTY_BASE/start.d/` directory and other directories that contain the configuration of the server, including the `$JETTY_BASE/webapps/` directory, in which Jakarta {ee-current-caps} `+*.war+` files can be deployed.

To deploy Jetty's demo web applications, run this command:

----
$ java -jar $JETTY_HOME/start.jar --add-module=demos
----

Now you can start the Jetty server with:

----
$ java -jar $JETTY_HOME/start.jar
----

Point your browser at `+http://localhost:8080+` to see the demo web applications deployed in Jetty.

The Jetty server can be stopped with kbd:[Ctrl+C] in the terminal window.

The following sections will guide you in details about <<download,downloading>>, <<install,installing>> and <<start,starting>> Jetty, and <<deploy,deploying>> your web applications to Jetty.

Read the xref:arch/index.adoc[Jetty architecture section] for more information about Jetty modules, `$JETTY_HOME`, `$JETTY_BASE` and how to customize and start Jetty.

[[download]]
== Downloading Jetty

The Jetty distribution is a file of the form `jetty-home-<version>.<ext>`, available for download from https://jetty.org/download.html[]

The Jetty distribution is available in both `zip` and `gzip` formats; download the one most appropriate for your system, typically `zip` for Windows and `gzip` for other operating systems.

[[install]]
== Installing Jetty

After the download, unpacking Jetty will extract the files into a directory called `jetty-home-<version>`, where `<version>` is the version of Jetty that you downloaded.
For example, installing Jetty {jetty-version} will create a directory called `jetty-home-{jetty-version}`.

IMPORTANT: It is important that *only* stable release versions are used in production environments.
Versions that have been deprecated or are released as Milestones (M), Alpha, Beta or Release Candidates (RC) are *not* suitable for production as they may contain security flaws or incomplete/non-functioning feature sets.

Unpack Jetty file into a convenient location, such as `/opt`.
The rest of the instructions in this documentation will refer to this location as `$JETTY_HOME`, or `${jetty.home}`.

CAUTION: For Windows users, you should unpack Jetty to a path that does not contain spaces.

If you are new to Jetty, you should read the xref:arch/index.adoc[Jetty architecture section] to become familiar with the terms used in this documentation.
Otherwise, you can jump to the <<start,section on starting Jetty>>.

[[start]]
== Starting Jetty

Jetty as a standalone server has no graphical user interface; configuring and running the server is done from the command line.

First, create a `$JETTY_BASE` directory.

----
$ JETTY_BASE=/path/to/jetty.base
$ mkdir $JETTY_BASE
$ cd $JETTY_BASE
----

You will typically start Jetty by executing `$JETTY_HOME/start.jar` from this directory.
However, if you try to start Jetty from an empty `$JETTY_BASE`, it will complain that you haven't enabled any modules:

----
$ java -jar $JETTY_HOME/start.jar
----

[jetty%nowrap]
....
[jetty]
....

Jetty uses a xref:modules/index.adoc[module system] to configure and assemble the server; these modules are enabled and configured in xref:arch/index.adoc#jetty-base[`$JETTY_BASE`].
Since the `$JETTY_BASE` directory you just created is empty, Jetty has no configuration it can use to assemble the server.

NOTE: See the xref:arch/index.adoc[architecture section] of this document for more information on the design of Jetty's module system.

You can explore what modules are available with the `--list-modules` flag:

----
$ java -jar $JETTY_HOME/start.jar --list-modules=*
----

Now try to enable the xref:protocols/index.adoc#http[`http`] module.

NOTE: If you want to enable support for protocols like secure HTTP/1.1 or HTTP/2 or HTTP/3, or want to configure Jetty behind a load balancer, read xref:protocols/index.adoc[this section].

----
$ java -jar $JETTY_HOME/start.jar --add-modules=http
----

[jetty%nowrap]
....
[jetty]
args=--add-modules=http
highlight=([a-z\-]+ *transitively enabled)
....

When Jetty enables the `http` module, it also automatically enables a number of transitive dependencies of the `http` module, such as the `server` module, the `logging-jetty` module, and so on.

You can now start Jetty:

----
$ java -jar $JETTY_HOME/start.jar
----

[jetty%nowrap]
....
[jetty]
args=--module=http
highlight=(\{.*:8080})
....

Jetty is listening on port `8080` for clear-text HTTP/1.1 connections.
But since it has no web applications deployed, it will just reply with `404 Not Found` to every request.

Before you <<deploy,deploy your first web application>>, take a moment to see what happened to the `$JETTY_BASE` directory once you enabled the `http` module:

[source]
----
$JETTY_BASE
├── resources
│   └── jetty-logging.properties <1>
└── start.d                      <2>
    ├── bytebufferpool.ini
    ├── http-config.ini
    ├── http.ini                 <3>
    ├── scheduler.ini
    ├── server.ini
    └── threadpool.ini
----

<1> The `resources/jetty-logging.properties` file configures the server's logging level; this file was auto-generated when the `jetty-logging` module was activated as a transitive dependency of the `http` module.
<2> The `start.d/` directory contains the `+*.ini+` configuration files for any modules you have enabled, either explicitly or transitively.
<3> The `start.d/http.ini` file is the `http` module configuration file, where you can specify values for the xref:modules/standard.adoc#http[`http` module properties].

[NOTE]
====
Jetty generates `+*.ini+` configuration files in `start.d/` also for modules enabled as transitive dependencies that have an xref:modules/index.adoc#directive-ini-template[`+[ini-template]+` directive].
====

In the `http.ini` file you can find the following (among other contents):

.http.ini
[source]
----
--module=http <1>
# jetty.http.port=8080 <2>
...
----

<1> This line enables the `http` module and should not be modified.
<2> This commented line specifies the default value for the `jetty.http.port` property, which is the network port that Jetty uses to listen for clear-text HTTP connections.

Try changing the default port.
Open `http.ini`, uncomment the line containing `jetty.http.port=`, and change its value to `9999`:

.http.ini
----
--module=http
jetty.http.port=9999
...
----

If you restart Jetty, it will use this new value:

----
$ java -jar $JETTY_HOME/start.jar
----

[jetty%nowrap]
....
[jetty]
args=--module=http jetty.http.port=9999
highlight=(\{.*:9999})
....

You can also specify the value of a module property when you start up Jetty.
A property value specified on the command-line in this way will *override* the value configured in a module's `+*.ini+` file.

----
$ java -jar $JETTY_HOME/start.jar jetty.http.port=8080
----

[jetty%nowrap]
....
[jetty]
args=--module=http jetty.http.port=8080
highlight=(\{.*:8080})
....

For more detailed information about the Jetty start mechanism, you can read the xref:arch/index.adoc#start[Jetty start mechanism] section.

[[deploy]]
== Deploying Web Applications

Deployments of web applications in Jetty is detailed in xref:deploy/index.adoc[this section].

Each web application is deployed to a specific _environment_, such as the old Java EE 8, or Jakarta {ee-current-caps}, or Jetty specific environments.

Jetty supports _simultaneous_ deployment of web applications each to a possibly different environment, for example an old Java EE 8 web application alongside a new Jakarta {ee-current-caps} web application.

In the following sections you can find simple examples of deployments of Jakarta {ee-current-caps} web applications.

[[deploy-war]]
=== Deploying +*.war+ Files

A Jakarta {ee-current-caps} web application is packaged in either a `+*.war+` file or in a directory with the structure of a `+*.war+` file.

[NOTE]
====
Recall that the structure of a `+*.war+` file is as follows:

[source]
----
mywebapp.war
├── index.html <1>
└── WEB-INF <2>
    ├── classes/ <3>
    ├── lib/ <4>
    └── web.xml <5>
----
<1> Publicly accessible resources such as `+*.html+`, `+*.jsp+`, `+*.css+`, `+*.js+` files, etc. are placed in `+*.war+` or in sub-directories of the `+*.war+`.
<2> `WEB-INF` is a special directory used to store anything related to the web application that must not be publicly accessible, but may be accessed by other resources.
<3> `WEB-INF/classes` stores the web application's compiled `+*.class+` files
<4> `WEB-INF/lib` stores the web application's `+*.jar+` files
<5> `WEB-INF/web.xml` is the web application deployment descriptor, which defines the components and the configuration of your web application.
====

To deploy a Jakarta {ee-current-caps} web application, you need to enable the xref:modules/standard.adoc#eeN-deploy[`{ee-current}-deploy` module].

[NOTE]
====
The following examples assume you are deploying a Jakarta {ee-current-caps} web application; for other versions of Jakarta EE, or for Jetty web applications, make sure to activate the corresponding deploy module.

Refer to the section about xref:deploy/index.adoc[deployment] for further information about how to deploy to different environments.
====

[source,subs=attributes+]
----
$ java -jar $JETTY_HOME/start.jar --add-modules={ee-current}-deploy
----

[jetty%nowrap]
....
[jetty]
setupArgs=--add-modules=http
args=--add-modules={ee-current}-deploy
....

The `{ee-current}-deploy` module creates `$JETTY_BASE/webapps`, which is the directory where Jetty looks for any `+*.war+` files or web application directories to deploy.

Activating one of Jetty's `{ee-all}-deploy` modules enables web application deployment for that specific environment.
Whether these web applications are served via clear-text HTTP/1.1, or secure HTTP/1.1, or secure HTTP/2, or HTTP/3 (or even all of these protocols) depends on whether the correspondent Jetty protocol modules have been enabled.
Refer to the xref:protocols/index.adoc[section about protocols] for further information.

Now you're ready to copy a web application to the `$JETTY_BASE/webapps` directory.
You can use one of the demos shipped with Jetty:

[source,subs=attributes+]
----
$ java -jar $JETTY_HOME/start.jar --add-modules={ee-current}-demo-simple
----

The `$JETTY_BASE` directory is now:

[source,subs=attributes+]
----
$JETTY_BASE
├── resources
│   └── jetty-logging.properties
├── start.d
│   ├── ...
│   ├── {ee-current}-deploy.ini
│   ├── {ee-current}-demo-simple.ini
│   └── http.ini
└── webapps
    └── {ee-current}-demo-simple.war
----

Now start Jetty:

----
$ java -jar $JETTY_HOME/start.jar
----

[jetty%nowrap]
....
[jetty]
setupArgs=--add-modules=http,deploy,{ee-current}-demo-simple
highlight=WebAppContext
....

Note the highlighted line that logs the deployment of `{ee-current}-demo-simple.war`.

Now you can access the web application by pointing your browser to `pass:a[http://localhost:8080/{ee-current}-demo-simple]`.

[[deploy-war-advanced]]
=== Advanced Deployment

If you want to customize the deployment of your web application -- for example, by specifying a `contextPath` different from the file/directory name, or by specifying JNDI entries, or by specifying virtual hosts -- read xref:deploy/index.adoc[this section].

---

### Documentation Jetty Modules Operations Guide Pages Deploy Index (`documentation/jetty/modules/operations-guide/pages/deploy/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Web Application Deployment

The Jetty server supports the deployment of these types of web applications:

* <<web-application-format-jakarta,Jakarta EE 9 to Jakarta {ee-current-caps} web applications>> -- web applications written using the corresponding Servlet (and associated specifications) in the `+jakarta.*+` packages.
* <<web-application-format-jakarta,Java EE 8 web applications>> -- web applications written using the Servlet 4.0 (and associated specifications) in the `+javax.*+` packages.
* <<web-application-format-core,Jetty Core web applications>> -- web applications written against the Jetty `Handler` APIs, without any Servlet dependencies.
* <<web-application-format-context,Jetty `ContextHandler` web applications>> -- web applications made of Jetty ``Handler``s shipped with the Jetty libraries.
* <<web-application-format-static,Jetty Static web applications>> -- web applications made of static files only.

In the following, the term "web application" is used to refer to either a Jetty Static web application, or Jetty Core web application, or Jetty `ContextHandler` web application, or Java EE 8 web application or Jakarta EE 9 to Jakarta {ee-current-caps} web application, unless specified otherwise.

Also, in the following, references to "Jakarta EE" refer to both Java EE and Jakarta EE, unless specified otherwise.

The Jetty server deploys each web application to a specific _environment_.

The available environments are:

* The `static` environment, for Jetty Static web applications.
* The `core` environment, for Jetty Core web applications, and Jetty `ContextHandler` web applications.
* The `{ee-all}` environments, for Jakarta EE web applications.

You can simultaneously deploy an old Java EE 8 web application, say `old-ee8.war`, alongside a new Jakarta {ee-current-caps} web application, say `new-{ee-current}.war`, alongside a web application that only uses the Jetty Core `Handler` APIs, say `backoffice-core.xml`, all in the same Jetty server.

For each specific environment there is a specific deploy module that must be enabled:

* For the `static` environment, the xref:modules/standard.adoc#static-deploy[`static-deploy`] module.
* For the `core` environment, the xref:modules/standard.adoc#core-deploy[`core-deploy`] module.
* For the `{ee-all}` environments, the corresponding xref:modules/standard.adoc#eeN-deploy[`{ee-all}-deploy`] module.

Each of these modules provides environment specific features, such as library dependencies and default configuration.
The Jakarta EE deploy modules provide the corresponding Jakarta EE APIs and the Jetty implementation of those APIs.

For example, you can enable the `{ee-current}-deploy` module in the following way:

[source,bash,subs=attributes+]
----
$ java -jar $JETTY_HOME/start.jar --add-modules={ee-current}-deploy
----

All deploy modules depend on the xref:modules/standard.adoc#deployment-scanner[`deployment-scanner`] module, that scans <<monitored-directories,monitored directories>> for <<web-application-files,web applications>> to deploy, and for <<environment-configuration,environment configuration>> files to process.

NOTE: While you can have many deploy modules enabled, one for each environment, there is only one `deployment-scanner` module that scans the monitored directories.

By default, for security reasons, web applications are only deployed at server startup from the `$JETTY_BASE/webapps` directory.

However, you can configure the `deployment-scanner` module to deploy web applications from a <<monitored-directories,different deploy directory>>, and/or to deploy/undeploy web applications as they are moved to/removed from the deploy directory (<<hot-cold,hot deployment>>).

[[monitored-directories]]
== Monitored Directories

The `deployment-scanner` module scans the web applications deploy directory (or directories) specified by the `jetty.deploy.webappsDir` property, by default `$JETTY_BASE/webapps`, for web applications to deploy.

The directory specified by the `jetty.deploy.environmentsDir` property, by default `$JETTY_BASE/environments`, is also scanned for configuration files that apply to each environment, and therefore to all web applications deployed to that environment.

[[web-application-files]]
== Web Application Files

A web application _basefile_ can be:

* A <<context-xml, Jetty context XML file>>, for Jetty Static, Jetty Core and Jakarta EE web applications.
* A `+*.war+` file, for Jakarta EE web applications.
* A directory, for Jetty Static, Jetty Core and Jakarta EE web applications.

A web application has a _basename_ derived either from the _basefile_ described above, that is either from the Jetty context XML file name, from the `+*.war+` file name, or from the directory name.

For example, for the Jetty context XML file `old-site.xml`, the web application _basename_ is `old-site`.
For the file `myapp.war`, the _basename_ is `myapp`.
For the directory `shop-app/` the _basename_ is `shop-app`.

Web applications may have an associated `<basename>.properties` configuration file, for example `old-site.properties` or `shop-app.properties`.

This `<basename>.properties` file contains properties that influence the deployment of the web application, for example by declaring its environment, as described <<rules-environment,in this section>>.

A web application `<basename>.properties` configuration file can specify the following properties:

* `environment=<environment>`, to specify the web application environment.
* `jetty.deploy.contextPath=<contextPath>`, to specify the web application `contextPath` (see also <<rules-context-path,this section>>).
* `jetty.deploy.baseResource=<pathToBaseResource>`, to specify the web application base resource directory.
The web application base resource directory can be outside of `$JETTY_BASE/webapps`.
* `jetty.deploy.baseResource.dirAllowed=<true|false>`, to specify whether directory listing for the base resource directory is allowed (by default `true`).

[[web-application-format]]
== Web Application Format

[[web-application-format-jakarta]]
=== Jakarta EE Web Application Format

For Jakarta EE web applications, the deployment unit is the usual `+*.war+` file or a directory with a `+*.war+` structure.

Like for the <<web-application-format-core,Jetty Core web application format>>, the `+*.war+` file or a directory can contain an optional in-application <<jetty-web-xml,Jetty context XML file>> under the `WEB-INF` directory, that allows you to configure the web application deployment.

[[web-application-format-core]]
=== Jetty Core Web Application Format

For Jetty Core web applications, the web application format is a directory with the following structure:

[,bash]
----
core-app
├── static         # Static files are served from this directory.
│   ├── css
│   │   └── styles.css
│   └── index.html
├── classes        # Where web application Java classes reside.
│   └── com
│       └── acme
│           └── AcmeHandler.class
├── lib            # Where web application Java libraries reside.
│   └── acme-util.jar
└── jetty-web.xml  # Web application specific configuration.
----

The structure is similar to that of a `+*.war+` file.

A notable difference with `+*.war+` files is that static files are only served from the `static/` directory (with the `static/` prefix removed).
This means that files in the directory root, or under any other directory that is not `static/`, are hidden and if requested they will result in a `404 Not Found` response.
If the `static/` directory is missing, then no static files can be served, and therefore all requests are processed dynamically by the ``Handler``s configured in the web application.

Since static files can only be served from the `static/` directory, the `classes/` and `lib/` directories are implicitly hidden, as well as any other directory such as `includes/` or `fragments/`, and any file in the directory root.

The `classes/` directory stores the web application's `+*.class+` files, while the `lib/` directory stores the web application's libraries, typically `+*.jar+` files.
Both these directories are optional.

The <<jetty-web-xml,`jetty-web.xml`>> file is an optional in-application Jetty context XML file, that allows you to configure the web application deployment.

[[web-application-format-context]]
=== Jetty `ContextHandler` Web Application Format

For Jetty `ContextHandler` web applications, the web application format is just a Jetty XML file, that contains references to Jetty ``Handler``s shipped with the Jetty libraries.
Refer to xref:programming-guide:server/http.adoc#handler-use[this section] for the available Jetty ``Handler``s.

For example, you can use xref:programming-guide:server/http.adoc#handler-use-moved[`MovedContextHandler`] to redirect an old issue-tracking website at `bugs.acme.org` to the new issue-tracking website at `github.com`:

----
$JETTY_BASE/webapps
└── bugs.acme.org.xml
----

.bugs.acme.org.xml
[,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">

<Configure class="org.eclipse.jetty.server.handler.MovedContextHandler">
  <Set name="virtualHosts">
    <Array type="string"><Item>bugs.acme.org</Item></Array>
  </Set>
  <Set name="contextPath">/</Set>
  <Set name="redirectURI">https://github.com/acme/acme/issues</Set>
  <Set name="statusCode">301</Set>
  <Set name="discardPathInContext">true</Set>
  <Set name="discardQuery">true</Set>
</Configure>
----

In the example above, file `$JETTY_BASE/webapps/bugs.acme.org.xml` specifies a web application deployed under virtual host `bugs.acme.org` at context path `/`.

The web application is a `MovedContextHandler` that redirects with HTTP status code `301` (a permanent redirect).

Every request to `+http://bugs.acme.org/<any-path-or-query>+` will be redirected to `+https://github.com/acme/acme/issues+`.

Another example is the use of xref:programming-guide:server/http.adoc#handler-use-context[`ContextHandler`] in conjunction with xref:programming-guide:server/http.adoc#handler-use-rewrite[`RewriteHandler`] to rewrite old documentation URIs to new documentation URIs:

----
$JETTY_BASE/webapps
└── docs.project.org.xml
----

.docs.project.org.xml
[,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">

<Configure class="org.eclipse.jetty.server.handler.ContextHandler">
  <Set name="virtualHosts">
    <Array type="string"><Item>docs.project.org</Item></Array>
  </Set>
  <Set name="contextPath">/</Set>
  <Set name="handler">
    <New class="org.eclipse.jetty.rewrite.handler.RewriteHandler">
      <Call name="addRule">
        <Arg>
          <New class="org.eclipse.jetty.rewrite.handler.RewriteRegexRule">
            <Set name="pattern">/docs/#_(.*)</Set>
            <Set name="">/$1</Set>
          </New>
        </Arg>
      </Call>
    </New>
  </Set>
</Configure>
----

In the example above, file `$JETTY_BASE/webapps/docs.project.org.xml` specifies a web application deployed under virtual host `docs.project.org` at context path `/`.

The web application is a `ContextHandler` with a child `RewriteHandler` that rewrites old documentation URIs such as `/docs/#_proj-intro` to `/proj-intro` using a regular expression rewrite rule.

[[web-application-format-static]]
=== Jetty Static Web Application Format

For Jetty Static web applications, the web application format is just a directory, and any file in that directory and any subdirectory is served when requested with the proper request URI -- no files are hidden.

For example, with the following directory structure:

----
$JETTY_BASE/webapps
└── static-app
    ├── css/
    │   └── styles.css
    └── index.html
----

Requests to `+http://localhost:8080/static-app/index.html+` and `+http://localhost:8080/static-app/css/style.css+` will serve the correspondent files.

A request to `+http://localhost:8080/static-app/css/+` will list the content of the directory.

To disable directory listing, create a web application <<web-application-files,properties file>> with the following content:

.static-app.properties
[,properties]
----
# Specifies the environment.
environment=static

# Disables directory listing.
jetty.deploy.baseResource.dirAllowed=false
----

[[hot-cold]]
== Hot vs Cold Deployment

You can configure the `deployment-scanner` module to scan the <<monitored-directories, monitored directories>> every `N` seconds, where `N` is configured via the `jetty.deploy.scanInterval` property.

By default, the scan interval is `0` seconds, which means _cold_ deployment, where the scanning is only performed once, at server startup.
This means that to deploy a new web application, or redeploy/undeploy an existing web application, you will need to stop and restart the Jetty server.

Setting the scan interval to a value greater than zero means that _hot_ deployment is enabled.
The <<monitored-directories, monitored directories>> are scanned periodically for file changes.

When a file is added/changed/removed from the web applications deploy directory (by default `$JETTY_BASE/webapps`), then the correspondent web application is processed for deploy/redeploy/undeploy with the <<rules,deployment rules>> detailed below.

When a file is added/changed/removed from the environments directory (by default `$JETTY_BASE/environments`), the file is processed and all web applications deployed to that specific environment are redeployed.

The following command line enables _hot_ deployment by specifying the `jetty.deploy.scanInterval` property on the command line, and therefore only for this particular run:

----
$ java -jar $JETTY_HOME/start.jar jetty.deploy.scanInterval=5
----

To make _hot_ deployment configuration persistent, you need to edit the `$JETTY_BASE/start.d/deployment-scanner.ini` module configuration file, and uncomment the property `jetty.deploy.scanInterval` and change the value to `5` seconds:

.deployment-scanner.ini
[source,subs=+quotes]
----
--module=deployment-scanner

#jetty.deploy.scanInterval=5#
...
----

When _hot_ deployment is enabled, the scanner may detect changes to multiple files, and will process the changes in the following way:

* Undeploy the web applications that have been removed, if any, in _basename_ [.underline]#reverse# alphabetical order.
* Redeploy the web applications that have been modified, if any, in _basename_ alphabetical order.
* Deploy the web applications that have been added, if any, in _basename_ alphabetical order.

[[rules]]
== Deployment Rules

For <<hot-cold,_cold_ deployment>>, the presence of a web application <<web-application-files,_basefile_>> causes its deployment at server startup.

For <<hot-cold,_hot_ deployment>> the following rules apply:

* Adding a new web application _basefile_ that refers to a new _basename_ to the web applications deploy directory (by default `$JETTY_BASE/webapps`) causes the deployment of the new web application.
For example, adding `mywebapp.war` to an empty `$JETTY_BASE/webapps`.
* Adding a new web application _basefile_ (such as `mywebapp.xml`), or a web application properties file (such as `mywebapp.properties`) to the web applications deploy directory that refers to an existing _basename_ causes the redeployment of the web application.
For example, adding `mywebapp.xml` or `mywebapp.properties` when `mywebapp.war` already exists.
* Changing a file that refers to a _basename_ causes the redeployment of the web application.
For example, editing or changing `mywebapp.xml`, `mywebapp.war` or `mywebapp.properties`.
* Removing some, but not all, of the files that refer to the same _basename_ causes the redeployment of the web application.
For example, removing `mywebapp.xml` or `mywebapp.properties`, but leaving `mywebapp.war`.
* Removing all the files that refer to the same _basename_ causes the undeployment of the web application.
For example, removing `mywebapp.xml`, `mywebapp.war` and `mywebapp.properties`.

NOTE: When a web application is deployed as a directory (not as a file), it is not possible to change the directory (for example using the `touch` command) to cause the redeployment of the web application.
Modification of the files inside the web application directory is not detected as a change.
You must add at least a `<basename>.properties` file (even empty), and `touch` it to cause redeployment.

In the presence of multiple web application __basefile__s that refers to the same _basename_, for example when both `mywebapp.xml` and `mywebapp.war` exist, or when both `mywebapp.war` and `mywebapp/` exist, then the following rules apply:

* If `<basename>.xml` exists, it has precedence over `<basename>.war` and `<basename>/`, so only `<basename>.xml` will be considered for deployment.
This allows the XML file to reference the `+*.war+` file or directory and avoid that the web application is deployed twice.
* Otherwise, if `<basename>.war` exists, it has precedence over `<basename>/`, so only the `+*.war+` file will be considered for deployment.
This allows the directory with the same _basename_ to be the `+*.war+` file unpack location and avoid that the web application is deployed twice.
* Otherwise, `<basename>/` exists and will be considered for deployment.

[[rules-context-path]]
=== Context Path Resolution

When a web application is deployed (or redeployed), the web application `contextPath` is derived from the <<web-application-files,web application files>>, with the following rules:

* If `<basename>.xml` exists, it is processed and the directives contained in the XML file can set the `contextPath` to any value (including one that is different from the _basename_).
* If `<basename>.properties` exists, then the `contextPath` is set to the value of the `jetty.deploy.contextPath` property, if it is present in `<basename>.properties`.
* If the _basename_ is `ROOT`, case-insensitive, the `contextPath` is `/`, and the web application is reachable at `+http://localhost:8080/+`.
* Otherwise, the `contextPath` is the _basename_, and the web application is reachable at `+http://localhost:8080/<basename>+`.

[[rules-environment]]
=== Environment Resolution

A web application is always deployed to a specific environment, which is either configured explicitly for the web application or set to the default environment.

You must always enable at least one deploy module, otherwise no web application can be deployed.

[[rules-environment-single-deployer]]
==== Single Deployer Environment Resolution

If only a single specific deploy module is enabled, for example `{ee-current}-deploy`, then its environment is the default environment and applications will be deployed to that environment without any additional configuration.

If a web application <<web-application-files,properties file>> exists, and it specifies the `environment` property, then its value must specify the same environment as the deploy module, otherwise the deployment will fail.

[CAUTION]
====
Make sure that you do not inadvertently mix a deploy module for one environment with a web application of a different environment.

For example, if you have a Jetty `core` web application in directory `core-app/`, and you enable the `static-deploy` module, then the `core-app/` directory will be interpreted as a `static` web application, and files from the `core-app/classes/` directory (that are normally hidden and not served) will be served as static files.
====

TIP: It is good practice to always specify the web application <<web-application-files,properties file>> and specify the `environment` property, to avoid deployment of your web applications to an unexpected environment.

[[rules-environment-multiple-deployers]]
==== Multiple Deployers Environment Resolution

If multiple deployer modules are enabled, then the default environment is (in order):

* The most recent Jakarta EE environment of the `{ee-all}-deploy` modules that are enabled, if any.
* Otherwise, the `core` environment, if the `core-deploy` module is enabled.
* Otherwise, the `static` environment, if the `static-deploy` module is enabled.

For example, if `core-deploy`, `ee9-deploy` and the `{ee-current}-deploy` modules are enabled, then `{ee-current}` is the default environment, to which applications will be deployed unless otherwise configured (see below).

To configure a specific environment for an application, you add a web application <<web-application-files,properties file>> and specify the `environment` property.

For example, an application deployed to `$JETTY_BASE/webapps/my-ee9-app.war` is configured with the file `$JETTY_BASE/webapps/my-ee9-app.properties`, with the following content:

.my-ee9-app.properties
[,properties]
----
environment=ee9
----

TIP: In case of multiple deploy environments, it is good practice to always specify the web application <<web-application-files,properties file>> for your web applications, and specify the `environment` property, to avoid deployment of your web applications to an unexpected environment.

[CAUTION]
====
If you do *not* specify the `environment` property in the web application <<web-application-files,properties file>> for your web applications, then the deployer for the default environment will be used.

For example, if the `{ee-current}-deploy` and the `ee9-deploy` module are enabled, and you deploy a Jakarta EE 9 web application _without_ the `+*.properties+` file, then it will be deployed to the (default) `{ee-current}` environment, with unspecified results.

This unspecified deployment may not work as the Jakarta EE 9 web application may use APIs that have been removed in {ee-current-caps}, causing an error at runtime.
====

Also in case of multiple deployers, if a web application <<web-application-files,properties file>> specifies an `environment` property, and the corresponding deploy module is not enabled, the deployment will fail.

[[context-xml]]
== Deploying Jetty Context XML Files

A Jetty context XML file is a xref:xml/index.adoc[Jetty XML file] that allows you to customize the deployment of web applications.

NOTE: Recall that the <<rules,deployment rules>> give priority to Jetty context XML files over `+*.war+` files or directories.

To deploy a web application using a Jetty context XML file, simply place the Jetty context XML file in the `$JETTY_BASE/webapps` directory.

For example, to deploy a Jakarta {ee-current-caps} web application, enable the `{ee-current}-deploy` module, and add the following Jetty context XML file, named `wiki.xml`:

.wiki.xml
[,xml,subs=attributes+]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext"> 
  <Set name="contextPath">/wiki</Set> 
  <Set name="war">/opt/myapps/myapp.war</Set> 
</Configure>
----
<1> Configures a link:{javadoc-url}/org/eclipse/jetty/{ee-current}/webapp/WebAppContext.html[`WebAppContext`], which is the Jetty component that represents a standard Servlet web application for Jakarta {ee-current-caps}.
<2> Specifies the web application `contextPath`, which may be different from the _basename_ derived from `wiki.xml`.
<3> Specifies the file system path of the `+*.war+` file (which may be external to `$JETTY_BASE`).

The Jetty context XML file may be accompanied by a web application <<web-application-files,properties file>> that specifies the xref:deploy/index.adoc[environment] to use for the deployment:

.wiki.properties
[,properties,subs=attributes+]
----
environment={ee-current}
----

Refer to <<rules-environment,this section>> for more information about specifying the environment.

The `$JETTY_BASE` directory would look like this:

[,subs=+attributes]
----
$JETTY_BASE
├── resources
│   └── jetty-logging.properties
├── start.d
│   ├── ...
│   ├── {ee-current}-deploy.ini
│   └── http.ini
└── webapps
    ├── wiki.properties
    └── wiki.xml
----

TIP: The `+*.war+` file may be placed anywhere in the file system and does not need to be placed in the `$JETTY_BASE/webapps` directory, as long as you have a Jetty context XML file referencing it.

IMPORTANT: If you place both the Jetty context XML file _and_ the `+*.war+` file in the `$JETTY_BASE/webapps` directory, remember that they must produce the same _basename_, for example `wiki.xml` and `wiki.war`, so that the deployment is performed only once using the Jetty context XML file (and not the `+*.war+` file).

You can use the features of xref:xml/index.adoc[Jetty XML files] to avoid to hard-code file system paths or other configurations in your Jetty context XML files, for example by using properties declared in `wiki.properties` or JVM system properties:

.wiki.xml
[,xml,subs=attributes+]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext">
  <Set name="contextPath"><Property name="wiki.contextPath"/></Set>
  <Set name="war"><SystemProperty name="webapps.dir"/>/myapp.war</Set>
</Configure>
----

Note how the `contextPath` is obtained from the `wiki.contextPath` configuration property (as an alternative to the `jetty.deploy.contextPath` property specified in <<web-application-files,this section>>), while `+*.war+` file path is obtained by resolving the system property `webapps.dir` that you can specify on the command line when you start Jetty:

.wiki.properties
[,properties,subs=+attributes]
----
environment={ee-current}
wiki.contextPath=/wiki
----

[,bash]
----
$ java -jar $JETTY_HOME/start.jar -Dwebapps.dir=/opt/myapps
----

Properties specified in environment properties file (described in <<environment-configuration,this section>>) and in the web application properties file (described in <<web-application-files,this section>>) are available when processing the Jetty context XML file.

Remember that Jetty context XML files in the `$JETTY_BASE/webapps` directory can only refer to classes present in the environment class-path/module-path, in the server class-path/module-path, and in the JVM class-path/module-path.
For more information about the Jetty class loader hierarchy, see xref:start/index.adoc#start-class-path[this section].

If you need to configure the deployment of your web application referring to web application specific classes, use the in-application Jetty context XML file, as described in <<jetty-web-xml,this section>>.

[[environment-configuration]]
== Environment Configuration

Environments can be configured via xref:xml/index.adoc[Jetty XML files] or `+*.properties+` files placed in the `$JETTY_HOME/environments` directory.

Each environment configuration is applied to _all_ web applications deployed/redeployed to that `environment`.

The environment configuration files must start with the environment name, and are processed in alphabetical order, for example `{ee-current}.xml`, `{ee-current}-extra.xml` and `{ee-current}-other.xml` are processed in this order, and they all affect the `{ee-current}` environment.
The same holds for `+*.properties+` files.

The environment-specific Jetty XML files will be applied to the web application _before_ any Jetty context XML file associated with the web application, present in the `$JETTY_HOME/webapps` directory.

IMPORTANT: The contents of the environment specific Jetty context XML file may only contain references to classes appropriate for that environment.

For example, you can have a `$JETTY_HOME/environments/{ee-current}.xml` file with the following content:

.{ee-current}.xml
[,xml,subs=+attributes]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext">
  <Call name="setAttribute">
    <Arg>com.acme.{ee-current}.feature</Arg>
    <Arg>value</Arg>
  </Call>
</Configure>
----

The `$JETTY_HOME/environments/{ee-current}-extra.xml` file may contain:

.{ee-current}-extra.xml
[,xml,subs=+attributes]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext">
  <Call name="addEventListener">
    <Arg>
      <New class="com.acme.MyListenerFeature"/>
    </Arg>
  </Call>
</Configure>
----

Custom classes like `com.acme.MyListenerFeature` must be present in the environment class loader, so that the XML file can reference them.
You can extend the environment class loader by enabling the xref:modules/standard.adoc#eeN-ext[`{ee-current}-ext.mod`].
Refer to xref:start/index.adoc#start-class-path[this section] for more information about the Jetty class loader hierarchy.

The directory structure would look like this:

[,subs=+attributes]
----
$JETTY_BASE
├── environments
│   ├── {ee-current}.xml
│   └── {ee-current}-extra.xml
├── start.d
│   ├── ...
│   └── {ee-current}-ext.ini
└── webapps
    ├── wiki.properties
    └── wiki.xml
----

[[jetty-web-xml]]
== In-Application Jetty Context XML File

As discussed in <<context-xml,this section>>, the deployment of a web application can be specified with a Jetty context XML file placed in the `$JETTY_HOME/webapps` directory.

However, the deployment of a web application can also be configured using a Jetty context XML file placed _inside_ the web application, both for <<web-application-format-core,Core web application deployments>> and <<web-application-format-jakarta,Jakarta EE web application deployments>>.

This file can be useful:

* To place all configuration inside your web application archive.
* To perform configuration of the web application that can only occur _after_ the web application's class-path has been created, for example because it references classes present in web application itself.

[[jetty-web-xml-core]]
=== In-Application Jetty Context XML File for Core Web Application Deployments

The file must be in the root of the web application and named `jetty-web.xml`, see <<web-application-format-core,this section>> for an example.

[[jetty-web-xml-jakarta]]
=== In-Application Jetty Context XML Files for Jakarta EE Web Application Deployments

For Jakarta EE deployments, there may be multiple in-application Jetty context XML files, typically one for each `{ee-all}` environment.

The files must be named either `WEB-INF/jetty-{ee-all}-web.xml` or `WEB-INF/jetty-web.xml`; the latter is only processed if the former(s) are missing.

[TIP]
====
It is good practice to name the file according to the environment into which the web application will be deployed.

If your web application can be deployed to multiple environments, then you should include a `WEB-INF/jetty-{ee-all}-web.xml` file for each environment.

If you only deploy to a single environment, then you can omit it from the filename and use `WEB-INF/jetty-web.xml`, but be aware that likely you will not be able to deploy the web application to a different environment without updating the contents of the file.
====

[[jndi]]
== Configuring JNDI Entries

Web applications may _reference_ JNDI entries, such as a JDBC `DataSource` or a Jakarta Mail `Session`.

The JNDI entries must be _defined_ in a Jetty context XML file, as detailed in xref:jndi/index.adoc[this section].

[[virtual-hosts]]
== Configuring Virtual Hosts

A _virtual host_ is an internet domain name, registered in the Domain Name Server (DNS), for an IP address such that multiple virtual hosts will resolve to the same IP address of a single server instance.

If you have multiple web applications deployed on the same Jetty server, by using virtual hosts you will be able to target a specific web application.

For example, you may have a web application for your business and a web application for your hobbies , both deployed in the same Jetty server.
By using virtual hosts, you will be able to have the first web application available at `+http://domain.biz/+`, and the second web application available at `+http://hobby.net/+`.

Another typical case is when you want to use different subdomains for different web application, for example a project website is at `+http://project.org/+` and the project documentation is at `+http://docs.project.org+`.

Virtual hosts can be used with any context that is a subclass of link:{javadoc-url}/org/eclipse/jetty/server/handler/ContextHandler.html[ContextHandler], and therefore for <<web-application-format-static,Jetty Static web applications>>, <<web-application-format-core,Jetty Core web applications>> and <<web-application-format-jakarta,Jakarta EE web applications>>.

[[virtual-hosts-names]]
=== Virtual Host Names

Jetty supports the following variants to be specified as virtual host names:

`www.hostname.com`::
A fully qualified domain name. It is important to list all variants as a site may receive traffic for both `www.hostname.com` and `hostname.com`.

`+*.hostname.com+`::
A wildcard domain name which will match only one level of arbitrary subdomains.
The wildcard domain `+*.foo.com+` will match `www.foo.com` and `m.foo.com`, but not `www.other.foo.com`.

`10.0.0.2`::
An IP address may be set as a virtual host to indicate that a web application should handle requests received on the network interface with that IP address.
This is typically only used for protocols that do not indicate a host name such as HTTP/0.9 or HTTP/1.0.

`@<ConnectorName>`::
A Jetty server `Connector` name to indicate that a web application should handle requests received on the server `Connector` with that name, and therefore received on a specific socket address, either an IP port for `ServerConnector`, or a Unix-Domain path for `UnixDomainServerConnector` (see also xref:virtual-hosts-port[this section]).

`www.√integral.com`::
Non-ASCII and https://en.wikipedia.org/wiki/Internationalized_domain_name[IDN] domain names can be set as virtual hosts using https://en.wikipedia.org/wiki/Punycode[Puny Code] equivalents that may be obtained from a https://www.punycoder.com/[Punycode/IDN converters].
For example if the non-ASCII domain name `www.√integral.com` is given to a browser, then the browser will make a request that uses the domain name `www.xn--integral-7g7d.com`, which is the name that should be added as the virtual host name.

[[virtual-hosts-config]]
=== Virtual Hosts Configuration

If you have a web application `mywebapp.war` you can configure its virtual hosts in this way:

[,xml,subs=attributes+]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext">
  <Set name="contextPath">/mywebapp</Set>
  <Set name="war">/opt/webapps/mywebapp.war</Set>
  <Set name="virtualHosts">
    <Array type="java.lang.String">
      <Item>mywebapp.com</Item>
      <Item>www.mywebapp.com</Item>
      <Item>mywebapp.net</Item>
      <Item>www.mywebapp.net</Item>
    </Array>
  </Set>
</Configure>
----

Your web application will be available at:

* `+http://mywebapp.com/mywebapp+`
* `+http://www.mywebapp.com/mywebapp+`
* `+http://mywebapp.net/mywebapp+`
* `+http://www.mywebapp.net/mywebapp+`

[NOTE]
====
In the example above, the web application `contextPath` is configured to `/mywebapp`.

As such, a request to `+http://mywebapp.com/other+` will not match your web application because the `contextPath` does not match.

Likewise, a request to `+http://other.com/mywebapp+` will not match your web application because the virtual host does not match.
====

[[virtual-hosts-same-context]]
=== Same Context Path, Different Virtual Hosts

If you want to deploy different web applications to the same context path, typically the root context path `/`, you must use virtual hosts to differentiate among web applications.

You have `domain.war` that you want to deploy at `+http://domain.biz/+` and `hobby.war` that you want to deploy at `+http://hobby.net+`.

To achieve this, you simply use the same context path of `/` for each of your web applications, while specifying different virtual hosts for each of your web applications:

.domain.xml
[,xml,subs=attributes+]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext">
  <Set name="contextPath">/</Set>
  <Set name="war">/opt/webapps/domain.war</Set>
  <Set name="virtualHosts">
    <Array type="java.lang.String">
      <Item>domain.biz</Item>
    </Array>
  </Set>
</Configure>
----

.hobby.xml
[,xml,subs=attributes+]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext">
  <Set name="contextPath">/</Set>
  <Set name="war">/opt/webapps/hobby.war</Set>
  <Set name="virtualHosts">
    <Array type="java.lang.String">
      <Item>hobby.net</Item>
    </Array>
  </Set>
</Configure>
----

[[virtual-hosts-port]]
=== Different Port, Different Web Application

Sometimes it is required to serve different web applications from different socket addresses (either different IP ports, or different Unix-Domain paths), and therefore from different server ``Connector``s.

For example, you want requests to `+http://localhost:8080/+` to be served by one web application, but requests to `+http://localhost:9090/+` to be served by another web application.

This configuration may be useful when Jetty sits behind a load balancer.

In this case, you want to xref:protocols/index.adoc[configure multiple connectors], each with a different name specified with link:{javadoc-url}/org/eclipse/jetty/server/AbstractConnector.html#setName(java.lang.String)[], and then reference the connector name in the web application virtual host configuration:

.domain.xml
[,xml,subs="attributes+,+quotes"]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext">
  <Set name="contextPath">/</Set>
  <Set name="war">/opt/webapps/domain.war</Set>
  <Set name="virtualHosts">
    <Array type="java.lang.String">
      #<Item>@port8080</Item>#
    </Array>
  </Set>
</Configure>
----

.hobby.xml
[,xml,subs="attributes+,+quotes"]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext">
  <Set name="contextPath">/</Set>
  <Set name="war">/opt/webapps/hobby.war</Set>
  <Set name="virtualHosts">
    <Array type="java.lang.String">
      #<Item>@port9090</Item>#
    </Array>
  </Set>
</Configure>
----

[NOTE]
====
Web application `domain.war` has a virtual host of `@port8080`, where `port8080` is the name of a Jetty connector.

Likewise, web application `hobby.war` has a virtual host of `@port9090`, where `port9090` is the name of another Jetty connector.

See xref:protocols/index.adoc[this section] for further information about how to configure connectors.
====

[[extract-war]]
== Configuring `+*.war+` File Extraction

By default, when a Jakarta EE web application in `+*.war+` file format is deployed, the `+*.war+` file is uncompressed and its content extracted in a temporary directory.
// TODO: reference the `work` module and how it works, perhaps in a section about the `deploy` module?
The web application resources are served by Jetty from the files extracted in the temporary directory, not from the files within the `+*.war+` file, for performance reasons.

If you do not want Jetty to extract the `+*.war+` files, you can disable this feature in this way:

.mywebapp.xml
[,xml,subs="attributes+,+quotes"]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext">
  <Set name="contextPath">/mywebapp</Set>
  <Set name="war">/opt/webapps/mywebapp.war</Set>
  #<Set name="extractWAR">false</Set>#
</Configure>
----

[[jetty-override-web-xml]]
== Overriding `web.xml`

When deploying Jakarta EE web applications, you can configure an additional `web.xml` that complements the `web.xml` file that is present in the web application `+*.war+` file.
This additional `web.xml` is processed _after_ the `+*.war+` file `web.xml`.
This allows you to add host specific configuration or server specific configuration without having to extract the web application `web.xml`, modify it, and repackage it in the `+*.war+` file.

.mywebapp.xml
[,xml,subs="attributes+,+quotes"]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext">
  <Set name="contextPath">/mywebapp</Set>
  <Set name="war">/opt/webapps/mywebapp.war</Set>
  #<Set name="overrideDescriptor">/opt/webapps/mywebapp-web.xml</Set>#
</Configure>
----

The format of the additional `web.xml` is exactly the same as a standard `web.xml` file, for example:

.mywebapp-web.xml
[,xml,linenums,highlight=10-11]
----
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_1.xsd"
         version="6.1">
  <servlet>
    <servlet-name>my-servlet</servlet-name>
    <init-param>
      <param-name>host</param-name>
      <param-value>192.168.0.13</param-value>
    </init-param>
  </servlet>
</web-app>
----

In the example above, you configured the `my-servlet` Servlet (defined in the web application `web.xml`), adding a host-specific `init-param` with the IP address of the host.

---

### Documentation Jetty Modules Operations Guide Pages Features Index (`documentation/jetty/modules/operations-guide/pages/features/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Eclipse Jetty Features

If you know Eclipse Jetty already, jump to a feature:

Protocols::
* xref:protocols/index.adoc#http[HTTP/1.1 Support]
* xref:protocols/index.adoc#http2[HTTP/2 Support]
* xref:protocols/index.adoc#http3[HTTP/3 Support]
* xref:protocols/index.adoc#websocket[WebSocket Support]

Technologies::
* xref:annotations/index.adoc[Servlet Annotations]
* xref:security/jaas-support.adoc[JAAS]
* xref:jndi/index.adoc[JNDI]
* xref:jsp/index.adoc[JSP]
* xref:jmx/index.adoc[JMX Monitoring & Management]

Clustering::
* xref:session/index.adoc[HTTP Session Caching and Clustering]

Performance::
* xref:server/index.adoc#threadpool-virtual[Virtual Threads]
* xref:quickstart/index.adoc[Faster Jakarta EE Web Application Deployment]

---

### Documentation Jetty Modules Operations Guide Pages Howtos Index (`documentation/jetty/modules/operations-guide/pages/howtos/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Eclipse Jetty How-Tos

* xref:protocols/index.adoc#http[Configure Clear-Text HTTP/1.1]
* xref:protocols/index.adoc#https[Configure Secure HTTP/1.1 (https)]
* xref:protocols/index.adoc#http2c[Configure Clear-Text HTTP/2]
* xref:protocols/index.adoc#http2s[Configure Secure HTTP/2]
* xref:protocols/index.adoc#http3[Configure HTTP/3]
* xref:protocols/index.adoc#proxy[Configure Jetty Behind a Load Balancer or Reverse Proxy]
* xref:server/index.adoc#logging[Configure Jetty Logging]
* xref:server/index.adoc#threadpool[Configure Jetty Thread Pool and Virtual Threads]
* xref:troubleshooting/index.adoc[Troubleshooting]

---

### Documentation Jetty Modules Operations Guide Pages Jmx Index (`documentation/jetty/modules/operations-guide/pages/jmx/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= JMX Monitoring & Management

Monitoring and management of a Jetty server is important because it allows you to monitor the status of the server (_"Is the server processing requests?"_) and to manage -- i.e. read and possibly change -- its configuration.

The ability to read and change the Jetty configuration is very important for troubleshooting Jetty -- please refer to the xref:troubleshooting/index.adoc[troubleshooting section] for more information.

Jetty relies on the Java Management Extensions (JMX) APIs included in OpenJDK to provide monitoring and management.

The JMX APIs support a JVM-local `MBeanServer`, accessible only from within the JVM itself (or by tools that can _attach_ to a running JVM), and a way to expose the `MBeanServer` to remote clients via Java's RMI (Remote Method Invocation).

[[local]]
== Enabling Local JMX Support

As with many other Jetty features, local JMX support is enabled with the xref:modules/standard.adoc#jmx[`jmx` Jetty module]:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=jmx
----

With the `jmx` Jetty module enabled, Jetty components will be exported as JMX _MBeans_ to the JVM platform `MBeanServer`, so that they can be accessed by JMX compliant tools.

Each Jetty component will export to its correspondent MBean relevant configuration parameters, so that a JMX tool can read and possibly change the component configuration through the MBean.

Note that the Jetty MBeans are registered into the platform `MBeanServer`, but are not available to remote clients: they are _local_ to the JVM.

This configuration is useful when you develop and test your Jetty server locally.

JMX compliant tools such as https://adoptium.net/jmc.html[Java Mission Control (JMC)] can be started locally on your machine and can attach to other JVMs running on your machine, showing you the registered MBeans among which you will find the Jetty MBeans.

NOTE: Enabling only the local JMX support is the most secure option for monitoring and management, but only users that have local access to the JVM will be able to browse the MBeans.
If you need to access the MBeans from a remote machine, read <<remote,this section>>.

[[remote]]
== Enabling Remote JMX Support

There are two ways to configure a Jetty server so that it is possible to access the JVM platform MBeans from remote clients:

* Use the `com.sun.management.jmxremote` and related system properties when starting Jetty.
Unfortunately, this solution does not work well with firewalls, and will not be discussed further.
* Use the `jmx-remote` Jetty module.

Both ways use Java's Remote Method Invocation (RMI) to communicate between the client and the server.

[IMPORTANT]
.Refresher: How RMI Works
====
A server application that wants to make an object available to remote clients must _export_ the object.

Exporting an object creates an RMI _stub_ that contains the host/port of the RMI _server_ that accepts incoming invocations from clients and forwards them to the object.
During the creation of the RMI stub, the host stored in the RMI stub is retrieved from the local name resolution system (for example, in Linux, from `/etc/hosts`).

The RMI stub is then sent, along with a name that uniquely identifies the object, to the RMI _registry_.
The RMI registry is a service that maps names to RMI stubs; it may be external to both clients and server, although often it is part of the server JVM.

When a client application wants to connect to the server object using RMI, it first connects to the RMI registry to download the RMI stub for the RMI server; recall that the RMI stub contains the host/port to connect to the RMI server.
Then, the client uses the RMI stub to connect to the RMI server, typically to a host/port that may be different from the RMI registry host/port (in particular, by default the RMI server port will be different from the RMI registry port).
====

Remote access to the platform MBeans, and therefore the Jetty MBeans, is enabled by the `jmx-remote` Jetty module:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=jmx-remote
----

This command creates the `jmx-remote.ini` file:

[source,subs=+quotes]
----
JETTY_BASE
└── start.d
    └── #jmx-remote.ini#
----

Enabling the `jmx-remote` module transitively enables the <<local,`jmx` module>> as well.

The configuration for the RMI registry and the RMI server is specified by a `JMXServiceURL`.
The string format of an RMI `JMXServiceURL` is the following:

----
service:jmx:rmi://<rmi_server_host>:<rmi_server_port>/jndi/rmi://<rmi_registry_host>:<rmi_registry_port>/jmxrmi
----

Below you can find examples of ``JMXServiceURL``s:

[source,subs=+quotes]
----
*service:jmx:rmi:///jndi/rmi:///jmxrmi*
where:
  rmi_server_host = local host address
  rmi_server_port = randomly chosen
  rmi_registry_host = local host address
  rmi_registry_port = 1099

*service:jmx:rmi://0.0.0.0:1099/jndi/rmi://0.0.0.0:1099/jmxrmi*
where:
  rmi_server_host = any address
  rmi_server_port = 1099
  rmi_registry_host = any address
  rmi_registry_port = 1099

*service:jmx:rmi://localhost:1100/jndi/rmi://localhost:1099/jmxrmi*
where:
  rmi_server_host = loopback address
  rmi_server_port = 1100
  rmi_registry_host = loopback address
  rmi_registry_port = 1099
----

The default `JMXServiceURL` configured by the `jmx-remote` module is the following:

----
service:jmx:rmi://localhost:1099/jndi/rmi://localhost:1099/jmxrmi
----

With the default configuration, only clients that are local to the server machine can connect to the RMI registry and RMI server - this is done for security reasons.
However, even with this local-only configuration, it would still be possible to access the MBeans from remote using an SSH tunnel, as explained in <<remote-ssh-tunnel,this section>>.

By specifying an appropriate `JMXServiceURL`, you can fine tune the network address the RMI registry and the RMI server bind to, and the ports that the RMI registry and the RMI server listen to.
The RMI server and RMI registry hosts and ports can be the same (as in the default configuration) because RMI is able to multiplex traffic arriving to one port to multiple RMI objects.

If you need to allow JMX remote access through a firewall, you must open both the RMI registry and the RMI server ports.
The default configuration simplifies the firewall configuration because you only need to open port `1099`.

[NOTE]
====
When Jetty is started with the `jmx-remote` module enabled, the RMI stub of the Jetty component that provides access to the MBeans is exported to the RMI registry.

The RMI stub contains the host/port to connect to the RMI server, but the host is typically the machine host name, not the host specified in the `JMXServiceURL` (the latter is only used to specify the network address the RMI server binds to).

To control the host stored in the RMI stub you need to set the system property `java.rmi.server.hostname` with the desired value in the module configuration file, `jmx-remote.ini`.
====

IMPORTANT: If your client cannot connect to the server, the most common cause is a mismatch between the RMI server host of the `JMXServiceURL` and the RMI server host of the RMI stub.

You can customize the RMI server host/port, the RMI registry host/port and the system property `java.rmi.server.hostname` by editing the `jmx-remote.ini` configuration file.
Further information about the `jmx-remote` module configuration can be found xref:modules/standard.adoc#jmx-remote[here].

[[remote-ssh-tunnel]]
=== Remote JMX Access with Port Forwarding via SSH Tunnel

You can access JMX MBeans on a remote machine when the RMI ports are not open, for example because of firewall policies, but you have SSH access to the machine, using local port forwarding via an SSH tunnel.

In this case you want to configure the `JMXServiceURL` that binds the RMI server and the RMI registry to the loopback interface only and to the same port:

----
service:jmx:rmi://localhost:1099/jndi/rmi://localhost:1099/jmxrmi
----

You must set the system property `-Djava.rmi.server.hostname=localhost` so that the RMI stub contains `localhost` as the host name to connect to.
This is, incidentally, the default configuration of the `jmx-remote` module.

Then you set up the local port forwarding with the SSH tunnel:

----
$ ssh -L 1099:localhost:1099 <user>@<machine_host>
----

Thanks to the local port forwarding of the SSH tunnel, when the client connects to `localhost:1099` on your local computer, the traffic will be forwarded to `machine_host` and when there, the SSH daemon will forward the traffic to `localhost:1099` on `machine_host`, which is exactly where the RMI server and the RMI registry listens to.

The client first contacts the RMI registry, so it connects to `localhost:1099` on your local computer; the traffic is forwarded to `machine_host` through the SSH tunnel, connects to the RMI registry and the RMI stub is downloaded to the client.

Then the client uses the RMI stub to connect to the RMI server. The RMI stub contains `localhost` as the RMI server host because that is what you have configured with the system property `java.rmi.server.hostname`.

The client will connect again to `localhost:1099` on your local computer, this time to contact the RMI server; the traffic is forwarded to `machine_host` through the SSH tunnel, arrives to `machine_host` and connects to the RMI server.

[[remote-auth]]
=== Remote JMX Access Authentication & Authorization

The standard `javax.management.remote.JMXConnectorServer` class, used by the `jmx-remote` module to provide remote JMX access to Jetty MBeans, provides several options to authenticate and authorize users.
For a complete guide to controlling authentication and authorization in JMX, see https://docs.oracle.com/en/java/javase/11/management/monitoring-and-management-using-jmx-technology.html[the official JMX documentation].

The simplest way to control JMX authentication and authorization is to specify two files: one contains username and password pairs, and the other contains username and permission pairs.

This is achieved by enabling the `jmx-remote-auth` Jetty module:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=jmx-remote-auth
----

Enabling the `jmx-remote-auth` Jetty module creates the following files:

----
$JETTY_BASE
├── etc
│   ├── jmxremote.access
│   ├── jmxremote.password
│   └── jmx-remote-auth.xml
└── start.d
    ├── jmx-remote-auth.ini
    └── jmx-remote.ini
----

Then you edit the `$JETTY_BASE/etc/jmxremote.password` file, adding the username/password pairs that you need:

.$JETTY_BASE/etc/jmxremote.password
----
# The file format is: <username> <password>
alice wonderland
bob marley
----

You must also edit the `$JETTY_BASE/etc/jmxremote.access` file to give permissions to your users:

.$JETTY_BASE/etc/jmxremote.access
----
# The file format is: <username> <readonly|readwrite>
alice readwrite
bob readonly
----

The above files define user `alice` with password `wonderland` to have `readwrite` access, and user `bob` with password `marley` to have `readonly` access.

[[remote-secure]]
=== Securing Remote JMX Access with TLS

The JMX communication via RMI happens by default in clear-text, but it is possible to secure the JMX communication via RMI with TLS.

If you want to reuse the configuration that you are using for the xref:protocols/index.adoc#https[`https` module], you can just enable the `jmx-remote-ssl.xml` Jetty module:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=jmx-remote-ssl
----

[NOTE]
====
The `jmx-remote-ssl` Jetty module depends on the `ssl` Jetty module that in turn requires a KeyStore (read xref:protocols/index.adoc#ssl[this section] for more information).
====

The KeyStore must contain a valid certificate signed by a Certification Authority.
Having certificates signed by a Certification Authority simplifies by a lot the configuration needed to get the RMI communication over TLS working properly.

The RMI mechanic is the usual one: the RMI client (typically a monitoring console) will connect first to the RMI registry (using TLS), download the RMI stub that contains the address and port of the RMI server to connect to, then connect to the RMI server (using TLS).

This also mean that if the RMI registry and the RMI server are on different hosts, the RMI client must have available the cryptographic material to validate the certificates from both hosts.
This is where having certificates signed by a Certification Authority simplifies the configuration: if they are signed by a well known Certification Authority, the client does not need any extra configuration -- everything will be handled by the Java runtime.

If the certificates are not signed by a Certification Authority (for example the certificate is self-signed), then you need to specify the TLS system properties that allow RMI (especially when acting as an RMI client) to retrieve the cryptographic material necessary to establish the TLS connection.

[IMPORTANT]
====
When the RMI server exports the `JMXConnectorServer` it acts as an RMI _client_ towards the RMI registry, and as such you must specify the TLS system properties as detailed below.
====

You must edit the `$JETTY_BASE/start.d/jmx-remote-ssl.ini` file and add the TrustStore path and password:

.$JETTY_BASE/start.d/jmx-remote-ssl.ini
----
--module=jmx-remote-ssl

# System properties necessary for non-trusted certificates.
-Djavax.net.ssl.trustStore=/path/to/trustStore.p12
-Djavax.net.ssl.trustStorePassword=password
----

[IMPORTANT]
====
The TrustStore must contain the certificate you want to trust.

If you are using self-signed certificates, the KeyStore already contains the self-signed certificate and therefore the KeyStore can be used as a TrustStore, and the system properties above can refer to the KeyStore path and password.
====

JMX compliant tools that offer a graphical user interface also must be started specifying the TrustStore path and password.

For example, to launch https://adoptium.net/jmc.html[Java Mission Control (JMC)]:

----
$ jmc -vmargs -Djavax.net.ssl.trustStore=/path/to/trustStore.p12 -Djavax.net.ssl.trustStorePassword=password
----

---

### Documentation Jetty Modules Operations Guide Pages Jndi Index (`documentation/jetty/modules/operations-guide/pages/jndi/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= JNDI

For each specific xref:deploy/index.adoc[_environment_] there is a specific Jetty module that you must enable in order to use JNDI resources in your web application:

* For Jetty Static and Core web applications, the `plus` module.
* For Jakarta {ee-all-caps} web applications, the correspondent `{ee-all}-plus` module.

If you have already enabled an xref:annotations/index.adoc[`annotations`] module suitable for your environment, an appropriate `plus` module will already have been enabled, and you do not need to explicitly enable a `plus` module.

If your JNDI resources need extra libraries, for example database driver libraries, that are not located inside your web application, then you must ensure they are on the environment class-path/module-path.

You can enable the xref:modules/standard.adoc#ext[`ext`] module for Jetty Core web applications, or the specific xref:modules/standard.adoc#eeN-ext[`{ee-all}-ext`] module for Jakarta EE web applications, and place your extra libraries respectively in `$JETTY_BASE/lib/ext` or `$JETTY_BASE/lib/{ee-all}/ext` to make them available in the environment class-path/module-path.

You can now define JNDI resources and reference them within your web applications.

== Defining resources

JNDI resources are referenced by web application via JNDI names.

// TODO: document how it is for Jetty web applications.

For Jakarta EE web applications, the JNDI names must be declared via `env-entry`, `resource-ref` and `resource-env-refs` in `web.xml`, `web-fragment.xml` or Jetty's `override-web.xml`.

The resources are bound to their JNDI names in Jetty XML files that are either _external_  or _internal_ to your web application.

External Jetty XML files are those used to deploy the web application, as defined in xref:deploy/index.adoc[this section].

Internal Jetty XML files are those present inside the web application directory or packaged file, as defined in xref:deploy/index.adoc#jetty-web-xml[this section].

For Jakarta EE web applications, the file `WEB-INF/jetty-{ee-all}-env.xml` is processed _before_ other internal Jetty XML files such as `WEB-INF/jetty-{ee-all}-web.xml`, so that JNDI resources can be bound and later possibly processed by `WEB-INF/jetty-{ee-all}-web.xml`.

See the section on <<xml,Jetty XML files>> for more information on how to choose in which XML file to place the definitions of your JNDI resources.

The definitions of your JNDI resources are instances of the following types:

<<env,`org.eclipse.jetty.plus.jndi.EnvEntry`>>::
Used for `env-entry` resources in `web.xml`.
<<resource,`org.eclipse.jetty.plus.jndi.Resource`>>::
Used for other type of resources.
<<tx,`org.eclipse.jetty.plus.jndi.Transaction`>>::
<<tx,`org.eclipse.jetty.{ee-all}.plus.jndi.Transaction`>>::
Used for a JTA transaction manager.
<<link,`org.eclipse.jetty.plus.jndi.Link`>>::
Used for linking between a `web.xml` resource name and a naming entry.

Definitions of each of these types follow this pattern:

[,xml,subs=attributes+]
----
<New class="org.eclipse.jetty.plus.jndi.<resource-class>"> 
  <Arg></Arg> 
  <Arg></Arg>  
  <Arg></Arg> 
</New>
----
<1> Defines a resource of the given class to Jetty.
<2> Specifies the <<scope,scope>> of the resource.
<3> Specifies the name of the resource which will be looked up by the web application relative to the `java:comp/` or `java:comp/env` namespace.
<4> Specifies the value of the resource.

[[env]]
=== org.eclipse.jetty.plus.jndi.EnvEntry

Sometimes it is useful to pass configuration information to a web application at runtime that you either cannot or cannot conveniently code into a `web.xml` `<env-entry>`.
In such cases, you can use the `org.eclipse.jetty.plus.jndi.EnvEntry` class, and optionally even override an entry of the same name in `web.xml`.

Here's an example that defines the equivalent of an `env-entry` called `mySpecialValue` with value `4000` that overrides an `<env-entry>` declaration of the same name in `web.xml`:

[,xml,subs=attributes+]
----
<New class="org.eclipse.jetty.plus.jndi.EnvEntry"> 
  <Arg></Arg> 
  <Arg>mySpecialValue</Arg> 
  <Arg type="java.lang.Integer">4000</Arg> 
  <Arg type="boolean">true</Arg> 
</New>
----
<1> Defines an `EnvEntry` that corresponds to an `<env-entry>` in `web.xml`.
<2> <<scope,Scoped>> at the JVM level.
<3> The name of the entry, corresponding to a lookup by the web application of `java:comp/env/mySpecialValue`.
<4> The value of the entry, in this case the integer value `4000`.
<5> `true` means to override the value of an `<env-entry>` of the same name in `web.xml`.

Note that if you don't want to override the `web.xml` value, simply omit the last argument, or set it to `false`.

The Servlet specification allows binding only the following object types to an `env-entry`:

* java.lang.String
* java.lang.Integer
* java.lang.Float
* java.lang.Double
* java.lang.Long
* java.lang.Short
* java.lang.Character
* java.lang.Byte
* java.lang.Boolean

Jetty is a little more flexible and allows you to also bind:

* Instances of any object.
* `javax.naming.Reference` instances.
* `javax.naming.Referenceable` instances.

Be aware that if you take advantage of this flexibility, your web application is __not portable__ across Servlet containers.

[[resource]]
=== org.eclipse.jetty.plus.jndi.Resource

You can configure any type of resource that you want to refer to in `web.xml` via a `resource-ref` or `resource-env-ref` by using the `org.eclipse.jetty.plus.jndi.Resource` type of naming entry.

You provide the scope, the name of the object (relative to `java:comp/env`) and either the resource, or a `javax.naming.Reference` or a `javax.naming.Referenceable` instance.

==== DataSources

This example configures a http://db.apache.org/derby[Derby] DataSource named `jdbc/myds`:

[,xml,subs=attributes+]
----
<Configure id="wac" class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext">
  <New class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg><Ref refid="wac"/></Arg>
    <Arg>jdbc/myds</Arg>
    <Arg>
      <New class="org.apache.derby.jdbc.EmbeddedDataSource">
        <Set name="DatabaseName">test</Set>
        <Set name="createDatabase">create</Set>
      </New>
    </Arg>
  </New>
</Configure>
----

This would be linked into the web application's JNDI namespace via an entry in a `web.xml` like so:

[,xml]
----
<resource-ref>
  <res-ref-name>jdbc/myds</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
</resource-ref>
----

[NOTE]
====
When configuring ``Resource``s, ensure that the type of object you configure matches the type of object you expect to look up from `java:comp/env`.
For database connection factories, this means that the object you register as a `Resource` _must_ implement the `javax.sql.DataSource` interface.

Also note that the Jakarta specification recommends storing ``DataSource``s relative to `jdbc/` and thus looked up by the web application as `java:comp/env/jdbc/<datasource-name>`.

For example, the `Datasource` bound in Jetty as `jdbc/users` would be looked up by the web application as `java:comp/env/jdbc/users`.
====

//TODO For more examples of datasource configurations, see xref:jndi-datasource-examples[].

==== JMS Queues, Topics and ConnectionFactories

Jetty can bind any implementation of the JMS destinations and connection factories.

Here is an example of binding an http://activemq.apache.org[ActiveMQ] in-JVM connection factory:

[,xml,subs=attributes+]
----
<Configure id="wac" class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext">
  <New class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg><Ref refid="wac"/></Arg>
    <Arg>jms/connectionFactory</Arg>
    <Arg>
      <New class="org.apache.activemq.ActiveMQConnectionFactory">
        <Arg>vm://localhost?broker.persistent=false</Arg>
      </New>
    </Arg>
  </New>
</Configure>
----

The corresponding entry in `web.xml` to bind the ConnectionFactory into the web application's JNDI namespace would be:

[,xml,subs=attributes+]
----
<resource-ref>
  <res-ref-name>jms/connectionFactory</res-ref-name>
  <res-type>javax.jms.ConnectionFactory</res-type>
  <res-auth>Container</res-auth>
</resource-ref>
----

[NOTE]
====
The Jakarta specification recommends storing JMS connection factories under `jms/`.

For example, the `ConnectionFactory` bound in Jetty as `jms/inqueue` would be looked up by the application as `java:comp/env/jms/inqueue`.
====

==== Mail

To configure access to `jakarta.mail.Session` from within a web application, define an `org.eclipse.jetty.plus.jndi.Resource` with an `org.eclipse.jetty.{ee-all}.jndi.factories.MailSessionReference` that will hold the mail configuration and create the instance of the `Session` when it is referenced:

[,xml,subs=attributes+]
----
<Configure id="wac" class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext">
  <New class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg><Ref refid="wac"/></Arg>
    <Arg>mail/Session</Arg>
    <Arg>
      <New class="org.eclipse.jetty.{ee-current}.jndi.factories.MailSessionReference"> 
        <Set name="user">fred</Set> 
        <Set name="password">OBF:1xmk1w261z0f1w1c1xmq</Set> 
        <Set name="properties"> 
          <New class="java.util.Properties">
            <Put name="mail.smtp.host">XXX</Put>
            <Put name="mail.from">me@me</Put>
            <Put name="mail.debug">true</Put>
          </New>
        </Set>
      </New>
    </Arg>
  </New>
</Configure>
----
<1> Use the `org.eclipse.jetty.{ee-current}.factories.MailSessionReference` class to hold the configuration.
<2> Set the username for the mail instance.
<3> Set the password for the mail instance -- use the xref:tools/index.adoc#password[Jetty Password tool] to obfuscate the password.
<4> Set all other applicable properties.

The web application performs a lookup for `java:comp/env/mail/Session` at runtime and obtains a `jakarta.mail.Session` that has the correct configuration to permit it to send email via SMTP.

[NOTE]
====
Note also that the Jakarta specification recommends storing Jakarta Mail connection factories under `mail/`.

For example, the `MailSessionReference` bound to jetty as `mail/smtp` would be looked up by the web application as `java:comp/env/mail/smtp`.
====

IMPORTANT: Jetty does not provide the `jakarta.mail` and `jakarta.activation` libraries.

[[tx]]
=== org.eclipse.jetty.plus.jndi.Transaction

To perform distributed transactions with your resources, a _transaction manager_ that supports the link:https://jakarta.ee/specifications/transactions/[Jakarta Transaction] (JTA) interfaces is required.
The transaction manager is looked up by the application as `java:comp/UserTransaction`.

Jetty does not ship with a transaction manager, but provides the infrastructure to plug in the transaction manager of your choice.

If the JTA implementation of `jakarta.transaction.UserTransaction` implements `javax.naming.Reference`, then you should use the `org.eclipse.jetty.plus.jndi.Transaction` object in a <<xml,Jetty XML file>> to register it in JNDI:

[,xml,subs=attributes+]
----
<New id="tx" class="org.eclipse.jetty.plus.jndi.Transaction">
  <Arg><Property name="environment" default="{ee-current}"/></Arg> 
  <Arg>
    <New class="org.acme.jta.MyUserTransactionReference" /> 
  </Arg>
</New>
----
<1> The <<scope,scope>> of the resource.
<2> The transaction manager instance implementing `jakarta.transaction.UserTransaction` and `javax.naming.Reference`.

If your JTA implementation of `UserTransaction` does _not_ implement `javax.naming.Reference`, then you should use the Jakarta specific Jetty class to register it in JNDI:

[,xml,subs=attributes+]
----
<New id="tx" class="org.eclipse.jetty.{ee-current}.plus.jndi.Transaction">
  <Arg><Property name="environment" default="{ee-current}"/></Arg> 
  <Arg>
    <New class="org.acme.jta.MyUserTransaction" /> 
  </Arg>
</New>
----
<1> The <<scope,scope>> of the resource.
<2> The transaction manager instance implementing `jakarta.transaction.UserTransaction`.

Jetty will automatically bind this transaction manager to the web application's JNDI namespace at `java:comp/UserTransaction`.

[[link]]
=== org.eclipse.jetty.plus.jndi.Link

Usually, the name you provide for the `org.eclipse.jetty.plus.jndi.Resource` is the same name you reference in `web.xml`.
This ensures that the two are linked together and thus accessible to your web application.

However, if the names cannot be the same, then it is possible to effectively alias one to another using an `org.eclipse.jetty.plus.jndi.Link`.

Suppose you have a definition for a `Datasource` named `jdbc/workforce` in a Jetty context XML file, but your `web.xml` wants to link to a `<resource-ref>` named `jdbc/employees`, and you cannot edit the `web.xml`.
You can create a `WEB-INF/jetty-{ee-all}-env.xml` file with an `org.eclipse.jetty.plus.jndi.Link` that ties together the names `jdbc/workforce` and `jdbc/employees`:

The Jetty Context XML file defines `jdbc/workforce`:

[,xml,subs=attributes+]
----
<Configure id="wac" class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext">
  <New class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg><Ref refid="wac"/></Arg>
    <Arg>jdbc/workforce</Arg>
    <Arg>
      <New class="org.apache.derby.jdbc.EmbeddedDataSource">
        <Set name="DatabaseName">test</Set>
        <Set name="createDatabase">create</Set>
      </New>
    </Arg>
  </New>
</Configure>
----

The `web.xml` refers to it as `jdbc/employees`:

[,xml,subs=attributes+]
----
<resource-ref>
  <res-ref-name>jdbc/employees</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
</resource-ref>
----

Create a `WEB-INF/jetty-{ee-all}-env.xml` file with a `org.eclipse.jetty.plus.jndi.Link` to link these names together:

[,xml,subs=attributes+]
----
<New class="org.eclipse.jetty.plus.jndi.Link">
  <Arg><Ref refid="wac"/></Arg>
  <Arg>jdbc/employees</Arg> 
  <Arg>jdbc/workforce</Arg> 
</New>
----
<1> The name as referenced in the `web.xml` file.
<2> The name as referenced in the Jetty Context XML file.

[[xml]]
=== Jetty XML files

You can define naming resources in three places:

Server XML file::
Resources defined in a server XML file are <<scope,scoped>> at the JVM, `org.eclipse.jetty.server.Server` or xref:deploy/index.adoc[environment] level.
Note that the classes for the resource _must_ be accessible at the time that the XML is processed.
+
For example, environment resources that depend on an external library should be defined in an XML file that is processed after the environment class-path has been properly configured.
This can be achieved by using the `{ee-all}-ext` module, with the external library in the `$JETTY_BASE/lib/{ee-all}/ext/` directory, or with a xref:modules/custom.adoc[custom module] that contains an `[environment]` clause and a `[lib]` clause.

Context XML file::
Resources defined in a context XML file should be <<scope,scoped>> at the level of the web application to which they apply (it is possible to use a less strict scoping level of Server or JVM, but not recommended).
As the Context XML file executes _before_ the web application classes are available, the classes for your resource must be external to the web application, either on the container or on environment class-path.

WEB-INF/jetty-{ee-all}-env.xml::
Resources defined in a `WEB-INF/jetty-{ee-all}-env.xml` file are <<scope,scoped>> to the web application in which the file resides.
The resources defined by this file may use classes from inside your web application.

WEB-INF/jetty-{ee-all}-web.xml::
Resources defined in a `WEB-INF/jetty-{ee-all}-web.xml` file are <<scope,scoped>> to the web application in which the file resides.
The resources defined by this file may use classes from inside your web application.
This file is processed _after_ `WEB-INF/jetty-{ee-all}-env.xml`.

[[scope]]
=== Resource scoping

Resources within Jetty are defined in different scopes, in increasing order of restrictiveness:

JVM scope::
The resource is unique across the JVM instance, and is visible to all application code.
This scope is represented by a `null` first parameter to the resource definition.
For example:
+
[,xml,subs=attributes+]
----
<New id="cf" class="org.eclipse.jetty.plus.jndi.Resource">
  <Arg></Arg>  
  <Arg>jms/connectionFactory</Arg>
  <Arg>
    <New class="org.apache.activemq.ActiveMQConnectionFactory">
       <Arg>vm://localhost?broker.persistent=false</Arg>
    </New>
  </Arg>
</New>
----
<1> The scope is `null` or the empty string to indicate the JVM scope for the resource.

Environment scope::
The resource is unique within a Jetty _environment_.
The scope is represented by referencing the name of the environment as the first parameter to the resource definition.
For example:
+
[,xml,subs=attributes+]
----
<New id="cf" class="org.eclipse.jetty.plus.jndi.Resource">
  <Arg>{ee-current}</Arg>  
  <Arg>jms/connectionFactory</Arg>
  <Arg>
    <New class="org.apache.activemq.ActiveMQConnectionFactory">
      <Arg>vm://localhost?broker.persistent=false</Arg>
    </New>
  </Arg>
</New>
----
<1> The scope is the name of the environment to indicate the environment scope for the resource.

Web application scope::
The resource is unique to the `org.eclipse.jetty.{ee-all}.webapp.WebAppContext` instance, and is only visible to that application.
The scope is represented by referencing the context instance as the first parameter to the resource definition.
For example:
+
[,xml,subs=attributes+]
----
<New class="org.eclipse.jetty.plus.jndi.Resource">
  <Arg><Ref refid="wac"/></Arg> 
  <Arg>jms/connectionFactory</Arg>
  <Arg>
    <New class="org.apache.activemq.ActiveMQConnectionFactory">
      <Arg>vm://localhost?broker.persistent=false</Arg>
    </New>
  </Arg>
</New>
----
<1> The scope is the `org.eclipse.jetty.{ee-all}.webapp.WebAppContext` instance, to indicate the web application scope for the resource.

---

### Documentation Jetty Modules Operations Guide Pages Jsf Taglibs Index (`documentation/jetty/modules/operations-guide/pages/jsf-taglibs/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Jakarta Faces TagLibs

If you want to use Jakarta Faces with your Jakarta EE web application, you should enable the xref:modules/standard.adoc#eeN-ext[`{ee-all}-ext`] Jetty module, and copy the relevant Jakarta Faces API jar files, and the Jakarta Faces implementation jar files of your choice into the `$JETTY_BASE/lib/{ee-all}/ext` directory created by the `{ee-all}-ext` module you enabled.

Then you will need to tell Jetty which of those jar files contains the `+*.tld+` files.
To accomplish that, you need to specify either the name of the file or a pattern that matches the name of the file(s) as the `org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern` context attribute.
You will need to preserve the existing value of the attribute, and add in your extra pattern.

Below you can find an example of using a Jetty context XML file to add a pattern to match files starting with `jsf-`, which contain the `+*.tld+` files:

[,xml,subs=+attributes]
----
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext"> 
  <Call name="setAttribute"> 
    <Arg>org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern</Arg> 
    <Arg>.*/jetty-servlet-api-[^/]*\.jar$|.*/javax.servlet.jsp.jstl-.*\.jar$|.*/org.apache.taglibs.taglibs-standard-impl-.*\.jar$|.*/jsf-[^/]*\.jar$</Arg> 
  </Call>
</Configure>
----
<1> Configures a link:{javadoc-url}/org/eclipse/jetty/{ee-current}/webapp/WebAppContext.html[`WebAppContext`], which is the Jetty component that represents a Jakarta EE web application.
<2> Sets a context attribute.
<3> Specifies the name of the context attribute.
<4> Specifies the value of the context attribute, adding the additional pattern `+.*/jsf-[^/]*\.jar$+` to those already existing.

---

### Documentation Jetty Modules Operations Guide Pages Jsp Index (`documentation/jetty/modules/operations-guide/pages/jsp/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Java Server Pages

Jetty supports JSP via the `{ee-all}-jsp` modules, which are based on Apache Jasper:

[source,subs=attributes+]
----
include::{jetty-home}/modules/ee11-jsp.mod[]
----

Logging has been bridged to Jetty logging, so you can enable logging for the `org.apache.jasper` package, subpackages and classes as usual.

== Configuration of the JSP Servlet

The `org.eclipse.jetty.jsp.JettyJspServlet` is the servlet responsible for serving JSPs.

It is configured as the default jsp servlet in the `$JETTY_HOME/etc/webdefault.xml` file.
Notice that Jetty identifies the jsp servlet by the presence of the `id="jsp"` attribute in the `<servlet>` declaration.

That file maps the `org.eclipse.jetty.jsp.JettyJspServlet` to the following partial urls:

* `+*.jsp+`
* `+*.jspf+`
* `+*.jspx+`
* `+*.xsp+`
* `+*.JSP+`
* `+*.JSPF+`
* `+*.JSPX+`
* `+*.XSP+`

You can change to a different servlet, change or add ``<init-param>``s or add extra ``<servlet-mapping>``s in your `web.xml` file.

Here's an example of adding an `<init-param>` to augment the definitions from the standard `webdefault.xml` file:

[,xml]
----
<servlet id="jsp">  
  <servlet-name>jsp</servlet-name> 
  <init-param>
    <param-name>keepgenerated</param-name> 
    <param-value>true</param-value>  
  </init-param>
</servlet>
----
<1> This identifies this servlet as the jsp servlet to Jetty.
<2> This identifies this declaration as augmenting the already-defined servlet called `jsp`.
<3> This init param controls whether the jsp servlet retains the `+*.java+` files generated during jsp compilation.
<4> This sets the value of the init param

Another element you might consider adding to the default setup is `async-supported`:

[,xml]
----
<servlet id="jsp">  
  <servlet-name>jsp</servlet-name>  
  <async-supported>true</async-supported>  
</servlet>
----
<1> This identifies this servlet as the jsp servlet to Jetty.
<2> This identifies this declaration as augmenting the already-defined servlet called `jsp`.
<3> By default, the jsp servlet does not support async.

There are many configuration parameters for the Apache Jasper JSP Servlet, here are some of them:

.JSP Servlet Parameters
[cols=",,,",options="header"]
|===
|init param |Description |Default |`webdefault.xml`

|checkInterval |If non-zero and `development` is `false`, background jsp recompilation is enabled. This value is the interval in seconds between background recompile checks.
               |0 |–
|classpath |The classpath is dynamically generated if the context has a URL classloader. The `org.apache.catalina.jsp_classpath`
context attribute is used to add to the classpath, but if this is not set, this `classpath` configuration item is added to the classpath instead.` |- |–

|classdebuginfo |Include debugging info in class file. |true |–

|compilerClassName |If not set, defaults to the Eclipse jdt compiler. |- |–

|compiler |Used if the Eclipse jdt compiler cannot be found on the
classpath. It is the classname of a compiler that Ant should invoke. |–
|–

|compilerTargetVM |Target vm to compile for. |1.8 |1.8

|compilerSourceVM |Sets source compliance level for the jdt compiler.
|1.8 |1.8

|development |If `true` recompilation checks occur at the frequency governed by `modificationTestInterval`. |true |–

|displaySourceFragment |Should a source fragment be included in
exception messages |true |–

|dumpSmap |Dump SMAP JSR45 info to a file. |false |–

|enablePooling |Determines whether tag handler pooling is enabled. |true |–

|engineOptionsClass |Allows specifying the Options class used to
configure Jasper. If not present, the default EmbeddedServletOptions
will be used. |- |–

|errorOnUseBeanInvalidClassAttribute |Should Jasper issue an error when
the value of the class attribute in an useBean action is not a valid
bean class |true |–

|fork |Only relevant if you use Ant to compile JSPs: by default Jetty will use the Eclipse jdt compiler.|true |-

|genStrAsCharArray |Option for generating Strings as char arrays. |false |–

|ieClassId |The class-id value to be sent to Internet Explorer when
using <jsp:plugin> tags. |clsid:8AD9C840-044E-11D1-B3E9-00805F499D93 |–

|javaEncoding |Pass through the encoding to use for the compilation.
|UTF8 |–

|jspIdleTimeout |The amount of time in seconds a JSP can be idle before
it is unloaded. A value of zero or less indicates never unload. |-1 |–

|keepgenerated |Do you want to keep the generated Java files around?
|true |–

|mappedFile |Support for mapped Files. Generates a servlet that has a
print statement per line of the JSP file |true |–

|maxLoadedJsps |The maximum number of JSPs that will be loaded for a web
application. If more than this number of JSPs are loaded, the least
recently used JSPs will be unloaded so that the number of JSPs loaded at
any one time does not exceed this limit. A value of zero or less
indicates no limit. |-1 |–

|modificationTestInterval |If `development=true`, interval between
recompilation checks, triggered by a request. |4 |–

|quoteAttributeEL | When EL is used in an attribute value on a JSP page, should the rules for quoting of attributes described in JSP.1.6 be applied to the expression
                 |true |-

|recompileOnFail |If a JSP compilation fails should the
modificationTestInterval be ignored and the next access trigger a
re-compilation attempt? Used in development mode only and is disabled by
default as compilation may be expensive and could lead to excessive
resource usage. |false |–

|scratchDir |Directory where servlets are generated. The default is the value of the context attribute `javax.servlet.context.tempdir`, or the system property `java.io.tmpdir` if the context attribute is not set. |– |–

|strictQuoteEscaping |Should the quote escaping required by section JSP.1.6 of the JSP specification be applied to scriplet expression.
             |true|-

|suppressSmap |Generation of SMAP info for JSR45 debugging. |false |–

|trimSpaces |Should template text that consists entirely of whitespace be removed from the output (true), replaced with a single space (single) or left unchanged (false)? Note that if a JSP page or tag file specifies a trimDirectiveWhitespaces value of true, that will take precedence over this configuration setting for that page/tag.
trimmed? |false |–

|xpoweredBy |Generate an X-Powered-By response header. |false |false

|===

[NOTE]
====
If the value you set doesn't take effect, try using all lower case instead of camel case, or capitalizing only some of the words in the name, as Jasper is inconsistent in its parameter naming strategy.
====

---

### Documentation Jetty Modules Operations Guide Pages Jstl Index (`documentation/jetty/modules/operations-guide/pages/jstl/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= JavaServer Pages Standard Tag Libraries

The JavaServer Pages Standard Tag Library (JSTL) is part of the Jetty distribution, and is available via the `{ee-all}-jstl` modules:

[source,subs=attributes+]
----
include::{jetty-home}/modules/ee11-jstl.mod[]
----

When enabled, Jetty will make the JSTL tags available for your web applications.

---

### Documentation Jetty Modules Operations Guide Pages Keystore Index (`documentation/jetty/modules/operations-guide/pages/keystore/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Configuring SSL/TLS KeyStores

A KeyStore is a file on the file system that contains a private key and a public certificate, along with the certificate chain of the certificate authorities that issued the certificate.
The private key, the public certificate and the certificate chain, but more generally the items present in a KeyStore, are typically referred to as "cryptographic material".

Keystores may encode the cryptographic material with different encodings, the most common being https://en.wikipedia.org/wiki/PKCS_12[PKCS12], and are typically protected by a password.

Refer to the xref:protocols/index.adoc#ssl[secure protocols section] for more information about how to configure a secure connector using a KeyStore.

[[create]]
== Creating a KeyStore

KeyStores are created with the JDK tool `$JAVA_HOME/bin/keytool`.

The following command creates a KeyStore file containing a private key and a self-signed certificate:

[source]
----
keytool
    -genkeypair <1>
    -alias mykey <2>
    -validity 90 <3>
    -keyalg RSA <4>
    -keysize 2048 <5>
    -keystore /path/to/keystore.p12 <6>
    -storetype pkcs12 <7>
    -dname "CN=domain.com, OU=Unit, O=Company, L=City, S=State, C=Country" <8>
    -ext san=dns:www.domain.com,dns:domain.org <9>
    -v <10>
----
<1> the command to generate a key and certificate pair
<2> the alias name of the key and certificate pair
<3> specifies the number of days after which the certificate expires
<4> the algorithm _must_ be RSA (the DSA algorithm does not work for web sites)
<5> indicates the strength of the key
<6> the KeyStore file
<7> the KeyStore type, stick with the standard PKCS12
<8> the distinguished name (more below) -- customize it with your values for CN, OU, O, L, S and C
<9> the extension with the subject alternative names (more below)
<10> verbose output

The command prompts for the KeyStore password that you must choose to protect the access to the KeyStore.

[IMPORTANT]
====
The important parts of the command above are the _Common Name_ (CN) part of the distinguished name, and the subject alternative names (SAN).

The CN value must be the main domain you own and that you want to use for your web applications.
For example, if you have bought domains `domain.com` and `domain.org`, you want to specify `CN=domain.com` as your main domain.

Furthermore, to specify additional domains or subdomains within the same certificate, you must specify the SAN extension.
In the example above, `san=dns:www.domain.com,dns:domain.org` specifies `www.domain.com` and `domain.org` as alternative names for your web applications (that you can configure using xref:deploy/index.adoc#virtual-hosts[virtual hosts]).

In rare cases, you may want to specify IP addresses, rather than domains, in the SAN extension.
The syntax in such case is `san=ip:127.0.0.1,ip:[::1]`, which specifies as subject alternative names IPv4 `127.0.0.1` and IPv6 `[::1]`.
====

[[create-many]]
=== KeyStores with Multiple Entries

A single KeyStore may contain multiple key/certificate pairs.
This is useful when you need to support multiple domains on the same Jetty server (typically accomplished using xref:deploy/index.adoc#virtual-hosts[virtual hosts]).

You can create multiple key/certificate pairs as detailed in the <<create,previous section>>, provided that you assign each one to a different alias.

Compliant TLS clients will send the xref:protocols/index.adoc#ssl-sni[TLS SNI extension] when creating new connections, and Jetty will automatically choose the right certificate by matching the SNI name sent by the client with the CN or SAN of certificates present in the KeyStore.

[[csr]]
== Creating a Certificate Signing Request

Self-signed certificates are not trusted by browsers and generic clients: you need to establish a trust chain by having your self-signed certificate signed by a certificate authority (CA).

Browsers and generic clients (e.g. Java clients) have an internal list of trusted certificate authorities root certificates; they use these trusted certificates to verify the certificate they received from the server when they connect to your web applications.

To have your self-signed certificate signed by a certificate authority you first need to produce a _certificate signing request_ (CSR):

[source]
----
keytool
    -certreq <1>
    -file domain.com.csr <2>
    -keystore keystore.p12 <3>
----
<1> the command to generate a certificate signing request
<2> the file to save the CSR
<3> the keystore that contains the self-signed certificate

Then, you have to send the CSR file to the certificate authority of your choice, and wait for their reply (they will probably require a proof that you really own the domains indicated in your certificate).

Eventually, the certificate authority will reply to you with one or more files containing the CA certificate chain, and your certificate signed by their certificate chain.

[[csr-import]]
== Importing the Signed Certificate

The file you receive from the CA is typically in PEM format, and you *must* import it back into the same KeyStore file you used to generate the CSR.
You must import *both* the certificate chain and your signed certificate.

First, import the certificate chain:

[source]
----
keytool
    -importcert <1>
    -alias ca <2>
    -file chain_from_ca.pem <3>
    -keystore keystore.p12 <4>
    -trustcacerts <5>
    -v <6>
----
<1> the command to import certificates
<2> use the `ca` alias to differentiate from the alias of the server certificate
<3> the file containing the certificate chain received from the CA
<4> your KeyStore file
<5> specify that you trust CA certificates
<6> verbose output

Then, import the signed certificate:

----
keytool
    -importcert
    -file signed_certificate.pem
    -keystore keystore.p12
    -trustcacerts
    -v
----

Now you have a trusted certificate in your KeyStore that you can use for the domains of your web applications.

// TODO: add a section about renewal?

Refer to the section about configuring xref:protocols/index.adoc#ssl[secure protocols] to configure the secure connector with your newly created KeyStore.

[[client-authn]]
== Creating a KeyStore for Client Certificate Authentication

For the majority of secure web sites, it is the client (typically the browser) that validates the certificate sent by the server (by verifying the certificate chain).
This is the _server domain certificate_.

However, the TLS protocol supports a _mutual authentication_ mode where also the client must send a certificate to the server, that the server validates.

You typically want to sign the client certificate(s) with a server certificate that you control, and you must distribute the client certificate(s) to all the clients that need it, and redistribute the client certificates when they expire.
The _server authentication certificate_ may be different from the _server domain certificate_, but it's typically stored in the same KeyStore for simplicity (although under a different alias).

First, you want to create the private key and server authentication certificate that you will use to sign client certificates:

[source]
----
keytool
    -genkeypair
    -alias server_authn <1>
    -validity 90
    -keyalg RSA
    -keysize 2048
    -keystore keystore.p12 <2>
    -storetype pkcs12
    -dname "CN=server_authn, OU=Unit, O=Company, L=City, S=State, C=Country" <3>
    -ext bc=ca:true <4>
    -v
----
<1> use the `server_authn` alias to differentiate from the alias of the server certificate
<2> the KeyStore file
<3> the CN is not that important, since this certificate will not be validated by clients
<4> the extension with the basic constraints (more below)

IMPORTANT: The important difference with the <<create,creation of a server certificate>> is the _basic constraints_ extension (`bc`) that indicates that this certificates acts as a certificate authority (`ca:true`).

Now you want to export both the private key and server authentication certificate.
Unfortunately, the `keytool` program cannot export private keys, so you need to use a different command line program like `openssl`, or a graphical program like https://keystore-explorer.org/[KeyStore Explorer].

Let's use `openssl` to export the server authentication private key:

----
openssl
    pkcs12
    -in keystore.p12
    -nodes
    -nocerts
    -out server_authn.key
----

Now let's export the server authentication certificate:

----
keytool
    -exportcert
    -keystore keystore.p12
    -rfc
    -file server_authn.crt
    -v
----

At this point, you want to create a client KeyStore, so that you can sign the client certificate with the server authentication cryptographic material:

[source]
----
keytool
    -genkeypair
    -validity 90
    -keyalg RSA
    -keysize 2048
    -keystore client_keystore.p12 <1>
    -storetype pkcs12
    -dname "CN=client, OU=Unit, O=Company, L=City, S=State, C=Country" <2>
    -v
----
<1> the client KeyStore file
<2> the CN is not that important, since it will not be validated by the server

Now produce a certificate signing request (CSR):

----
keytool
    -certreq
    -file client.csr
    -keystore client_keystore.p12
----

Now you need to sign the CSR, but again the `keytool` program does not have this functionality, and you must resort again to use `openssl`:

----
openssl
    x509
    -req
    -days 90
    -in client.csr
    -CA server_authn.crt
    -CAkey server_authn.key
    -CAcreateserial
    -sha256
    -out signed.crt
----

Now you need to import the server authentication certificate and the signed client certificate into the client KeyStore.

First, the server authentication certificate:

----
keytool
    -importcert
    -alias ca
    -file server_authn.crt
    -keystore client_keystore.p12
    -v
----

Then, the signed client certificate:

----
keytool
    -importcert
    -file signed.crt
    -keystore client_keystore.p12
    -v
----

Now you can distribute `client_keystore.p12` to your client(s).

// TODO: add a section about renewal?

Refer to the section about configuring xref:protocols/index.adoc#ssl[secure protocols] to configure the secure connector to require client authentication.

---

### Documentation Jetty Modules Operations Guide Pages Modules Custom (`documentation/jetty/modules/operations-guide/pages/modules/custom.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Custom Jetty Modules

In addition to the modules that come packaged with Jetty, you can create your own custom modules.

NOTE: Make sure you have read the xref:modules/index.adoc[Jetty modules section] if you are not familiar with the concepts used in this section.

Custom modules can be used for a number of reasons -- they can extend Jetty features, or add new features, or make additional libraries available to the server, etc.

[[modify]]
== Modifying an Existing Module

The standard Jetty modules typically come with a number of configurable properties that can be easily customized without the need of writing a custom module.

However, there may be cases where the customization is more complex than a simple property, and a custom module is necessary.

For example, let's assume that you want to modify the order of the TLS cipher suites offered by the server when a client connects, using the https://www.openssl.org/docs/man1.1.0/man1/ciphers.html[OpenSSL cipher list format].

The Jetty class that handles the TLS configuration is `SslContextFactory`, and it already has a method `setCipherComparator(Comparator<String>)`; however, you need to pass your custom implementation, which cannot be represented with a simple module property.

The `SslContextFactory` component is already allocated by the standard Jetty module `ssl`, so what you need to do is the following:

* Write the custom cipher `Comparator` and package it into a `+*.jar+` file (exercise left to reader).
* Write a custom Jetty XML file that calls the `SslContextFactory.setCipherComparator(Comparator<String>)` method.
* Write a custom Jetty module file that depends on the standard `ssl` module.

Start with the custom Jetty XML file, `$JETTY_BASE/etc/custom-ssl.xml`:

.custom-ssl.xml
[,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">
<Configure>
  <Ref refid="sslContextFactory"> 
    <Set name="CipherComparator"> 
      <New class="com.acme.ssl.CustomCipherComparator"> 
        <Arg>
          <Property name="com.acme.ssl.cipherList"> 
            <Default>ECDH+AESGCM:ECDH+AES256:!aNULL:!MD5:!DSS:!ADH</Default>
          </Property>
        </Arg>
      </New>
    </Set>
  </Ref>
</Configure>
----
<1> Reference the existing `SslContextFactory` object created by the standard `ssl` module using its `id`.
<2> Call the `setCipherComparator()` method.
<3> Instantiate your custom cipher comparator.
<4> Pass to the constructor the ordering string in OpenSSL format, reading it from the module property `com.acme.ssl.cipherList`.

CAUTION: The cipher list used above may not be secure -- it's just an example.

Then write your custom module in the `$JETTY_BASE/modules/custom-ssl.mod` file:

.custom-ssl.mod
[source]
----
[description]
Customizes the standard ssl module.

[tags] <1>
acme

[depends] <2>
ssl

[lib] <3>
lib/custom-cipher-comparator.jar

[xml] <4>
etc/custom-ssl.xml

[ini-template] <5>
## The cipher list in OpenSSL format.
# com.acme.ssl.cipherList=ECDH+AESGCM:ECDH+AES256:!aNULL:!MD5:!DSS:!ADH

----
<1> A tag that characterizes this custom module (see xref:modules/index.adoc#directive-tags[here]).
<2> This custom module depends on the standard `ssl` module.
<3> The custom cipher comparator class is compiled and packaged into this `+*.jar+` file.
<4> The custom Jetty XML file from above.
<5> The text that will be copied in the `custom-ssl.ini` file when this custom module will be enabled.

Now you can xref:start/index.adoc#configure-enable[enable] the custom module with the following command issued from the `$JETTY_BASE` directory:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=https,custom-ssl
----

The command above will produce the following `$JETTY_BASE` directory structure:

[source,subs=+quotes]
----
$JETTY_BASE
├── etc
│   └── custom-ssl.xml
├── modules
│   └── custom-ssl.mod
├── resources
│   └── jetty-logging.properties
└── start.d
    ├── https.ini
    └── ##custom-ssl.ini##
----

In the custom XML file you have used a custom module property to parametrize your custom cipher comparator.
This custom module property was then referenced in the `[ini-template]` section of the custom module file, so that when the custom module is enabled, a correspondent `custom-ssl.ini` file is created.

In this way, updating the cipher list won't require you to update the XML file, but just the `custom-ssl.ini` file.

[[create]]
== Creating a New Module

In the cases where you need to enhance Jetty with a custom functionality, you can write a new Jetty module that provides it.

For example, let's assume that you need to add a custom auditing component that integrates with the auditing tools used by your company.
This custom auditing component should measure the HTTP request processing times and record them (how they are recorded is irrelevant here -- could be in a local log file or sent via network to an external service).

The Jetty libraries already provide a way to measure HTTP request processing times via xref:programming-guide:server/http.adoc#handler-use-events[`EventsHandler`]: you write a custom `EventsHandler` subclass that overrides the methods corresponding to the events you are interested in.

The steps to create a Jetty module are similar to those necessary to <<modify,modify an existing module>>:

* Write the auditing component and package it into a `+*.jar+` file.
* Write a custom Jetty XML file that wires the auditing component to the `Handler` tree.
* Write a custom Jetty module file that puts everything together.

Let's start with the auditing component, sketched below:

[,java]
----
package com.acme.audit;

public class AuditingEventsHandler extends EventsHandler {
    // Auditing is implemented here.
}
----

Let's assume that this class is compiled and packaged into `acme-audit.jar`, and that it has a dependency on `acme-util.jar`.
Both `+*.jar+` files will be put in the `$JETTY_BASE/lib/` directory.

Next, let's write the Jetty XML file that wires the auditing component to the `ServerConnector`, `$JETTY_BASE/etc/acme-audit.xml`:

.acme-audit.xml
[,xml,options=nowrap]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">
<Configure>
  <Ref refid="Server"> 
    <Call name="insertHandler"> 
      <Arg>
        <New class="com.acme.audit.AuditingEventsHandler"> 
          <Set name="someProperty">
            <Property name="com.acme.audit.some.property" default="42" /> 
          </Set>
        </New>
      </Arg>
    </Call>
  </Ref>
</Configure>
----
<1> Reference `Server` instance.
<2> Call `insertHandler()` on the `Server` so that the auditing component will be inserted just after the `Server` and just before its child `Handler`.
<3> Instantiate the auditing component.
<4> Configure the auditing component with a property.

The last step is to create the custom Jetty module file for the auditing component, `$JETTY_BASE/modules/acme-audit.mod`:

.acme-audit.mod
----
[description]
Adds ACME auditing to the Jetty Server.

[tags] <1>
acme
audit

[depends] <2>
server

[libs] <3>
lib/acme-audit.jar
lib/acme-util.jar

[xml] <4>
etc/acme-audit.xml

[ini-template] <5>
## An auditing property.
# com.acme.audit.some.property=42
----
<1> The tags that characterize this custom module (see xref:modules/index.adoc#directive-tags[here]).
<2> This custom module depends on the standard `server` module.
<3> The `+*.jar+` files that contains the custom auditing component, and its dependencies.
<4> The custom Jetty XML file from above.
<5> The text that will be copied in the `acme-audit.ini` file when this custom module will be enabled.

Now you can xref:start/index.adoc#configure-enable[enable] the custom auditing module with the following command issued from the `$JETTY_BASE` directory:

[source]
----
$ java -jar $JETTY_HOME/start.jar --add-modules=http,acme-audit
----

The command above will produce the following `$JETTY_BASE` directory structure:

[source,subs=+quotes]
----
$JETTY_BASE
├── etc
│   └── acme-audit.xml
├── modules
│   └── acme-audit.mod
├── resources
│   └── jetty-logging.properties
└── start.d
    ├── http.ini
    └── ##acme-audit.ini##
----

Enabling the custom auditing component will create the `$JETTY_BASE/start.d/acme-audit.ini` module configuration file that you can edit to configure auditing properties.

---

### Documentation Jetty Modules Operations Guide Pages Modules Index (`documentation/jetty/modules/operations-guide/pages/modules/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Jetty Modules

A Jetty _module_ provides one or more Java components that work together to implement one or more features.
Such features could be listening for clear-text HTTP/1.1 requests, exposing Jetty components to JMX, provide hot-deployment of web applications, etc.

Every Jetty feature is provided by a Jetty module.

A Jetty module is defined in a `<name>.mod` file, where `<name>` is the module name (see also the <<names,section about module names>>).

Jetty module files are read from the typical xref:start/index.adoc#configure[configuration source directories], under the `modules/` subdirectory; from higher priority to lower priority:

* The `$JETTY_BASE/modules/` directory.
* If a directory is specified with the `--add-config-dir` option, its `modules/` subdirectory.
* The `$JETTY_HOME/modules/` directory.

The standard Jetty modules that Jetty provides out-of-the-box are under `$JETTY_HOME/modules/`.

xref:modules/custom.adoc[Custom Jetty modules] should be put under `$JETTY_BASE/modules/`.

[[names]]
== Module Names

A Jetty module has a unique name.
The module name is by default derived from the file name, so module file `acme.mod` identifies a module named `acme`.

However, a module file may specify a <<directive-provides,+[provides]+>> directive for a _virtual_ module, so that many modules may provide a different implementation for the same feature.

For example, among the standard modules provided by Jetty, the `server` module depends on the `logging` module, but there is no correspondent `logging.mod` file.

However, the `logging-jetty.mod` file has, among others, this section:

.logging-jetty.mod
----
[provides]
logging|default
----

This section means that the `logging-jetty.mod` file provides the virtual module `logging`, and it is the default provider.

The `logging-log4j2.mod` file has a similar section:

.logging-log4j2.mod
----
[provides]
logging
----

If there are no enabled modules that provide the `logging` virtual module, either explicitly or transitively, then the default provider is used, in this case `logging-jetty.mod`.

Otherwise, a module that provides the `logging` virtual module is explicitly or transitively enabled, and the default provider is not used.

[[components]]
== Module Components

A Jetty module may provide one or more Java components that implement a feature.
These Java components are nothing more than regular Java classes that are instantiated and configured via xref:xml/index.adoc[Jetty XML] files.

The Jetty XML file of a Jetty module may instantiate and assemble together its own components, or reference existing components from other Jetty modules to enhance or reconfigure them.

The Jetty module's XML files are read from the typical xref:start/index.adoc#configure[configuration source directories], under the `etc/` subdirectory; from higher priority to lower priority:

* The `$JETTY_BASE/etc/` directory.
* If a directory is specified with the `--add-config-dir` option, its `etc/` subdirectory.
* The `$JETTY_HOME/etc/` directory.

The standard Jetty modules XML files that Jetty provides out-of-the-box are under `$JETTY_HOME/etc/`.

For example, a Jetty XML file that allocates Jetty's `QueuedThreadPool` could be as simple as:

[,xml]
.jetty-threadpool.xml
----
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">
<Configure>
  <New id="threadPool" class="org.eclipse.jetty.util.thread.QueuedThreadPool">
    <Set name="maxThreads" type="int">
      <Property name="jetty.threadPool.maxThreads" default="256"/>
    </Set>
  </New>
</Configure>
----

Note how the Jetty XML file above is allocating (with the `<New>` element) a `QueuedThreadPool` instance, giving it the unique `id` of `threadPool` (so that other modules can reference it, if they need to).
It is then calling the setter method `QueuedThreadPool.setMaxThreads(int)` with the value defined by the <<properties,module property>> `jetty.threadPool.maxThreads`; if the property value is not defined, it will have the default value of `256`.

This is nothing more than Java code in XML format with configurable properties support that can be leveraged by the xref:start/index.adoc[Jetty start mechanism].

The Jetty module's XML files make easy to instantiate and assemble Java components (just write the equivalent Java code in XML format), and make easy to configure them by declaring module properties that can be easily customized elsewhere (for example, in `+*.ini+` files as described in xref:start/index.adoc#configure-enable[this section], or on the command line as described in xref:start/index.adoc#start[this section]).

[IMPORTANT]
====
Remember that the standard Jetty XML files in `$JETTY_HOME/etc/` should not be modified.

Even if you need to modify a standard Jetty component, write a new Jetty XML file, save it under `$JETTY_BASE/etc/`, and create a xref:modules/custom.adoc[custom Jetty module] so that it gets processed when Jetty starts.
====

[[properties]]
== Module Properties

A Jetty module property is declared in the <<components,module XML file(s)>> via the `<Property>` element.
Modules properties are used to parametrize Jetty components so that you can customize their values when Jetty starts, rather than hard-coding it in the XML files.

NOTE: You can declare your own properties, but the `+jetty.*+` namespace is reserved.

A module property can be given a value in a Jetty module `[ini]` section (see <<directive-ini,here>>), in a `+*.ini+` file as described in xref:start/index.adoc#configure-enable[this section], or on the command line as described in xref:start/index.adoc#start[this section].

The syntax to specify a property value is the following:

<name>=<value>::
Sets the property value unconditionally.
<name>+=<value>::
Appends the value to the existing value.
This is useful to append a value to properties that accept a comma separated list of values, for example:
+
----
jetty.webapp.addProtectedClasses+=,com.acme.
----
+
// TODO: check what happens if the property is empty and +=,value is done: is the comma stripped? If so add a sentence about this.
<name>?=<value>::
Sets the property value only if it is not already set.
This is useful to define default values, for example for "version" properties, where the "version" property can be explicitly configured to a newer version, but if it is not explicitly configured it will have a default version (see also xref:start/index.adoc#configure-custom-module[here]).
For example:
+
----
conscrypt.version?=2.5.1
jetty.sslContext.provider?=Conscrypt
----

[[directives]]
== Module Directives

Lines that start with `#` are comments.

[[directive-description]]
=== [description]

A text that describes the module.

This text will be shown by the xref:start/index.adoc#configure[Jetty start mechanism] when using the `--list-modules` command.

[[directive-tags]]
=== [tags]

A list of words that characterize the module.

Modules that have the same tags will be shown by the Jetty start mechanism when using the `--list-modules=<tag>` command.

.example.mod
----
[tags]
demo
webapp
jsp
----

[[directive-provides]]
=== [provides]

A module name with an optional `default` specifier.

As explained in the <<names,module name section>>, there can be many module files each providing a different implementation for the same feature.

The format is:

----
[provides]
<module_name>[|default]
----

where the `|default` part is optional and specifies that the module is the default provider.

[[directive-depends]]
=== [depends]

A list of module names that this module depends on.

For example, the standard module `http` depends on module `server`.
Enabling the `http` module also enables, transitively, the `server` module, since the `http` module cannot work without the `server` module; when the `server` module is transitively enabled, the modules it depends on will be transitively enabled, and so on recursively.

The `[depends]` directive establishes a https://en.wikipedia.org/wiki/Partially_ordered_set[_partial order_] relationship among modules so that enabled modules can be sorted and organized in a graph.
Circular dependencies are not allowed.

The order of the enabled modules is used to determine the processing of the configuration, for example the order of processing of the <<directive-files,+[files]+>> section, the order of processing of XML files defined in the <<directive-xml,+[xml]+>> section, etc.

[[directive-after]]
=== [after]

This directive indicates that this module is ordered after the listed module names, if they are enabled.

For example, module `https` is `[after]` module `http2`.
Enabling the `https` module _does not_ enable the `http2` module.

However, if the `http2` module is enabled (explicitly or transitively), then the `https` module is <<directive-depends,sorted>> _after_ the `http2` module.
In this way, you are guaranteed that the `https` module is processed after the `http2` module.

[[directive-before]]
=== [before]

This directive indicates that this module is ordered before the listed module names, if they are enabled.

For example, module `test-keystore` is `[before]` module `ssl-context`.
Enabling the `test-keystore` module _does not_ enable the `ssl-context` module.

This directive is used to create a prerequisite module without the need to modify the `depends` directive of an existing module.

For example, you want to create a custom `org.eclipse.jetty.server.Server` subclass instance to be used by the standard `server` module, but without modifying the existing `server.mod` file nor the `jetty.xml` file that `server.mod` uses.

This can be achieved by creating the following `custom-server` xref:modules/custom.adoc[Jetty custom module]:

.custom-server.mod
----
[description]
This module creates a custom Server subclass instance.

[before]
server

[xml]
etc/custom-server.xml
----

The `custom-server.xml` file is the following:

.custom-server.xml
[,xml]
----
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">
<Configure id="Server" class="com.acme.server.CustomJettyServer">
</Configure>
----

The presence of the `[before]` directive in `custom-server.mod` causes the processing of the `custom-server.xml` file to happen before the processing of the standard `jetty.xml` file referenced by the standard `server.mod` Jetty module.

Thus, the instance assigned to the `Server` identifier is your custom `com.acme.server.CustomJettyServer` instance from the  `custom-server.xml` file; this instance is then used while processing the `jetty.xml` file.

[[directive-files]]
=== [files]

A list of paths (directories and/or files) that are necessary for the module, created or resolved when the module is enabled.

Each path may be of the following types:

Path Name::
A path name representing a file, or a directory if the path name ends with `/`, such as `webapps/`.
The file or directory will be created relative to `$JETTY_BASE`, if not already present.
+
For example:
+
----
[files]
logs/
----

Maven Artifact::
An URI representing a Maven artifact to be downloaded from Maven Central, if not already present.
Property expansion is supported.
+
The format is:
+
----
[files]
maven://<groupId>/<artifactId>/<version>[/<type>]|<pathName>
----
+
where `<type>` is optional, and `<pathName>` after the `|` is the path under `$JETTY_BASE` where the downloaded file should be saved.
+
For example:
+
[,options=nowrap]
----
[files]
maven://org.postgresql/postgresql/${postgresql-version}|lib/postgresql-${postgresql-version}.jar
----

BaseHome::
An URI representing a `$JETTY_HOME` resource to be copied in `$JETTY_BASE`, if not already present.
URIs of this type are typically only used by standard Jetty modules; custom modules should not need to use it.
+
The format is:
+
----
[files]
basehome:<jettyHomePathName>|<pathName>
----
+
For example:
+
----
[files]
basehome:modules/demo.d/demo-moved-context.xml|webapps/demo-moved-context.xml
----

HTTP URL::

An `http://` or `https://` URL to be downloaded, if not already present.
+
The format is:
+
----
[files]
<httpURL>|<pathName>
----
+
For example:
+
----
[files]
https://acme.com/favicon.ico|webapps/acme/favicon.ico
----

[[directive-libs]]
=== [libs]

A list of paths, relative to the xref:start/index.adoc#configure[configuration source directories], of `+*.jar+` library files and/or directories that are added to the server class-path (or module-path when xref:start/start-jpms.adoc[running in JPMS mode]).

The `[libs]` section if often used in conjunction with the `[files]` section.

For example:

----
[files]
maven://org.postgresql/postgresql/${postgresql-version}|lib/postgresql-${postgresql-version}.jar

[libs]
lib/postgresql-${postgresql-version}.jar
----

The `postgresql-<version>.jar` artifact is downloaded from Maven Central, if not already present, into the `$JETTY_BASE/lib/` directory when the module is enabled.

When Jetty starts, the `$JETTY_BASE/lib/postgresql-<version>.jar` will be in the server class-path (or module-path).

[[directive-xml]]
=== [xml]

A list of paths, relative to the xref:start/index.adoc#configure[configuration source directories], of Jetty `+*.xml+` files that are passed as program arguments to be processed when Jetty starts (see the xref:start/index.adoc#start-xml[section about assembling Jetty components]).

Jetty XML files are read from the typical xref:start/index.adoc#configure[configuration source directories], under the `etc/` subdirectory.
Standard Jetty XML files are under `$JETTY_HOME/etc/`, while custom Jetty XML files are typically under `$JETTY_BASE/etc/`.

For example:

----
[xml]
etc/custom/components.xml
----

[[directive-ini]]
=== [ini]

A list of program arguments to pass to the command line when Jetty is started.

The program arguments may include any command line option (see xref:start/index.adoc#reference[here] for the list of command line options), <<properties,module properties>> and/or <<components,module XML files>>.

A property defined in the `[ini]` section is available in the `+*.mod+` module file for property expansion, for example:

----
[ini]
postgresql-version?=42.6.0

[lib]
lib/postgresql-${postgresql-version}.jar
----

In the example above, the `[lib]` section contains `$\{postgresql-version}`, a reference to property `postgresql-version` whose value is defined in the `[ini]` section.
The expression `${<property>}` _expands_ the property replacing the expression with the property value.

See also the xref:start/start-jpms.adoc[JPMS section] for additional examples about the `[ini]` section.

[[directive-ini-template]]
=== [ini-template]

A list of properties to be copied in the `+*.ini+` file generated when xref:start/index.adoc#configure-enable[the module is enabled], either explicitly or transitively.

The list of properties is derived from the <<components,module XML file(s)>> that declare them.

The properties are typically assigned their default value and commented out, so that it is evident which properties have been uncommented and customized with a non-default value.

[[directive-exec]]
=== [exec]

A list of JVM command line options and/or system properties passed to a forked JVM.

When the `[exec]` section is present, the JVM running the Jetty start mechanism will fork another JVM, passing the JVM command line options and system properties listed in the `[exec]` sections of the enabled modules.

This is necessary because JVM options such as `-Xmx` (that specifies the max JVM heap size) cannot be changed in a running JVM.
For an example, see xref:start/index.adoc#configure-custom-module-exec[this section].

You can avoid that the Jetty start mechanism forks the second JVM, as explained in xref:start/index.adoc#configure-dry-run[this section].

[[directive-jpms]]
=== [jpms]

A list of JVM command line options related to the Java Module System.

This section is processed only when Jetty is xref:start/start-jpms.adoc[started in JPMS mode].

The directives are:

add-modules::
Equivalent to the JVM option `--add-modules`.
The format is:
+
----
[jpms]
add-modules: <module>(,<module>)*
----
+
where `module` is a JPMS module name.

patch-module::
Equivalent to the JVM option `--patch-module`.
The format is:
+
----
[jpms]
patch-module: <module>=<file>(:<file>)*
----
where `module` is a JPMS module name.

add-opens::
Equivalent to the JVM option `--add-opens`.
The format is:
+
----
[jpms]
add-opens: <module>/<package>=<target-module>(,<target-module>)*
----
where `module` and `target-module` are a JPMS module names.

add-exports::
Equivalent to the JVM option `--add-exports`.
The format is:
+
----
[jpms]
add-exports: <module>/<package>=<target-module>(,<target-module>)*
----
where `module` and `target-module` are a JPMS module names.

add-reads::
Equivalent to the JVM option `--add-exports`.
The format is:
+
----
[jpms]
add-reads: <module>=<target-module>(,<target-module>)*
----
where `module` and `target-module` are a JPMS module names.

[[directive-license]]
=== [license]

The license under which the module is released.

A Jetty module may be released under a license that is different from Jetty's, or use libraries that require end-users to accept their licenses in order to be used.

You can put the license text in the `[license]` section, and when the Jetty module is enabled the license text will be printed on the terminal, and the user prompted to accept the license.
If the user does not accept the license, the module will not be enabled.

For example:

----
[license]
Acme Project is an open source project hosted on GitHub
and released under the Apache 2.0 license.
https://www.apache.org/licenses/LICENSE-2.0.txt
----

[[directive-version]]
=== [version]

The minimum Jetty version for which this module is valid.

For example, a module may only be valid for Jetty 10 and later, but not for earlier Jetty versions (because it references components that have been introduced in Jetty 10).

For example:

----
[version]
10.0
----

A Jetty module with such a section will only work for Jetty 10.0.x or later.

---

### Documentation Jetty Modules Operations Guide Pages Modules Standard (`documentation/jetty/modules/operations-guide/pages/modules/standard.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Standard Modules

[[alpn]]
== Module `alpn`

The `alpn` module enables support for the ALPN negotiation mechanism of the TLS protocol.

You can configure the list of application protocols negotiated by the ALPN mechanism, as well as the default protocol to use if the ALPN negotiation fails (for example, the client does not support ALPN).

The module properties are:

----
include::{jetty-home}/modules/alpn.mod[tags=documentation]
----

[[accept-rate-limit]]
== Module `accept-rate-limit`

The `accept-rate-limit` module limits the rate of new connections accepted by the server, across all connectors.

Once the configured accept rate has been reached, Jetty will not accept more connections.
Existing, established connections will work normally.

Once accepting new connections has been suspended, Jetty will periodically check whether the rate has returned withing the limits, and if so, accepting new connections will be resumed.

This module is often used in conjunction with the xref:network-connection-limit[`network-connection-limit`] module.

NOTE: The number of connections seen at the JVM level may be different from the number of connections seen at the OS level.
For more information, refer to xref:programming-guide:server/http.adoc#connector-limiting[this section].

The module file is `$JETTY_HOME/modules/accept-rate-limit.mod`:

----
include::{jetty-home}/modules/accept-rate-limit.mod[tags=documentation]
----

[[bytebufferpool]]
== Module `bytebufferpool`

The `bytebufferpool` module allows you to configure the server-wide `ByteBuffer` pool.
Pooling ``ByteBuffer``s results in less memory usage and less pressure on the Garbage Collector.

``ByteBuffer``s are pooled in _buckets_; each bucket as a capacity that is a multiple of a capacity factor that you can configure.
For example, if a request for a `ByteBuffer` of capacity 2000 is requested, and the capacity factor is 1024, then the pool will allocate a buffer from the second bucket, of capacity 2048 (1024 * 2).

Applications that need to sustain many concurrent requests -- or load spikes -- may require many buffers during peak load. These buffers will remain pooled once the system transitions to a lighter load (or becomes idle), and it may be undesirable to retain a lot of memory for an idle system.

It is possible to configure the max heap memory and the max direct memory that the pool retains.
Excess buffers will not be pooled and will be eventually garbage collected.

The module file is `$JETTY_HOME/modules/bytebufferpool.mod`:

----
include::{jetty-home}/modules/bytebufferpool.mod[]
----

Among the configurable properties, the most relevant are:

`jetty.byteBufferPool.maxHeapMemory`::
This property allows you to cap the max heap memory retained by the pool.

`jetty.byteBufferPool.maxDirectMemory`::
This property allows you to cap the max direct memory retained by the pool.

[[compression]]
== Compression Modules

The compression modules allow you to configure server-wide request decompression and response compression by installing the `org.eclipse.jetty.compression.server.CompressionHandler` at the root of the `Handler` tree (see also xref:programming-guide:server/http.adoc#handler-use-compression[this section] for further details).

The supported algorithms are the following:

* brotli, via the <<compression-brotli,compression-brotli>> module
* gzip, via the <<compression-gzip,compression-gzip>> module
* zstandard, via the <<compression-zstandard,compression-zstandard>> module

You can explicitly enable one or more of the compression modules, or all of them with the <<compression-all,compression-all>> module.

[[compression-all]]
=== Module `compression-all`

This module enables request decompression and response compression for all the currently supported algorithms listed in <<compression,this section>>.

[[compression-brotli]]
=== Module `compression-brotli`

This module enables request decompression and response compression with the link:https://github.com/google/brotli[brotli] algorithm.

The module properties are:

----
include::{jetty-home}/modules/compression-brotli.mod[tags=documentation]
----

[[compression-gzip]]
=== Module `compression-gzip`

This module enables request decompression and response compression with the link:https://en.wikipedia.org/wiki/Gzip[gzip] algorithm.

The module properties are:

----
include::{jetty-home}/modules/compression-gzip.mod[tags=documentation]
----

[[compression-zstandard]]
=== Module `compression-zstandard`

This module enables request decompression and response compression with the link:https://github.com/facebook/zstd[zstdandard] algorithm.

The module properties are:

----
include::{jetty-home}/modules/compression-zstandard.mod[tags=documentation]
----

[[network-connection-limit]]
== Module `network-connection-limit`

The `network-connection-limit` module limits the number of network connections accepted by the server, across all connectors.

Once the configured maximum number of network connections is reached, Jetty will not accept more network connections.
Existing, established network connections will work normally.
When existing network connections are closed, accepting new network connections will be resumed.

This module is often used in conjunction with the xref:accept-rate-limit[`accept-rate-limit`] module.

NOTE: The number of network connections seen at the JVM level may be different from the number of network connections seen at the OS level.
For more information, refer to xref:programming-guide:server/http.adoc#connector-limiting[this section].

The module file is `$JETTY_HOME/modules/network-connection-limit.mod`:

----
include::{jetty-home}/modules/network-connection-limit.mod[tags=documentation]
----

[[console-capture]]
== Module `console-capture`

The `console-capture` module captures `System.out` and `System.err` output and appends it to a rolling file.

The file is rolled every day at the midnight of the configured timezone.
Old, rolled files are kept for the number of days specified by the `jetty.console-capture.retainDays` property.

The module properties are:

----
include::{jetty-home}/modules/console-capture.mod[tags=documentation]
----

[[core-deploy]]
== Module `core-deploy`

The `core-deploy` module enables deployment of web applications that only use the Jetty Core APIs, as described in xref:deploy/index.adoc[this section].

See also the <<static-deploy,`static-deploy`>> module for Jetty static deployment, and the <<eeN-deploy,`{ee-all}-deploy`>> modules for Jakarta EE deployment environments.

[[cross-origin]]
== Module `cross-origin`

The `cross-origin` module provides support for the https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS[CORS protocol] implemented by browsers when performing cross-origin requests.

This module installs the xref:programming-guide:server/http.adoc#handler-use-cross-origin[`CrossOriginHandler`] in the `Handler` tree; `CrossOriginHandler` inspects cross-origin requests and adds the relevant CORS response headers.

`CrossOriginHandler` should be used when an application performs cross-origin requests to your domain, to protect from https://owasp.org/www-community/attacks/csrf[cross-site request forgery] attacks.

The module properties are:

----
include::{jetty-home}/modules/cross-origin.mod[tags=documentation]
----

[[debuglog]]
== Module `debuglog`

The `debuglog` module logs extra information about request processing.

This module installs the `DebugHandler` into the handler tree and logs information into a rollover log file.
By default, the location of the log file is `$JETTY_BASE/logs`, although this can be configured via the `jetty.debuglog.dir`.
The name of the log file is of the form: `yyyy_mm_dd.debug.log`, for example `2024-10-01.debug.log`.
All deployed contexts will write to the same log file.

Note that if a request uses asynchronous processing, the log event for the completion of handling may occur out of order with the log event for the continuation of processing.

If more precise logging of request processing is required - including well-ordered async processing events - or you wish to constrain request debug logging to particular contexts only, then configure an ee-specific  `DebugListener` for each context instead of using this module.

The module properties are:

----
include::{jetty-home}/modules/debuglog.mod[tags=documentation]
----

[[deployment-scanner]]
== Module `deployment-scanner`

The `deployment-scanner` module installs the `org.eclipse.jetty.deploy.DeploymentScanner` component that watches web application deploy directories for changes, to deploy/redeploy/undeploy web applications that are present in those directories.

This module is a dependency for all the deploy modules such as <<static-deploy,`static-deploy`>>, <<core-deploy,`core-deploy`>> and <<eeN-deploy,`{ee-all}-deploy`.

There is only one `DeploymentScanner` for all the deploy modules that are enabled.

The module properties are:

----
include::{jetty-home}/modules/deployment-scanner.mod[tags=documentation]
----

Among the configurable properties, the most relevant are:

`jetty.deploy.webappsDir`::
The name of the directory, or a comma-separated list of directories, that is scanned to deploy web applications.
`jetty.deploy.scanInterval`::
The scan period in seconds, that is how frequently the `DeploymentScanner` wakes up to scan the monitored directories for changes.
By default, `jetty.deploy.scanInterval=0` so that _hot_ deployment is disabled and only _cold_ deployment is possible (see also xref:deploy/index.adoc#hot-cold[here] for more information).

[[dos]]
== Module `dos`

The `dos` module installs the `org.eclipse.jetty.server.handler.DoSHandler` at the root of the `Handler` tree.

The `DoSHandler` limits the rate of requests per remote client, as explained in xref:programming-guide:server/http.adoc#handler-use-dos[this section].

The module properties are:

----
include::{jetty-home}/modules/dos.mod[tags=documentation]
----

[[eager-content]]
== Module `eager-content`

The `eager-content` module installs the `org.eclipse.jetty.server.handler.EagerContentHandler` at the root of the `Handler` tree.

The `EagerContentHandler` can eagerly load request content, asynchronously, before calling the next `Handler`.
For more information see xref:programming-guide:server/http.adoc#handler-use-eager[this section].

`EagerContentHandler` can eagerly load content for form uploads, multipart uploads and any request content, and you can configure it with different properties for these three cases.

The module properties are:

----
include::{jetty-home}/modules/eager-content.mod[tags=documentation]
----

[[eeN-deploy]]
== Module `{ee-all}-deploy`

The `{ee-all}-deploy` modules enable deployment of corresponding Java/Jakarta EE web applications, as described in xref:deploy/index.adoc[this section].

Each `eeN-deploy` module supports the correspondent Java/Jakarta EE specification.

Multiple `eeN-deploy` modules may be enabled at the same time, allowing deployment of, for example, old Java EE 8 web applications alongside new Jakarta {ee-current-caps} web applications, in the same server.

The module configuration properties are nearly identical across the EE versions.
The module configuration properties for the `{ee-current}-deploy` Jetty module are:

[,properties,subs=+attributes]
----
include::{jetty-home}/modules/{ee-current}-deploy.mod[tags=ini-template]
----

See also the <<static-deploy,`static-deploy`>> module and the <<core-deploy,`core-deploy`>> module for Jetty specific deployment environments.

[[eeN-ext]]
== Module `{ee-all}-ext`

The `{ee-all}-ext` modules allow to add libraries to the environment class-path or module-path, for example to provide implementations of Jakarta APIs such as a Jakarta Mail implementation or Jakarta Transaction implementation.

The libraries must be in `+*.jar+` format, and must be placed in `$JETTY_BASE/lib/{ee-all}/ext`.

Refer also to the server specific <<ext,`ext`>> module.

[[eeN-proxy]]
== Module `{ee-all}-proxy`

The `{ee-all}-proxy` module adds a simple reverse proxy service that proxies incoming requests to a backend server.
Each module targets the corresponding Jakarta EE platform version.

The module properties are:

----
include::{jetty-home}/modules/{ee-current}-proxy.mod[tags=documentation]
----

Among the configurable properties, the only mandatory one is:

`jetty.proxy.proxyTo`::
The URI of the backend server, in absolute form, for example `+http://backend:9090/path+`.
+
The path in context of the incoming request is appended to the `jetty.proxy.proxyTo` property to obtain the URI to proxy to.
For example, for the incoming URI `+http://proxy:8080/ctx/foo/bar+`, the path in context (assuming the context path is `/ctx`) is `/foo/bar`, therefore the request is proxied to `+http://backend:9090/path/foo/bar+`.

[[eeN-webapp]]
== Module `{ee-all}-webapp`

The `{ee-all}-webapp` module adds support for Jakarta {ee-all-caps} web applications.

Multiple versions of this module exist (`{ee-all}-webapp`) to support each Jakarta EE platform's version of the Java Servlet specification.

The module configuration properties are identical across all versions of this module, and are as follows:

----
include::{jetty-home}/modules/{ee-current}-webapp.mod[tags=ini-template]
----

Configuration of _protected_ and _hidden_ classes, if necessary, is explained in xref:programming-guide:server/http.adoc#handler-use-webapp-context-class-loading[this section].
Refer also to the module properties syntax, detailed xref:modules/index.adoc#properties[here].

[[ext]]
== Module `ext`

The `ext` module allows to add libraries to the server class-path or module-path, for example for custom classes that extend Jetty classes, for server-wide logging libraries, for JDBC drivers, etc.

The libraries must be in `+*.jar+` format, and must be placed in `$JETTY_BASE/lib/ext`.

Refer also to the environment specific <<eeN-ext,`{ee-all}-ext`>> modules.

[[graceful]]
== Module `graceful`

The `graceful` module allows to shut down gracefully the Jetty server when it is stopped (see xref:start/index.adoc#stop[this section] for more information about stopping Jetty).

The `graceful` module installs the `GracefulHandler` at the root of the `Handler` tree; the `GracefulHandler` rejects new requests, but allows current requests to terminate within a configurable timeout, as explained in xref:programming-guide:server/http.adoc#handler-use-graceful[this section].

The module properties are:

----
include::{jetty-home}/modules/graceful.mod[tags=documentation]
----

[[http]]
== Module `http`

The `http` module provides the clear-text connector and support for the clear-text HTTP/1.1 protocol, and depends on the <<server,`server` module>>.

The module properties to configure the clear-text connector are:

----
include::{jetty-home}/modules/http.mod[tags=documentation]
----

Among the configurable properties, the most relevant are:

`jetty.http.port`::
The network port that Jetty listens to for clear-text HTTP/1.1 connections -- default `8080`.
`jetty.http.idleTimeout`::
The amount of time a connection can be idle (i.e. no bytes received and no bytes sent) until the server decides to close it to save resources -- default `30` seconds.
`jetty.http.acceptors`::
The number of threads that compete to accept connections -- default 1. Use -1 to let the accept heuristic decides the value; the current heuristic calculates a value based on the number of cores).
Refer to <<http-acceptors,this section>> for more information about acceptor threads.
`jetty.http.selectors`::
The number of NIO selectors (with an associated thread) that manage connections -- default -1 (i.e. a select heuristic decides the value; the current heuristic calculates a value based on the number of cores).

[[http-acceptors]]
=== Configuration of Acceptors

Accepting connections from remote clients may be configured as a blocking operation, or a non-blocking operation.

When accepting connections is configured as a blocking operation (the number of acceptors is greater than zero), a thread is blocked in the `accept()` call until a connection is accepted, and other acceptor threads (if any) are blocked on the lock acquired by the accepting thread just before the `accept()` call.

When the accepting thread accepts a connection, it performs a little processing of the just accepted connection, before forwarding it to other components.

During this little processing other connections may be established; if there is only one accepting thread, the newly established connections are waiting for the accepting thread to finish the processing of the previously accepted connection and call again `accept()`.

Servers that manage a very high number of connections that may (naturally) come and go, or that handle inefficient protocols that open and close connections very frequently (such as HTTP/1.0) may benefit of an increased number of acceptor threads, so that when one acceptor thread processes a just accepted connection, another acceptor thread can immediately take over accepting connections.

When accepting connections is configured as a non-blocking operation (the number of acceptors is zero), then the server socket is set in non-blocking mode and added to a NIO selector.
In this way, no dedicated acceptor threads exist: the work of accepting connections is performed by the selector thread.

[[http-selectors]]
=== Configuration of Selectors

Performing a NIO `select()` call is a blocking operation, where the selecting thread is blocked in the `select()` call until at least one connection is ready to be processed for an I/O operation.
There are 4 I/O operations: ready to be accepted, ready to be connected, ready to be read and ready to be written.

A single NIO selector can manage thousands of connections, with the assumption that not many of them will be ready at the same time.

For a single NIO selector, the ratio between the average number of selected connections over the total number of connections for every `select()` call depends heavily on the protocol but also on the application.

Multiplexed TCP protocols such as HTTP/2 tend to be busier than duplex protocols such as HTTP/1.1, leading to a higher ratio.

REST applications that exchange many little JSON messages tend to be busier than file server applications, leading to a higher ratio.

The higher the ratio, the higher the number of selectors you want to have, compatibly with the number of cores -- there is no point in having 64 selector threads on a single core hardware.

[[http2]]
== Module `http2`

The `http2` module enables support for the secure HTTP/2 protocol.

The module properties are:

----
include::{jetty-home}/modules/http2.mod[tags=documentation]
----

// tag::rate-control[]
The `jetty.http2.rateControl.maxEventsPerSecond` property controls the number of "bad" or "unnecessary" frames that a client may send before the server closes the connection (with code https://tools.ietf.org/html/rfc7540#section-7[`ENHANCE_YOUR_CALM`]) to avoid a denial of service.

For example, an attacker may send empty `SETTINGS` frames to a server in a tight loop.
While the `SETTINGS` frames don't change the server configuration and each of them is somehow harmless, the server will be very busy processing them because they are sent by the attacker one after the other, causing a CPU spike and eventually a denial of service (as all CPUs will be busy processing empty `SETTINGS` frames).

The same attack may be performed with `PRIORITY` frames, empty `DATA` frames, `PING` frames, etc.

[[http2c]]
== Module `http2c`

The `http2c` module enables support for the clear-text HTTP/2 protocol.

The module properties are:

----
include::{jetty-home}/modules/http2c.mod[tags=documentation]
----

The `jetty.http2.rateControl.maxEventsPerSecond` property controls the number of "bad" or "unnecessary" frames that a client may send before the server closes the connection (with code https://tools.ietf.org/html/rfc7540#section-7[`ENHANCE_YOUR_CALM`]) to avoid a denial of service.

For example, an attacker may send empty `SETTINGS` frames to a server in a tight loop.
While the `SETTINGS` frames don't change the server configuration and each of them is somehow harmless, the server will be very busy processing them because they are sent by the attacker one after the other, causing a CPU spike and eventually a denial of service (as all CPUs will be busy processing empty `SETTINGS` frames).

The same attack may be performed with `PRIORITY` frames, empty `DATA` frames, `PING` frames, etc.

[[http3]]
== Module `http3`

The `http3` module enables support for the HTTP/3 protocol.

Support for the QUIC protocol underlying HTTP/3 is provided by default via the `http3-quiche` module that uses the link:https://github.com/cloudflare/quiche[Quiche] native library.

The `http3-quiche` module properties are:

----
include::{jetty-home}/modules/http3-quiche.mod[tags=documentation]
----

[[http-config]]
== Module `http-config`

The `http-config` module provides HTTP configuration parameters that describe the HTTP semantic, and therefore do not depend on a specific HTTP protocol version.

This module creates an instance of `HttpConfiguration` for clear-text HTTP.
For secure HTTP, the <<ssl,`ssl`>> module creates a copy of the `HttpConfiguration` created by this module.

The module properties are:

----
include::{jetty-home}/modules/http-config.mod[tags=documentation]
----

Among the configurable properties, the most relevant are:

`jetty.httpConfig.requestHeaderSize`::
The maximum size of the request headers, beyond which the request is rejected with the HTTP status code `431 Request Header Fields Too Large`.

`jetty.httpConfig.headerCacheSize`::
The header cache is used when parsing HTTP/1 to more efficiently handle fields that are repeated in every request on a connection.  If the server does not receive persistent connection or infrequent repeated fields, then there may be a performance gain in reducing the cache size. If large fields are frequently repeated, then a large cache may be beneficial.

`jetty.httpConfig.outputAggregationSize`::
The size of the buffer that aggregates response writes that are not explicitly flushed.
This is used only at the Servlet layer, to buffer writes performed with `ServletOutputStream`.

`jetty.httpConfig.sendServerVersion`::
Whether you want to send the `Server` header in every HTTP response:
+
[source,subs=attributes+]
----
HTTP/1.1 200 OK
Content-Length: 0
Server: Jetty({jetty-version})
----

=== Server Compliance Properties

The Jetty server strives to keep up with the latest https://en.wikipedia.org/wiki/Request_for_Comments[IETF RFC]s for compliance with internet specifications, which are periodically updated.

When possible, Jetty will support backwards compatibility by providing compliance modes that can be configured to allow violations of the current specifications that may have been allowed in obsoleted specifications.

The module properties to configure the Jetty server compliance are:

----
include::{jetty-home}/modules/http-config.mod[tags=documentation-compliance]
----

Among the configurable properties, the most relevant are:

`jetty.httpConfig.compliance`::
Configures the compliance to HTTP specifications, see also xref:programming-guide:server/compliance.adoc#http[this section].
+
The value could be: one of the predefined link:{javadoc-url}/org/eclipse/jetty/http/HttpCompliance.html[`HttpCompliance`] constants, such as `RFC9110`, `RFC7230` or `RFC2616`; or a comma-separated list of violations to allow or forbid, as specified by the link:{javadoc-url}/org/eclipse/jetty/http/HttpCompliance.html#from(java.lang.String)[`HttpCompliance.from(String)`] method.
+
For example, `jetty.httpConfig.compliance=RFC9110,MULTIPLE_CONTENT_LENGTHS` means that the HTTP compliance is that defined by `RFC9110`, but also allows the `HttpCompliance.Violation.MULTIPLE_CONTENT_LENGTHS`, so that requests that have multiple `Content-Length` headers are accepted (they would be rejected when using just `HttpCompliance.RFC9110`).

`jetty.httpConfig.uriCompliance`::
Configures the compliance to URI specifications, see also xref:programming-guide:server/compliance.adoc#uri[this section].
+
The value could be: one of the predefined link:{javadoc-url}/org/eclipse/jetty/http/UriCompliance.html[`UriCompliance`] constants, such as `DEFAULT` or `RFC3986`; or a comma-separated list of violations to allow or forbid, as specified by the link:{javadoc-url}/org/eclipse/jetty/http/UriCompliance.html#from(java.lang.String)[`UriCompliance.from(String)`] method.
+
For example, `jetty.httpConfig.uriCompliance=RFC3986,-AMBIGUOUS_PATH_SEPARATOR` means that the URI compliance is that defined by `RFC3986`, but also does not allow the `UriCompliance.Violation.AMBIGUOUS_PATH_SEPARATOR`, so that requests that have URIs such as `/foo/bar%2Fbaz` (where `%2F` is the URL-encoded `/` character) are rejected (they would be accepted when using just `UriCompliance.RFC3986`).

`jetty.httpConfig.requestCookieCompliance`::
`jetty.httpConfig.responseCookieCompliance`::
Configure the compliance to HTTP cookie specifications, respectively for requests and responses, see also xref:programming-guide:server/compliance.adoc#cookie[this section].
+
The value could be: one of the predefined link:{javadoc-url}/org/eclipse/jetty/http/CookieCompliance.html[`CookieCompliance`] constants, such as `RFC6265`; or a comma-separated list of violations to allow or forbid, as specified by the link:{javadoc-url}/org/eclipse/jetty/http/CookieCompliance.html#from(java.lang.String)[`CookieCompliance.from(String)`] method.
+
For example, `jetty.httpConfig.requestCookieCompliance=RFC6265,-RESERVED_NAMES_NOT_DOLLAR_PREFIXED` means that the cookie compliance is that defined by `RFC6265`, but also does not allow the `CookieCompliance.Violation.RESERVED_NAMES_NOT_DOLLAR_PREFIXED`, so that requests that have cookie headers such as `Cookie: $foo=bar` are rejected (they would be accepted when using just `CookieCompliance.RFC6265`).

`jetty.httpConfig.multiPartCompliance`::
Configures the compliance to MultiPart specifications, see also xref:programming-guide:server/compliance.adoc#multiPart[this section].
+
The value could be: one of the predefined link:{javadoc-url}/org/eclipse/jetty/http/MultiPartCompliance.html[`MultiPartCompliance`] constants, such as `RFC7578_STRICT` or `RFC7578`; or a comma-separated list of violations to allow or forbid, as specified by the link:{javadoc-url}/org/eclipse/jetty/http/MultiPartCompliance.html#from(java.lang.String)[`MultiPartCompliance.from(String)`] method.
+
For example, `jetty.httpConfig.multiPartCompliance=RFC7578,-CONTENT_TRANSFER_ENCODING` means that the MultiPart compliance is that defined by `RFC7578`, but also does not allow the `MultiPartCompliance.Violation.CONTENT_TRANSFER_ENCODING`, so that parts that specify the `Content-Transfer-Encoding` header are rejected (they would be accepted when using just `MultiPartCompliance.RFC7578`).

[[forwarded]]
== Module `forwarded`

The `forwarded` module provides support for processing the `Forwarded` HTTP header (defined in https://tools.ietf.org/html/rfc7239[RFC 7239]) and the now obsoleted `X-Forwarded-*` HTTP headers.

The module properties are:

----
include::{jetty-home}/modules/forwarded.mod[tags=documentation]
----

[[https]]
== Module `https`

The `https` module provides the HTTP/1.1 protocol to the <<ssl,`ssl` module>>.

The module file is `$JETTY_HOME/modules/https.mod`:

----
include::{jetty-home}/modules/https.mod[]
----

[[jmx]]
== Module `jmx`

include::{jetty-home}/modules/jmx.mod[tags=description]

This configuration is useful for xref:jmx/index.adoc#local[local development and testing].
If you need to xref:jmx/index.adoc#remote[enable remote access], use the xref:jmx/index.adoc#remote[`jmx-remote` module].

[[jmx-remote]]
== Module `jmx-remote`

The `jmx-remote` module provides remote access to JMX clients.

The module properties to configure remote JMX connector are:

----
include::{jetty-home}/modules/jmx-remote.mod[tags=documentation]
----

The system property `java.rmi.server.hostname` is specified with the usual notation, prepending a `-D` in front of the system property name.

The system property `java.rmi.server.hostname` is uncommented because it is necessary in the default configuration -- most systems do not have the local name resolution configured properly for remote access.

As an example, in a Linux machine named `beryl`, the `/etc/hosts` file may contain these entries:

----
127.0.0.1 localhost
127.0.1.1 beryl
----

If the system property `java.rmi.server.hostname` is not specified, the RMI implementation uses the host name `beryl` to figure out the IP address to store in the RMI stub, in this case `127.0.1.1`.
However, the RMI server is configured to bind to `localhost`, i.e. `127.0.0.1`.

If the system property `java.rmi.server.hostname` is not specified, the RMI client will try to connect to `127.0.1.1` (because that's what in the RMI stub) and fail because nothing is listening on that address.

[[loginservice-hash]]
== Module `loginservice-hash`

The `loginservice-hash` module creates a `HashLoginService` and adds it as a bean on the `Server`.
A ``SecurityHandler`` uses it by matching its configured realm name.

The `HashLoginService` reads users, their credentials and their roles from a properties file, where each line has the format `<username>: <password>[,<rolename>...]`.
Enabling the module copies a template `etc/realm.properties` file into the `$JETTY_BASE` directory that you can edit to define your users.

The `jetty.hashLoginService.name` property sets the realm name, which must match the realm name expected by the ``Authenticator``.
It defaults to `default`, the realm name used by web applications that declare a `<login-config>` without an explicit `<realm-name>`.
Set it to match the `<realm-name>` in a web application's `web.xml` when a specific realm is used.

The module properties are:

----
include::{jetty-home}/modules/loginservice-hash.mod[tags=documentation]
----

See also xref:programming-guide:security/index.adoc[this section] for more information about Jetty's security features.

[[min-data-rate]]
== Module `min-data-rate`

The `min-data-rate` installs the `MinimumDataRateHandler` at the root of the `Handler` tree.
`MinimumDataRateHandler` enables support to enforce minimum data rates for reading request content and writing response content, as explained in xref:programming-guide:server/http.adoc#handler-use-min-data-rate[this section].

The module properties are:

----
include::{jetty-home}/modules/min-data-rate.mod[tags=documentation]
----

[[openid]]
== Module `openid`

The `openid` module enables support for OpenID Connect (OIDC) authentication in Jetty, as detailed in xref:security/openid-support.adoc#openid-support[this section].

This module allows Jetty to authenticate users via an OpenID Connect identity provider, making possible to integrate features like "Sign in with Google" or "Sign in with Microsoft", among others.

This simplifies user authentication while leveraging the security and convenience of external identity providers.

The module properties are:

----
include::{jetty-home}/modules/openid.mod[tags=documentation]
----

Among the configurable properties, the only required properties are:

`jetty.openid.provider`::
The issuer identifier of the OpenID Provider, which is the base URL before .well-known/openid-configuration. It must match the issuer field in the provider's configuration and is case-sensitive.
`jetty.openid.clientId`::
The Client Identifier assigned to your application by the OpenID Provider, used to uniquely identify your app during authentication requests.
`jetty.openid.clientSecret`::
The Client Secret issued by the OpenID Provider, used to verify your application's identity during the authentication process. This must be kept confidential.

[[qos]]
== Module `qos`

The `qos` module installs the `QoSHandler` at the root of the `Handler` tree; the `QoSHandler` applies limits to the number of concurrent requests, as explained in xref:programming-guide:server/http.adoc#handler-use-qos[this section].

The module properties are:

----
include::{jetty-home}/modules/qos.mod[tags=documentation]
----

[[requestlog]]
== Module `requestlog`

The `requestlog` module provides HTTP request/response logging in the standard https://en.wikipedia.org/wiki/Common_Log_Format[NCSA format], or in a custom format of your choice.

The module properties are:

----
include::{jetty-home}/modules/requestlog.mod[tags=documentation]
----

The property `jetty.requestlog.formatString` can be customized using format codes.

javadoc::code:partial$org/eclipse/jetty/server/CustomRequestLog.java[]

[[requestlog-slf4j]]
== Module `requestlog-slf4j`

The `requestlog-slf4j` module provides HTTP request/response logging to the SLF4J logging framework, as an alternative to the file-based <<requestlog,requestlog>> module.

Request logs are written to a configurable SLF4J logger at INFO level, allowing integration with any SLF4J-compatible logging framework (Logback, Log4j2, etc.).

The module properties are:

----
include::{jetty-home}/modules/requestlog-slf4j.mod[tags=documentation]
----

The property `jetty.requestlog.formatString` uses the same format codes as the <<requestlog,requestlog>> module.

javadoc::code:partial$org/eclipse/jetty/server/CustomRequestLog.java[]

[NOTE]
====
Unlike the `requestlog` module, `requestlog-slf4j` does not create a `logs/` directory or write to files.
All log output is handled by your SLF4J logging configuration.
====

[[resources]]
== Module `resources`

include::{jetty-home}/modules/resources.mod[tags=description]

A common use-case for this module is to provide resources for third-party libraries via the xref:start/index.adoc#start-class-path[server classpath].
For instance, many logging libraries (including https://logging.apache.org/log4j/2.x/[Log4j2] and https://logback.qos.ch/[Logback]) look for their configuration files on the classpath.

Jetty provides a logging library implementation -- enabled via the `logging-jetty` module -- whose configuration file is `$JETTY_BASE/resources/jetty-logging.properties`.

[[rewrite]]
== Module `rewrite`

The `rewrite` module inserts the `RewriteHandler` at the beginning of the `Handler` chain, providing URI-rewriting features similar to the https://httpd.apache.org/docs/current/mod/mod_rewrite.html[Apache's mod_rewrite] or the https://nginx.org/en/docs/http/ngx_http_rewrite_module.html[Nginx rewrite module].

The module properties are:

----
include::{jetty-home}/modules/rewrite.mod[tags=documentation]
----

A common use of the `rewrite` module is to redirect/rewrite old URI paths that have been renamed, for example from `+/old/*+` to `+/new/*+`; in this way, the old paths will not result in a `404` response, but rather be redirected/rewritten to the new paths.

`RewriteHandler` matches incoming requests against a set of rules that you can specify in the `$JETTY_BASE/etc/jetty-rewrite-rules.xml` file.

Rules can be matched against request data such as the request URI or the request headers; if there is a match, the rule is applied.

The rule file `$JETTY_BASE/etc/jetty-rewrite-rules.xml` is initially empty, but contains commented examples of rules that you can add.

The list of available rules can be found link:{javadoc-url}/org/eclipse/jetty/rewrite/handler/package-summary.html[here].

An example of `jetty-rewrite-rules.xml` is the following:

[,xml]
.jetty-rewrite-rules.xml
----
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">
<Configure id="Rewrite" class="org.eclipse.jetty.rewrite.handler.RuleContainer">
  <Call name="addRule">
    
    <Arg>
      <New class="org.eclipse.jetty.rewrite.handler.RedirectRegexRule">
        <Set name="statusCode">301</Set>
        <Set name="pattern">/old/(.*)</Set>
        <Set name="location">/new/$1</Set>
      </New>
    </Arg>
  </Call>
</Configure>
----

Rules can be scoped to a specific virtual host.
In the example below, the rule will only be evaluated if the virtual host matches `example.com`:

[,xml]
.jetty-rewrite-rules.xml
----
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">
<Configure id="Rewrite" class="org.eclipse.jetty.rewrite.handler.RuleContainer">
  <Call name="addRule">
    <Arg>
      <New class="org.eclipse.jetty.rewrite.handler.VirtualHostRuleContainer">
        <Call name="addVirtualHost">
          
          <Arg>example.com</Arg>
        </Call>
        <Call name="addRule">
          <Arg>
            <New class="org.eclipse.jetty.rewrite.handler.RedirectPatternRule">
              <Set name="pattern">/advice</Set>
              <Set name="location">/support</Set>
            </New>
          </Arg>
        </Call>
      </New>
    </Arg>
  </Call>
</Configure>
----

[[scheduler]]
== Module `scheduler`

The `scheduler` module allows you to configure the server-wide scheduler.

The scheduler is used to schedule tasks that must run at a later time, for example the tasks that perform actions when timeouts expire, in particular idle timeouts.

The module properties to configure the scheduler are:

----
include::{jetty-home}/modules/scheduler.mod[tags=documentation]
----

[[server]]
== Module `server`

The `server` module provides generic server support, and configures generic HTTP properties that apply to all HTTP protocols, the scheduler properties and the server specific properties.

[NOTE]
====
The `server` module configures the shared parameters for generic HTTP handling, but does not enable any specific network protocol.

You have to explicitly enable the protocols you want to support by enabling, for example, the <<http,`http` module>> for clear-text HTTP/1.1 support, or the <<http2,`http2` module>> for secure HTTP/2 support, etc.

See also the xref:protocols/index.adoc[protocols section] for more information about the supported protocols.
====

[[server-config]]
=== Server Configuration Properties

The module properties to configure the Jetty server are:

----
include::{jetty-home}/modules/server.mod[tags=documentation]
----

Among the configurable properties, the most relevant are:

`jetty.server.dumpAfterStart`::
Whether to perform a `Server.dump()` operation after the `Server` has started.
The output of the dump operation is sent to `System.err`.
See also the xref:troubleshooting/index.adoc#dump[Jetty Server Dump] section for more information.

`jetty.server.dumpBeforeStop`::
Whether to perform a `Server.dump()` operation before the `Server` stops.
The output of the dump operation is sent to `System.err`.
See also the xref:troubleshooting/index.adoc#dump[Jetty Server Dump] section for more information.

`jetty.server.stopAtShutdown`::
Whether to call `Server.stop()` through a JVM shutdown hook when the JVM exits.

[[size-limit]]
== Module `size-limit`

The `size-limit` module installs the `SizeLimitHandler` at the root of the `Handler` tree; the `SizeLimitHandler` applies limits to the request content and the response content, as explained in xref:programming-guide:server/http.adoc#handler-use-size-limit[this section].

The module properties are:

----
include::{jetty-home}/modules/size-limit.mod[tags=documentation]
----

[[ssl]]
== Module `ssl`

The `ssl` module provides the secure connector, and allows you to configure the KeyStore properties and the TLS parameters, and depends on the <<server,`server` module>>.

For more information about securing a connector and supporting secure protocols, refer to xref:protocols/index.adoc#ssl[this section].

The module properties to configure the secure connector are:

----
include::{jetty-home}/modules/ssl.mod[tags=documentation]
----

Among the configurable properties, the most relevant are:

`jetty.ssl.port`::
The network port that Jetty listens to for secure connections -- default `8443`.
`jetty.ssl.idleTimeout`::
The amount of time a connection can be idle (i.e. no bytes received and no bytes sent) until the server decides to close it to save resources -- default `30000` milliseconds.
`jetty.ssl.acceptors`::
The number of threads that compete to accept connections -- default 1. Use -1 to let the accept heuristic decides the value; the current heuristic calculates a value based on the number of cores).
Refer to <<http-acceptors,this section>> for more information about acceptor threads.
`jetty.ssl.selectors`::
The number of NIO selectors (with an associated thread) that manage connections -- default -1 (i.e. a select heuristic decides the value; the current heuristic calculates a value based on the number of cores).
Refer to <<http-selectors,this section>> for more information about selector threads.

[[ssl-context]]
== Module `ssl-context`

The `ssl-context` module provides the KeyStore, TrustStore and TLS configuration necessary to the `ssl` module.

The Jetty component that manages the KeyStore, that contains the cryptographic material and the TLS configuration is an instance of `SslContextFactory.Server`.

The module properties to configure the KeyStore and TLS parameters are:

----
include::{jetty-home}/modules/ssl-context.mod[tags=documentation]
----

Among the configurable properties, the most relevant are:

`jetty.sslContext.keyStorePath`::
The KeyStore path on the file system, either an absolute path or a relative path to `$JETTY_BASE` -- defaults to `$JETTY_BASE/etc/keystore.p12`.
`jetty.sslContext.keyStorePassword`::
The KeyStore password, which you want to explicitly configure.
The password may be obfuscated with the xref:tools/index.adoc#password[Jetty Password tool].

If you need to configure client certificate authentication, you want to configure one of these properties (they are mutually exclusive):

`jetty.sslContext.needClientAuth`::
Whether client certificate authentication should be required.
`jetty.sslContext.wantClientAuth`::
Whether client certificate authentication should be requested.

Configuring `SslContextFactory.Server` using properties is sufficient for most cases.
For the cases where Jetty module properties are not defined, or when you need more advanced configuration, you can refer to xref:protocols/index.adoc#ssl-customize[this section].

If you configure client certificate authentication, you need to configure and distribute a client KeyStore as explained in xref:keystore/index.adoc#client-authn[this section].

[[ssl-reload]]
== Module `ssl-reload`

The `ssl-reload` module provides a periodic scanning of the directory where the KeyStore file resides.
When the scanning detects a change to the KeyStore file, the correspondent `SslContextFactory.Server` component is reloaded with the new KeyStore configuration.

The module properties are:

----
include::{jetty-home}/modules/ssl-reload.mod[tags=documentation]
----

[[state-tracking]]
== Module `state-tracking`

The `state-tracking` Jetty module inserts the `StateTrackingHandler` at the beginning of the Handler chain.

`StateTrackingHandler` is a xref:troubleshooting/index.adoc[troubleshooting] `Handler` that tracks usages of `Handler`/`Request`/`Response` asynchronous APIs, and logs at warning level invalid usages of the APIs that may lead to blockages, deadlocks, or missing completion of ``Callback``s.

This module can be enabled to troubleshoot web applications that do not behave as expected, for example:

* That consume a lot of threads (possibly because they block).
* That do not send responses (or send only partial responses) to clients.
* That timeout when apparently they have received or have sent all data.

The module properties are:

----
include::{jetty-home}/modules/state-tracking.mod[tags=documentation]
----

[[static-deploy]]
== Module `static-deploy`

The `static-deploy` module enables deployment of web applications that consist only of static files, as described in xref:deploy/index.adoc[this section].

See also the <<core-deploy,`core-deploy`>> module for Jetty core deployment, and the <<eeN-deploy,`{ee-all}-deploy`>> modules for Jakarta EE deployment environments.

[[test-keystore]]
== Module `test-keystore`

The `test-keystore` module creates on-the-fly a KeyStore containing a self-signed certificate for domain `localhost`.
The KeyStore file is automatically deleted when the JVM exits, and re-created when you restart Jetty, to enforce the fact that it is a _test_ KeyStore that should not be reused if not for testing.

The module file is `$JETTY_HOME/modules/test-keystore.mod`:

----
include::{jetty-home}/modules/test-keystore.mod[]
----

Note how properties `jetty.sslContext.keyStorePath` and `jetty.sslContext.keyStorePassword` are configured, only if not already set (via the `?=` operator), directly in the module file, rather than in a `+*.ini+` file.
This is done to avoid that these properties accidentally overwrite a real KeyStore configuration.

[[thread-limit]]
== Module `thread-limit`

The `thread-limit` module installs the `ThreadLimitHandler` at the root of the `Handler` tree; the `ThreadLimitHandler` applies limits to the number of concurrent threads per remote IP address, as explained in xref:programming-guide:server/http.adoc#handler-use-thread-limit[this section].

The module properties are:

----
include::{jetty-home}/modules/thread-limit.mod[tags=documentation]
----

[[threadpool]]
== Module `threadpool`

The `threadpool` module allows you to configure the server-wide thread pool.

The thread pool creates threads on demand up to `maxThreads`, and idles them out if they are not used.

Since Jetty uses the thread pool internally to execute critical tasks, it is not recommended to constrain the thread pool to small values of `maxThreads` with the purpose of limiting HTTP request concurrency, as this could very likely cause a server lockup when Jetty needs to run a critical task but there are no threads available.
Start with the default value of `maxThreads`, and tune for larger values if needed.

The module properties to configure the thread pool are:

----
include::{jetty-home}/modules/threadpool.mod[tags=documentation]
----

Among the configurable properties, the most relevant are:

`jetty.threadPool.namePrefix`::
The name prefix to use for the thread names.

`jetty.threadPool.detailedDump`::
Whether the thread pool should dump the whole stack trace of each thread, or just the topmost stack frame -- defaults to `false`.

`jetty.threadPool.idleTimeout`::
The time, in milliseconds, after which an idle thread is released from the pool -- defaults to 60000, i.e. 60 seconds.

`jetty.threadPool.maxThreads`::
The max number of threads pooled by the thread pool -- defaults to 200.

If you want to use virtual threads, introduced as a preview feature in Java 19 and Java 20, and become an official feature since Java 21, use the following modules:

* The <<threadpool-virtual,`threadpool-virtual`>> Jetty module for Java 21 or later.
* The <<threadpool-virtual-preview,`threadpool-virtual-preview`>> Jetty module for Java 19 and Java 20.

See also the xref:server/index.adoc#threadpool[section about configuring the thread pool].

[[threadpool-all-virtual]]
== Module `threadpool-all-virtual`

The `threadpool-all-virtual` module allows you to configure the server-wide thread pool, similarly to what you can do with the <<threadpool,`threadpool`>> Jetty module, so that all threads are virtual threads, introduced as an official feature since Java 21.

CAUTION: Only use this module if you are using Java 21 or later.

The module properties to configure the thread pool are:

----
include::{jetty-home}/modules/threadpool-all-virtual.mod[tags=documentation]
----

The property `jetty.threadpool.maxThreads` limits, using a `Semaphore`, the number of current virtual threads in use.

Please refer to the xref:programming-guide:arch/threads.adoc#thread-pool-virtual-threads[virtual threads section] of the Jetty Threading Architecture for more information about virtual threads and their pitfalls.

[[threadpool-virtual]]
== Module `threadpool-virtual`

The `threadpool-virtual` module allows you to configure the server-wide thread pool, similarly to what you can do with the <<threadpool,`threadpool`>> Jetty module, but also specify to use virtual threads, introduced as an official feature since Java 21.

CAUTION: Only use this module if you are using Java 21 or later.
If you are using Java 19 or Java 20, use the <<threadpool-virtual-preview,`threadpool-virtual-preview`>> Jetty module instead.

Refer to the <<threadpool,`threadpool`>> Jetty module for the general features provided by that Jetty module that also this Jetty module provides.

The module properties to configure the thread pool are:

----
include::{jetty-home}/modules/threadpool-virtual.mod[tags=documentation]
----

The specific properties to configure virtual threads are:

`jetty.threadPool.virtual.namePrefix`::
The name prefix to use for the virtual thread names.

`jetty.threadPool.virtual.tracking`::
Whether to track virtual threads so that they appear in a xref:troubleshooting/index.adoc#dump[Server Dump] even when they are unmounted.

`jetty.threadPool.virtual.maxConcurrentTasks`::
The number of concurrent tasks run by virtual threads.
This property limits resource usage, avoiding that virtual threads run an unlimited number of concurrent tasks when the system is under load.

[[threadpool-virtual-preview]]
== Module `threadpool-virtual-preview`

The `threadpool-virtual-preview` module allows you to configure the server-wide thread pool, similarly to what you can do with the <<threadpool,`threadpool`>> Jetty module, but also specify to use virtual threads, introduced as a preview feature in Java 19 and in Java 20.

CAUTION: Only use this module if you are using Java 19 or Java 20.
If you are using Java 21 or later, use the <<threadpool-virtual,`threadpool-virtual`>> Jetty module instead.

NOTE: To enable preview features, this module needs to specify the `+--enable-preview+` command line option using the xref:modules/index.adoc#directive-exec[[exec\] directive], and as such it will fork another JVM.

Refer to the <<threadpool,`threadpool`>> Jetty module for the general features provided by that Jetty module that also this Jetty module provides.

The module properties to configure the thread pool are:

----
include::{jetty-home}/modules/threadpool-virtual-preview.mod[tags=documentation]
----

The specific properties to configure virtual threads are:

`jetty.threadPool.virtual.namePrefix`::
The name prefix to use for the virtual thread names.

`jetty.threadPool.virtual.allowSetThreadLocals`::
Whether virtual threads are allowed to set thread locals.

`jetty.threadPool.virtual.inheritInheritableThreadLocals`::
Whether virtual threads inherit the values of `InheritableThreadLocal` variables.

[[well-known]]
== Module `well-known`

The `well-known` Jetty module creates a `ResourceHandler` deployed at the `/.well-known` context path which serves files from a directory.
By default, the directory created at `$JETTY_BASE/.well-known` is used, but it can be configured from `well-known.ini` to anywhere in the filesystem.
Note that the `.well-known` directory may be seen as a hidden directory by the filesystem.

The concept of well-known URIs has been defined in https://datatracker.ietf.org/doc/html/rfc5785[RFC5785].
This module can be used for things like the automatic renewal of https://letsencrypt.org/[Let's Encrypt] certificates.
See https://www.iana.org/assignments/well-known-uris/well-known-uris.xhtml[IANA Well-Known URIs] for more possible examples of how this can be used.

The module properties are:

----
include::{jetty-home}/modules/well-known.mod[tags=documentation]
----

---

### Documentation Jetty Modules Operations Guide Pages Protocols Index (`documentation/jetty/modules/operations-guide/pages/protocols/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Jetty Connectors and Protocols

Connectors are the network components through which Jetty accepts incoming network connections.

Each connector listens on a network port and can be configured with `ConnectionFactory` components that _understand_ one or more network protocols.

Understanding a protocol means that the connector is able to interpret incoming network bytes (for example, the bytes that represent an HTTP/1.1 request) and convert them into more abstract objects (for example an `HttpServletRequest` object) that are then processed by applications.
Conversely, an abstract object (for example an `HttpServletResponse`) is converted into the correspondent outgoing network bytes (the bytes that represent an HTTP/1.1 response).

Like other Jetty components, connectors are enabled and configured by enabling and configuring the correspondent Jetty module.

IMPORTANT: Recall that you must always issue the commands to enable Jetty modules from within the `$JETTY_BASE` directory, and that the Jetty module configuration files are in the `$JETTY_BASE/start.d/` directory.

You can obtain the list of connector-related modules in this way:

----
$ java -jar $JETTY_HOME/start.jar --list-modules=connector
----

[[http]]
== Clear-Text HTTP/1.1

Clear text HTTP/1.1 is enabled with the `http` Jetty module with the following command (issued from within the `$JETTY_BASE` directory):

----
$ java -jar $JETTY_HOME/start.jar --add-modules=http
----

[jetty%nowrap]
....
[jetty]
setupArgs=--add-modules=http
....

After having enabled the `http` module, the `$JETTY_BASE` directory looks like this:

[source,subs=+quotes]
----
JETTY_BASE
├── resources
│   └── jetty-logging.properties
└── start.d
    └── #http.ini#
----

The `http.ini` file is the file that you want to edit to configure network and protocol parameters -- for more details see xref:modules/standard.adoc#http[this section].

Note that the `http` Jetty module depends on the `server` Jetty module.

Some parameters that you may want to configure are in fact common HTTP parameters that are applied not only for clear-text HTTP/1.1, but also for secure HTTP/1.1 or for clear-text HTTP/2 or for encrypted HTTP/2, or for HTTP/3, and these configuration parameters may be present in the `server` module configuration file.

You can force the creation of the `server.ini` file via:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=server
----

Now the `$JETTY_BASE` directory looks like this:

[source]
----
JETTY_BASE
├── resources
│   └── jetty-logging.properties
└── start.d
    ├── http.ini
    └── server.ini
----

Now you can edit the `server.ini` file -- for more details see xref:modules/standard.adoc#server[this section].

[[https]]
== Secure HTTP/1.1

Secure HTTP/1.1 is enabled with both the `ssl` and `https` Jetty modules with the following command (issued from within the `$JETTY_BASE` directory):

----
$ java -jar $JETTY_HOME/start.jar --add-modules=ssl,https
----

[jetty%nowrap]
....
[jetty]
args=--add-modules=ssl,https
....

The command above enables the `ssl` module, that provides the secure network connector, the KeyStore configuration and TLS configuration -- for more details see <<ssl,this section>>.
Then, the xref:modules/standard.adoc#https[`https` module] adds HTTP/1.1 as the protocol secured by TLS.

The `$JETTY_BASE` directory looks like this:

[source]
----
$JETTY_BASE
├── resources
│   └── jetty-logging.properties
└── start.d
    ├── https.ini
    └── ssl-context.ini
----

Note that the KeyStore file is missing, because you have to provide one with the cryptographic material you want (read xref:keystore/index.adoc[this section] to create your own KeyStore).
You need to configure these two properties by editing `ssl-context.ini`:

* `jetty.sslContext.keyStorePath`
* `jetty.sslContext.keyStorePassword`

As a quick example, you can enable the xref:modules/standard.adoc#test-keystore[`test-keystore` module], that creates on-the-fly a KeyStore containing a self-signed certificate:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=test-keystore
----

[jetty%nowrap]
....
[jetty]
setupArgs=--add-modules=ssl,https
args=--add-modules=test-keystore
....

The `$JETTY_BASE` directory is now:

[source,subs=+quotes]
----
├── etc
│   └── #test-keystore.p12#
├── resources
│   └── jetty-logging.properties
└── start.d
    ├── https.ini
    ├── ssl-context.ini
    └── test-keystore.ini
----

Starting Jetty yields:

----
$ java -jar $JETTY_HOME/start.jar
----

[jetty%nowrap]
....
[jetty]
setupArgs=--add-modules=ssl,https,test-keystore
highlight=(\{.*:8443})
....

Note how Jetty is listening on port `8443` for the secure HTTP/1.1 protocol.

[WARNING]
====
If you point your browser at `+https://localhost:8443/+` you will get a warning from the browser about a "potential security risk ahead", or that "your connection is not private", or similar message depending on the browser.

This is normal because the certificate contained in `test-keystore.p12` is self-signed -- and as such not signed by a recognized certificate authority -- and therefore browsers do not trust it.
====

[[http2]]
== Configuring HTTP/2

https://tools.ietf.org/html/rfc7540[HTTP/2] is the successor of the HTTP/1.1 protocol, but it is quite different from HTTP/1.1: where HTTP/1.1 is a duplex, text-based protocol, HTTP/2 is a multiplex, binary protocol.

Because of these fundamental differences, a client and a server need to _negotiate_ what version of the HTTP protocol they speak, based on what versions each side supports.

To ensure maximum compatibility, and reduce the possibility that an intermediary that only understands HTTP/1.1 will close the connection when receiving unrecognized HTTP/2 bytes, HTTP/2 is typically deployed over secure connections, using the TLS protocol to wrap HTTP/2.

IMPORTANT: Browsers only support secure HTTP/2.

The protocol negotiation is performed by the https://tools.ietf.org/html/rfc7301[ALPN TLS extension]: the client advertises the list of protocols it can speak, and the server communicates to the client the protocol chosen by the server.

For example, you can have a client that only supports HTTP/1.1 and a server that supports both HTTP/1.1 and HTTP/2:

[plantuml]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false

participant "client\nsupports\nhttp/1.1" as client
participant "server\nsupports\nhttp/1.1 & http/2" as server

group TLS handshake
client -> server : ClientHello (alpn=[http/1.1])
server -> server : picks http/1.1
server -> client : ServerHello (alpn=http/1.1)
...rest of TLS handshake...
end
group TLS HTTP/1.1
client -> server : HTTP/1.1 GET
server -> client : HTTP/1.1 200
end
----

Nowadays, it's common that both clients and servers support HTTP/2, so servers prefer HTTP/2 as the protocol to speak:

[plantuml]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false

participant "client\nsupports\nhttp/1.1 & http/2" as client
participant "server\nsupports\nhttp/1.1 & http/2" as server

group TLS handshake
client -> server : ClientHello (alpn=[http/1.1,h2])
server -> server : picks http/2
server -> client : ServerHello (alpn=h2)
...rest of TLS handshake...
end
group TLS HTTP/2
client -> server : HTTP/2 GET
server -> client : HTTP/2 200
end
----

When you configure a connector with the HTTP/2 protocol, you typically want to also configure the HTTP/1.1 protocol.
The reason to configure both protocols is that you typically do not control the clients: for example an old browser that does not support HTTP/2, or a monitoring console that performs requests using HTTP/1.1, or a heartbeat service that performs a single HTTP/1.0 request to verify that the server is alive.

== Secure vs Clear-Text HTTP/2

Deciding whether you want to configure Jetty with <<http2s,secure HTTP/2>> or <<http2c,clear-text HTTP/2>> depends on your use case.

You want to configure secure HTTP/2 when Jetty is exposed directly to browsers, because browsers only support secure HTTP/2.

[plantuml]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false
skinparam roundCorner 10

rectangle browser
cloud internet
rectangle jetty

jetty <--> internet : TLS+HTTP/2
internet <--> browser : TLS+HTTP/2
----

You may configure clear-text HTTP/2 (mostly for performance reasons) if you offload TLS at a load balancer (for example, https://haproxy.org/[HAProxy]) or at a reverse proxy (for example, https://nginx.org/[nginx]).

[plantuml]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false
skinparam roundCorner 10

rectangle browser
cloud internet
rectangle haproxy
rectangle jetty

note right of haproxy: TLS offload

jetty <--> haproxy : HTTP/2 (clear-text)
haproxy <--> internet : TLS+HTTP/2
internet <--> browser : TLS+HTTP/2
----

You may configure clear-text HTTP/2 (mostly for performance reasons) to call microservices deployed to different Jetty servers (although you may want to use secure HTTP/2 for confidentiality reasons).

[plantuml]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false
skinparam roundCorner 10

rectangle browser
cloud internet
rectangle haproxy
rectangle jetty
rectangle microservice1
rectangle microservice2
rectangle microservice3

note right of haproxy: TLS offload

internet <--> browser : TLS+HTTP/2
haproxy <--> internet :  TLS+HTTP/2
jetty <--> haproxy : HTTP/2 (clear-text)
microservice1 <--> jetty : HTTP/2
microservice2 <--> jetty : HTTP/2
microservice3 <--> jetty : HTTP/2
microservice2 <--> microservice3 : HTTP/2
microservice1 <--> microservice3 : HTTP/2
----

[[http2s]]
== Secure HTTP/2

When you enable secure HTTP/2 you typically want to enable also secure HTTP/1.1, for backwards compatibility reasons: in this way, old browsers or other clients that do not support HTTP/2 will be able to connect to your server.

You need to enable:

* the `ssl` Jetty module, which provides the secure connector and the KeyStore and TLS configuration
* the `http2` Jetty module, which adds ALPN handling and adds the HTTP/2 protocol to the secured connector
* optionally, the `https` Jetty module, which adds the HTTP/1.1 protocol to the secured connector

Use the following command (issued from within the `$JETTY_BASE` directory):

----
$ java -jar $JETTY_HOME/start.jar --add-modules=ssl,http2,https
----

As when enabling the `https` Jetty module, you need a valid KeyStore (read xref:keystore/index.adoc[this section] to create your own KeyStore).

As a quick example, you can enable the xref:modules/standard.adoc#test-keystore[`test-keystore` module], that creates on-the-fly a KeyStore containing a self-signed certificate:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=test-keystore
----

Starting Jetty yields:

----
$ java -jar $JETTY_HOME/start.jar
----

[jetty%nowrap]
....
[jetty]
setupArgs=--add-modules=ssl,http2,https,test-keystore
highlight=(\{.*:8443})
....

Note how Jetty is listening on port `8443` and the protocols supported are the sequence `(ssl, alpn, h2, http/1.1)`.

The (ordered) list of protocols after `alpn` are the _application protocols_, in the example above `(h2, http/1.1)`.

When a new connection is accepted by the connector, Jetty first interprets the TLS bytes, then it handles the ALPN negotiation knowing that the application protocols are (in order) `h2` and then `http/1.1`.

You can customize the list of application protocols and the default protocol to use in case the ALPN negotiation fails by editing the xref:modules/standard.adoc#alpn[`alpn` module] properties.

The HTTP/2 protocol parameters can be configured by editing the xref:modules/standard.adoc#http2[`http2` module] properties.

[[http2c]]
== Clear-Text HTTP/2

When you enable clear-text HTTP/2 you typically want to enable also clear-text HTTP/1.1, for backwards compatibility reasons and to allow clients to https://tools.ietf.org/html/rfc7540#section-3.2[upgrade] from HTTP/1.1 to HTTP/2.

You need to enable:

* the `http` Jetty module, which provides the clear-text connector and adds the HTTP/1.1 protocol to the clear-text connector
* the `http2c` Jetty module, which adds the HTTP/2 protocol to the clear-text connector

----
$ java -jar $JETTY_HOME/start.jar --add-modules=http,http2c
----

Starting Jetty yields:

----
$ java -jar $JETTY_HOME/start.jar
----

[jetty%nowrap]
....
[jetty]
setupArgs=--add-modules=http,http2c
highlight=(\{.+:8080})
....

Note how Jetty is listening on port `8080` and the protocols supported are HTTP/1.1 and `h2c` (i.e. clear-text HTTP/2).

With this configuration, browsers and client applications will be able to connect to port `8080` using:

* HTTP/1.1 directly (e.g. `curl --http1.1 ++http://localhost:8080++`):
----
GET / HTTP/1.1
Host: localhost:8080
----
* HTTP/1.1 with upgrade to HTTP/2 (e.g. `curl --http2 ++http://localhost:8080++`):
----
GET / HTTP/1.1
Host: localhost:8080
Connection: Upgrade, HTTP2-Settings
Upgrade: h2c
HTTP2-Settings:
----
* HTTP/2 directly (e.g. `curl --http2-prior-knowledge ++http://localhost:8080++`):
----
50 52 49 20 2a 20 48 54 54 50 2f 32 2e 30 0d 0a
0d 0a 53 4d 0d 0a 0d 0a 00 00 12 04 00 00 00 00
00 00 03 00 00 00 64 00 04 40 00 00 00 00 02 00
00 00 00 00 00 1e 01 05 00 00 00 01 82 84 86 41
8a a0 e4 1d 13 9d 09 b8 f0 1e 07 7a 88 25 b6 50
c3 ab b8 f2 e0 53 03 2a 2f 2a
----

The HTTP/2 protocol parameters can be configured by editing the xref:modules/standard.adoc#http2c[`http2c` module] properties.

[[http3]]
== HTTP/3

When you enable support for the HTTP/3 protocol, by default the secure HTTP/2 protocol is also enabled, so that browsers or clients that do not support HTTP/3 will be able to connect to your server.

You need to enable:

* the `ssl` Jetty module, which provides the KeyStore and TLS configuration
* the `http3` Jetty module, which adds the HTTP/3 protocol on the HTTP/3 connector

Use the following command (issued from within the `$JETTY_BASE` directory):

----
$ java -jar $JETTY_HOME/start.jar --add-modules=ssl,http3
----

Enabling any module Jetty module that supports secure network communication requires a valid KeyStore (read xref:keystore/index.adoc[this section] to create your own KeyStore), that, as a quick example, you can enable with the xref:modules/standard.adoc#test-keystore[`test-keystore` module], that creates on-the-fly a KeyStore containing a self-signed certificate:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=test-keystore
----

Starting Jetty yields:

----
$ java -jar $JETTY_HOME/start.jar
----

[jetty%nowrap]
....
[jetty]
setupArgs=--approve-all-licenses --add-modules=ssl,http3,test-keystore
highlight=(\{.*:8444})
....

Note how Jetty is listening on port `8443` for HTTP/2 and on port `8444` for HTTP/3.

The HTTP/3 protocol parameters can be configured by editing the xref:modules/standard.adoc#http3[`http3` module] properties.

[[websocket]]
== WebSocket

WebSocket is a network protocol for bidirectional data communication, initiated via HTTP and then upgraded to WebSocket.
WebSocket provides a simple, low-level, framing protocol layered over the underlying transport protocol, typically TCP.
One or more WebSocket data frames compose a WebSocket _message_ that is either a UTF-8 _text_ message or _binary_ message.

Jetty provides an implementation of the following standards and specifications.

http://tools.ietf.org/html/rfc6455[RFC-6455] - The WebSocket Protocol::
Jetty supports version 13 of the released and final specification.

http://www.jcp.org/en/jsr/detail?id=356[JSR-356] - The Java WebSocket API (`javax.websocket`)::
This is the official Java API for working with WebSockets.

https://tools.ietf.org/html/rfc7692[RFC-7692] - WebSocket Per-Message Deflate Extension::
This is the replacement for perframe-compression, switching the compression to being based on the entire message, not the individual frames.

https://tools.ietf.org/html/rfc8441[RFC-8441] - Bootstrapping WebSockets with HTTP/2::
Allows a single stream of an HTTP/2 connection to be upgraded to WebSocket.
This allows one TCP connection to be shared by both protocols and extends HTTP/2's more efficient use of the network to WebSockets.

[[websocket-configure]]
=== Configuring WebSocket

Jetty provides two WebSocket implementations: one based on the Jakarta EE WebSocket APIs defined by JSR 356, provided by module `{ee-current}-websocket-jakarta` (for Java EE 8, the module is called `ee8-websocket-javax`), and one based on Jetty specific WebSocket APIs, provided by module `websocket-jetty`.

NOTE: Remember that a WebSocket connection is always initiated from the HTTP protocol (either an HTTP/1.1 upgrade or an HTTP/2 connect), therefore to enable WebSocket you need to enable HTTP.

To enable WebSocket support, you also need to decide what version of the HTTP protocol you want WebSocket to be initiated from, and whether you want secure HTTP.

For example, to enable clear-text WebSocket from HTTP/1.1, using the Jakarta {ee-current-caps} implementation, use the following command (issued from within the `$JETTY_BASE` directory):

[,shell,subs=+attributes]
----
$ java -jar $JETTY_HOME/start.jar --add-modules=http,{ee-current}-websocket-jakarta
----

To enable secure WebSocket from HTTP/2, using the Jetty implementation, use the following command (issued from within the `$JETTY_BASE` directory):

[,shell]
----
$ java -jar $JETTY_HOME/start.jar --add-modules=http2,websocket-jetty
----

When enabling secure protocols you need a valid KeyStore (read xref:keystore/index.adoc[this section] to create your own KeyStore).
As a quick example, you can enable the xref:modules/standard.adoc#test-keystore[`test-keystore` module], that creates on-the-fly a KeyStore containing a self-signed certificate:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=test-keystore
----

To enable WebSocket on both HTTP/1.1 and HTTP/2, both clear-text and secure, using the Jakarta {ee-current-caps} implementation, use the following command (issued from within the `$JETTY_BASE` directory):

----
$ java -jar $JETTY_HOME/start.jar --add-modules=http,https,http2c,http2,{ee-current}-websocket-jakarta
----

[[websocket-disable]]
=== Selectively Disabling WebSocket

Enabling the Jakarta EE WebSocket Jetty modules comes with a startup cost because Jetty must perform two steps:

. Scan Jakarta EE web applications `+*.war+` files (and all the jars and classes inside it) looking for WebSocket EndPoint classes (either annotated with the Jakarta EE WebSocket API annotations or extending/implementing Jakarta EE WebSocket API classes/interfaces).
This can be a significant cost if your web application contains a lot of classes and/or jar files.
. Configure and wire WebSocket EndPoints so that WebSocket messages are delivered to the correspondent WebSocket EndPoint.

WebSocket support is by default enabled for all Jakarta EE web applications.

For a specific Jakarta EE web application, you can disable step 2 for Jakarta EE WebSocket support (when the `{ee-current}-websocket-jakarta` module is enabled) by setting the context attribute `org.eclipse.jetty.websocket.jakarta` to `false`:

[,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
         version="6.0">

  
  <context-param>
    <param-name>org.eclipse.jetty.websocket.jakarta</param-name>
    <param-value>false</param-value>
  </context-param>

  ...

</web-app>
----

NOTE: For Java EE 8, the parameter name is `org.eclipse.jetty.websocket.javax`.

Furthermore, for a specific web application, you can disable step 1 (and therefore also step 2) as described in the xref:annotations/index.adoc[annotations processing section].

[[websocket-webapp-client]]
=== Using WebSocket Client in Web Applications

Web applications may need to use a WebSocket client to communicate with third party WebSocket services.

If the web application uses the Jakarta EE WebSocket APIs, the Jakarta EE WebSocket client APIs are provided by Jetty and are available to the web application as long as the `{ee-current}-websocket-jakarta` Jetty module is enabled.

However, the Jakarta EE WebSocket Client APIs are quite limited (for example, they do not support secure WebSocket).
For this reason, web applications may want to use the Jetty WebSocket Client APIs.

When using the Jetty WebSocket Client APIs, Jakarta EE web applications should include the required jars and their dependencies in the `WEB-INF/lib` directory of the `+*.war+` file.
Alternatively, when deploying your Jakarta EE web applications in Jetty, you can enable the `websocket-jetty-client` Jetty module to allow Jakarta EE web applications to use the Jetty WebSocket Client APIs provided by Jetty, without the need to include jars and their dependencies in the `+*.war+` file.

[[fcgi]]
== FastCGI

FastCGI is a network protocol primarily used by a _web server_ to communicate to a __FastCGI server__.

FastCGI servers are typically used to serve web content generated by dynamic web languages, primarily http://www.php.net/[PHP], but also Python, Ruby, Perl and others.

Web servers that supports FastCGI are, among others, http://httpd.apache.org/[Apache], http://nginx.org/[Nginx], and Jetty.
Web servers typically act as reverse proxies, converting HTTP requests that they receive from clients (browsers) to FastCGI requests that are forwarded to the FastCGI server.
The FastCGI server spawns the dynamic web language interpreter, passing it the information contained in the FastCGI request and a dynamic web language script is executed, producing web content, typically HTML.
The web content is then formatted into a FastCGI response that is returned to the web server, which converts it to an HTTP response that is then returned to the client.

The most well known FastCGI server is the http://php-fpm.org/[PHP FastCGI Process Manager], or `php-fpm`.
In the following we will assume that `php-fpm` is used as FastCGI server.

This is a diagram of what described above:

[plantuml]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false

Browser -> Jetty : GET /path (HTTP)
Jetty -> "php-fpm" : GET /path (FastCGI)
"php-fpm" -> PHP : spawn PHP interpreter
PHP -> "index.php" : execute PHP script
"index.php" -> "php-fpm" : HTML
"php-fpm" -> Jetty : 200 OK + HTML (FastCGI)
Jetty -> Browser : 200 OK + HTML (HTTP)

----

Jetty can be configured to act as a web server that supports FastCGI, replacing the functionality that is normally provided by Apache or Nginx.
This allows users to leverage Jetty features such as the support for HTTP/1.1, HTTP/2 and HTTP/3, Jetty's scalability, and of course Jetty's native support for Jakarta EE Web Standards such as Servlets, JSPs, etc.

With such configuration, users can not only deploy their Jakarta EE web applications in Jetty, but also serve their http://wordpress.com/[WordPress] site or blog or their https://drupal.org/[Drupal] site without having to install and manage multiple web servers.

[[fcgi-configure]]
=== Configuring WordPress

This section explains how to configure Jetty to serve your https://wordpress.com/[WordPress] site.

The prerequisites are:

* Have `php-fpm` installed on your server host, and configured to listen either on a Unix-Domain socket (such as `/run/php/php-fpm.sock`), or on a TCP socket (such as `localhost:9000`).
* Have WordPress installed on your server host, for example under `/var/www/wordpress`.
For more information about how to install WordPress and the necessary PHP modules, please refer to the https://wordpress.org/support/article/how-to-install-wordpress/[WordPress Installation Guide].

Then, the <<https,secure HTTP>> and/or the <<http2,secure HTTP/2>> Jetty modules should be enabled to allow browsers to connect to Jetty.

Lastly, enable the `fcgi-proxy` module to provide FastCGI support (to convert HTTP requests from browsers to FastCGI for `php-fpm` and vice versa), and the `core-deploy` module to deploy your WordPress web application as a xref:deploy/index.adoc#jetty[Jetty context XML file].

For example:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=ssl,https,fcgi-proxy,core-deploy
----

TIP: The `https` Jetty module requires a KeyStore. If you do not already have one configured, you can add the `test-keystore` Jetty module to the command line above to create a KeyStore on-the-fly.

Now you can deploy a Jetty context XML file that represents your WordPress web application.

Use the following file as example, copy it as `$JETTY_BASE/webapps/wordpress.xml` and customize it as necessary:

[,xml,options=nowrap]
----
<?xml version="1.0"  encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">
<Configure class="org.eclipse.jetty.server.handler.ContextHandler">

  <New id="root" class="java.lang.String">
    <Arg>/var/www/wordpress</Arg> 
  </New>

  <Set name="contextPath">/</Set> 
  <Set name="baseResourceAsString"><Ref refid="root" /></Set>

  <Set name="handler">
    <New class="org.eclipse.jetty.server.handler.TryPathsHandler">
      <Set name="originalPathAttribute">wordpress.originalPath</Set>
      <Set name="originalQueryAttribute">wordpress.originalQuery</Set>
      <Set name="paths">
        <Call class="java.util.List" name="of">
          <Arg>$path</Arg>
          <Arg>/index.php</Arg>
        </Call>
      </Set>

      <Set name="handler">
        <New class="org.eclipse.jetty.server.handler.PathMappingsHandler">
          <Call name="addMapping">
            <Arg>
              <New class="org.eclipse.jetty.http.pathmap.ServletPathSpec"><Arg>*.php</Arg></New>
            </Arg>
            <Arg>
              <New class="org.eclipse.jetty.fcgi.proxy.FastCGIProxyHandler"> 
                <Arg>(https?)://([^/]+)(.*)</Arg> 
                <Arg>http://localhost:9000$3</Arg> 
                <Arg><Ref refid="root" /></Arg>

                <Set name="originalPathAttribute">wordpress.originalPath</Set>
                <Set name="originalQueryAttribute">wordpress.originalQuery</Set>
                <Set name="scriptPattern"><Call class="java.util.regex.Pattern" name="compile"><Arg>(.+?\\.php)</Arg></Call></Set>
                <Set name="fastCGISecure">true</Set>
                <Set name="unixDomainPath"><Call class="java.nio.file.Path" name="of"><Arg>/run/php/php-fpm.sock</Arg></Call></Set> 
              </New>
            </Arg>
          </Call>
          <Call name="addMapping">
            <Arg>
              <New class="org.eclipse.jetty.http.pathmap.ServletPathSpec"><Arg>/</Arg></New>
            </Arg>
            <Arg>
              <New class="org.eclipse.jetty.server.handler.ResourceHandler"> 
                <Set name="welcomeFiles"><Call class="java.util.List" name="of"><Arg>/index.php</Arg></Call></Set>
                <Set name="welcomeMode"><Call class="org.eclipse.jetty.server.ResourceService$WelcomeMode" name="valueOf"><Arg>REHANDLE</Arg></Call></Set>
                <Set name="dirAllowed">false</Set>
              </New>
            </Arg>
          </Call>
        </New>
      </Set>
    </New>
  </Set>

</Configure>
----
<1> Specify the WordPress installation path.
<2> Specify the context path of your web application.
<3> The `FastCGIProxyHandler` forwards requests whose URI path matches `+*.php+` to `php-fpm`.
<4> The client URI regex pattern to match.
<5> The URI used to forward the request to `php-fpm`, where `+$3+` is the 3rd matching group of the client URI regex pattern (int this example, the client URI path).
If `php-fpm` is configured to listen on a TCP socket, the host and port must match the listening TCP socket.
If `php-fpm` is configured to listen on a Unix-Domain socket, the host and port values are ignored but must be present.
<6> If `php-fpm` is configured to listen on a Unix-Domain socket, specify the Unix-Domain socket path, otherwise omit this line.
<7> The `ResourceHandler` serves static files from WordPress, such as `+*.css+`, `+*.js+` and image files.

Now you can start Jetty and navigate to `+http://localhost:8080+` with your browser to enjoy WordPress:

----
$ java -jar $JETTY_HOME/start.jar
----

[[ssl]]
== Configuring Secure Protocols

Secure protocols are normal protocols such as HTTP/1.1, HTTP/2 or WebSocket that are wrapped by the https://en.wikipedia.org/wiki/Transport_Layer_Security[TLS protocol].
Any network protocol based on TCP can be wrapped with TLS.

QUIC, the protocol based on UDP that transports HTTP/3, uses TLS messages but not the TLS protocol framing.

The `https` scheme used in URIs really means `tls+http/1.1` (or `tls+http/2`, or `quic+http/3`) and similarly the `wss` scheme used in URIs really means `tls+websocket`, etc.
Senders wrap the underlying protocol bytes (e.g. HTTP bytes or WebSocket bytes) with the TLS protocol, while receivers first interpret the TLS protocol to obtain the underlying protocol bytes, and then interpret the wrapped bytes.

The xref:modules/standard.adoc#ssl[`ssl` Jetty module] allows you to configure a secure network connector; if other modules require encryption, they declare a dependency on the `ssl` module.

It is the job of other Jetty modules to configure the wrapped protocol.
For example, it is the <<https,`https` module>> that configures the wrapped protocol to be HTTP/1.1.
Similarly, it is the <<http2,`http2` module>> that configures the wrapped protocol to be HTTP/2.
If you enable _both_ the `https` and the `http2` module, you will have a single secure connector that will be able to interpret both HTTP/1.1 and HTTP/2.

[[ssl-customize]]
=== Customizing KeyStore and SSL/TLS Configuration

Secure protocols have a slightly more complicated configuration since they require to configure a _KeyStore_.
Refer to the xref:keystore/index.adoc[KeyStore section] for more information about how to create and manage a KeyStore.

For simple cases, you only need to configure the KeyStore path and KeyStore password as explained in xref:modules/standard.adoc#ssl-context[this section].

For more advanced configuration, for example where Jetty module properties are not defined, or the values for those properties need to be fetched from external sources, you can follow the steps below.

First, create a custom xref:xml/index.adoc[Jetty XML file] and reference it in `$JETTY_BASE/start.d/ssl-context.ini`:

.ssl-context.ini
[source,subs="verbatim,quotes"]
----
--module=ssl-context
*etc/tls-config.xml* <1>
...
----
<1> The path to the custom XML file, relative to `$JETTY_BASE`.

The `ssl-context.ini` file above only shows the lines that are not commented out (you can leave the lines that are commented unmodified for future reference).

You want to create the `$JETTY_BASE/etc/tls-config.xml` with the following template content:

.tls-config.xml
[,xml]
----
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure>
  <Ref refid="sslContextFactory">
    ... <1>
  </Ref>
</Configure>
----
<1> Here goes your advanced configuration.

The `tls-config.xml` file references the `sslContextFactory` component (created by the `ssl-context` Jetty module) that configures the KeyStore and TLS parameters, so that you can now call its APIs via XML (using the syntax described in xref:xml/index.adoc[this section]), and you will have full flexibility for any advanced configuration you want (see below for few examples).

Refer to the link:{javadoc-url}/org/eclipse/jetty/util/ssl/SslContextFactory.Server.html[SslContextFactory.Server javadocs] for the list of methods that you can call through the Jetty XML file.

CAUTION: Use module properties whenever possible, and only resort to use a Jetty XML file for advanced configuration that you cannot do using module properties.

[[ssl-customize-versions]]
==== Customizing SSL/TLS Protocol Versions

By default, the SSL protocols (SSL, SSLv2, SSLv3, etc.) are already excluded because they are vulnerable.
To explicitly add the exclusion of TLSv1.0 and TLSv1.1 (that are also vulnerable -- which leaves only TLSv1.2 and TLSv1.3 available), you want to use this XML:

.tls-config.xml
[,xml]
----
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure>
  <Ref refid="sslContextFactory">
    <Call name="addExcludeProtocols">
      <Arg>
        <Array type="String">
          <Item>TLSv1.0</Item>
          <Item>TLSv1.1</Item>
        </Array>
      </Arg>
    </Call>
  </Ref>
</Configure>
----

[[ssl-customize-ciphers]]
==== Customizing SSL/TLS Ciphers

By default, certain cipher suites are already excluded because they are either weak or vulnerable.
To explicitly exclude more cipher suites in addition to those excluded by default, you can use this XML:

.tls-config.xml
[,xml]
----
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure>
  <Ref refid="sslContextFactory">
    <Call name="addExcludeCipherSuites">
      <Arg>
        <Array type="String">
          <Item>^TLS_DHE_.*$</Item>
          <Item>TLS_CHACHA20_POLY1305_SHA256</Item>
        </Array>
      </Arg>
    </Call>
  </Ref>
</Configure>
----

Note how each array item specifies a _regular expression_ that matches multiple ciphers, or specifies a precise cipher to exclude.

You can choose to create multiple XML files, and reference them all from `$JETTY_BASE/start.d/ssl-context.ini`, or put all your custom configurations in a single XML file.

[[ssl-renew]]
=== Renewing the Certificates

When you create a certificate, you must specify for how many days it is valid.

The typical validity is 90 days, and while this period may seem short, it has two advantages:

* Reduces the risk in case of compromised/stolen keys.
* Encourages automation, i.e. certificate renewal performed by automated tools (rather than manually) at scheduled times.

To renew a certificate, you must go through the xref:keystore/index.adoc#create[same steps] you followed to create the certificate the first time, and then you can <<ssl-reload,reload the KeyStore>> without the need to stop Jetty.

[[ssl-reload]]
=== Watching and Reloading the KeyStore

Jetty can be configured to monitor the directory of the KeyStore file, and reload the `SslContextFactory` component if the KeyStore file changed.

This feature can be enabled by activating the `ssl-reload` Jetty module:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=ssl-reload
----

For more information about the configuration of the `ssl-reload` Jetty module, see xref:modules/standard.adoc#ssl-reload[this section].

[[ssl-conscrypt]]
=== Using Conscrypt as SSL/TLS Provider

If not explicitly configured, the TLS implementation is provided by the JDK you are using at runtime.

OpenJDK's vendors may replace the default TLS provider with their own, but you can also explicitly configure an alternative TLS provider.

The standard TLS provider from OpenJDK is implemented in Java (no native code), and its performance is not optimal, both in CPU usage and memory usage.

A faster alternative, implemented natively, is Google's https://github.com/google/conscrypt/[Conscrypt], which is built on https://boringssl.googlesource.com/boringssl/[BoringSSL], which is Google's fork of https://www.openssl.org/[OpenSSL].

CAUTION: As Conscrypt eventually binds to a native library, there is a higher risk that a bug in Conscrypt or in the native library causes a JVM crash, while the Java implementation will not cause a JVM crash.

To use Conscrypt as the TLS provider just enable the `conscrypt` Jetty module:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=conscrypt
----

[[ssl-bouncycastle]]
=== Using Bouncy Castle as SSL/TLS Provider

If not explicitly configured, the TLS implementation is provided by the JDK you are using at runtime.

OpenJDK's vendors may replace the default TLS provider with their own, but you can also explicitly configure an alternative TLS provider.

link:https://www.bouncycastle.org/[Bouncy Castle] is a pure Java alternative provider for the Java security APIs, and includes a TLS provider.

To use Bouncy Castle as the TLS provider just enable the `bouncycastle` Jetty module:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=bouncycastle
----

[[ssl-sni]]
=== Configuring SNI

Server Name Indication (SNI) is a TLS extension that clients send to indicate what domain they want to connect to during the initial TLS handshake.

Modern TLS clients (e.g. browsers) always send the SNI extension; however, older TLS clients may not send the SNI extension.

Being able to handle the SNI is important when you have xref:deploy/index.adoc#virtual-hosts[virtual hosts] and a KeyStore with multiple certificates, one for each domain.

For example, you may have deployed over a secure connector two web applications, both at context path `/`, one at virtual host `one.com` and one at virtual host `two.net`.
The KeyStore contains two certificates, one for `one.com` and one for `two.net`.

There are three `ssl` module properties that control the SNI behavior on the server: one that works at the TLS level, and two that works at the HTTP level.

The property that works at the TLS level is:

`jetty.sslContext.sniRequired`::
Whether SNI is required at the TLS level, defaults to `false`.
Its behavior is explained by the following table:
+
.Behavior of the `jetty.sslContext.sniRequired` property
[cols="3*a"]
|===
|
| `sniRequired=false`
| `sniRequired=true`

| SNI = `null`
| client receives default certificate
| client receives TLS failure

| SNI = `wrong.org`
| client receives default certificate
| client receives TLS failure

| SNI = `one.com`
| client receives `one.com` certificate
| client receives `one.com` certificate
|===
+
[WARNING]
====
The _default certificate_ is the certificate returned by the TLS implementation in case there is no SNI match, and you should not rely on this certificate to be the same across Java vendors and versions, or Jetty versions, or TLS provider vendors and versions.

In the example above it could be either the `one.com` certificate or the `two.net` certificate.
====

When `jetty.sslContext.sniRequired=true`, clients that don't send a valid SNI receive a TLS failure, and their attempt to connect to the server fails.
The details of this failure may not be reported and could be difficult to figure out that the failure is related to an invalid SNI.

For this reason, other two properties are defined at the HTTP level, so that clients can received an HTTP 400 response with more details about what went wrong while trying to connect to the server:

`jetty.ssl.sniRequired`::
Whether SNI is required at the HTTP level, defaults to `false`.
Its behavior is similar to the `jetty.sslContext.sniRequired` property above, and is explained by the following table:
+
.Behavior of the `jetty.ssl.sniRequired` property
[cols=3*a]
|===
|
| `sniRequired=false`
| `sniRequired=true`

| SNI = `null`
| Accept
| Reject: 400 Bad Request

| SNI = `wrong.org`
| Accept
| Reject: 400 Bad Request

| SNI = `one.com`
| Accept
| Accept
|===

When `jetty.ssl.sniRequired=true`, the SNI is matched against the certificate sent to the client, and only if there is a match the request is accepted.

When the request is accepted, there could be an additional check controlled by the following property:

`jetty.ssl.sniHostCheck`::
Whether the certificate sent to the client matches the `Host` header, defaults to `true`.
Its behavior is explained by the following table:
+
.Behavior of the `jetty.ssl.sniHostCheck` property
[cols="3*a"]
|===
|
| `sniHostCheck=false`
| `sniHostCheck=true`

| certificate = `one.com` +
`Host: wrong.org`
| Accept
| Reject: 400 Bad Request

| certificate = `one.com` +
`Host: one.com`
| Accept
| Accept
|===

In the normal case with the default server configuration, for a TLS clients that sends SNI, and then sends an HTTP request with the correct `Host` header, Jetty will pick the correct certificate from the KeyStore based on the SNI received from the client, and accept the request.

Accepting the request does not mean that the request is responded with an HTTP 200 OK, but just that the request passed successfully the SNI checks and will be processed by the server.
If the request URI is for a resource that does not exist, the response will likely be a 404 Not Found.

You may modify the default values of the SNI properties if you want stricter control over old/broken TLS clients or bad HTTP requests.

[[proxy]]
== Jetty Behind a Load Balancer or Reverse Proxy

You may need to configure one or more Jetty instances behind an _intermediary_, typically a load balancer such as https://haproxy.org[HAProxy], or a reverse proxy such as https://httpd.apache.org[Apache HTTP Server] or https://nginx.org[Nginx].

[plantuml]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false
skinparam padding 5

scale 1.5

rectangle client
rectangle proxy
rectangle "Jetty" as jetty1
rectangle "Jetty" as jetty2

client -- proxy
proxy -- jetty1
proxy -- jetty2
----

[WARNING]
====
HAProxy can communicate either HTTP/1.1 or HTTP/2 to backend servers such as Jetty.

Apache HTTP Server and Nginx can only speak HTTP/1.1 to backend servers such as Jetty, and have no plans to support HTTP/2 towards backend servers.
====

In these setups, typically the proxy performs TLS offloading, and the communication with backend servers happens in clear-text.
It is possible, however, to configure the proxy so that all the bytes arriving from the client are tunnelled opaquely to the backend Jetty server (that therefore needs to perform the TLS offloading) and vice versa the bytes arriving from the Jetty server are tunnelled opaquely to the client.

Also in these setups, the TCP/IP connection terminating on the Jetty servers does not originate from the client, but from the proxy, so that the remote IP address and port number may be reported incorrectly in backend server logs, or worse applications may not work because they need to be able to differentiate different clients based on the client IP address.

For this reason, intermediaries typically implement at least one of several _de facto_ standards to communicate information about the original client connection to the backend Jetty server.

Jetty supports two methods to process client information sent by intermediaries:

* The `Forwarded` HTTP header, defined in https://tools.ietf.org/html/rfc7239[RFC 7239] and replacing the old `X-Forwarded-*` headers, defined in <<proxy-forwarded,this section>>.
* The https://www.haproxy.org/download/2.2/doc/proxy-protocol.txt[Proxy Protocol], defined in <<proxy-protocol,this section>>.

In both methods, web applications asking for the remote client IP address will receive the remote client IP address as specified by the client information sent by the intermediary, not the physical IP address of TCP connection with the intermediary.
Likewise, web applications asking for the remote client IP port will receive the remote client IP port as specified by the client information sent by the intermediary.
Finally, web applications asking whether the remote request was secure will receive whether the remote client made a secure request using the `https` scheme as specified by the client information sent by the intermediary.

[[proxy-forwarded]]
=== Configuring the Forwarded Header

The `Forwarded` HTTP header is added by the intermediary with information about the client and the client request, for example:

----
GET / HTTP/1.1
Host: domain.com
Forwarded: for=2.36.72.144:21216;proto=https
----

In the example above, the intermediary added the `Forwarded` header specifying that the client remote address is `2.36.72.144:21216` and that the request was made with the `https` scheme.

Let's assume you have already configured Jetty with the HTTP/1.1 protocol with the following command (issued from within the `$JETTY_BASE` directory):

----
$ java -jar $JETTY_HOME/start.jar --add-modules=http
----

Support for the `Forwarded` HTTP header (and its predecessor `X-Forwarded-*` headers) is enabled with the `forwarded` Jetty module:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=forwarded
----

[jetty%nowrap]
....
[jetty]
setupArgs=--add-modules=http
args=--add-modules=forwarded
....

With the `forwarded` Jetty module enabled, Jetty interprets the `Forwarded` header and makes its information available to web applications.

For further information about configuring the `forwarded` Jetty module, see xref:modules/standard.adoc#forwarded[this section].

[[proxy-protocol]]
=== Configuring the Proxy Protocol

The https://www.haproxy.org/download/2.2/doc/proxy-protocol.txt[Proxy Protocol] is the _de facto_ standard, introduced by https://haproxy.org[HAProxy], to communicate client information to backend servers via the TCP connection, rather than via HTTP headers.

The information about the client connection is sent as a small data frame on each newly established connection.
This mechanism is therefore independent of any protocol, so it can be used for TLS, HTTP/1.1, HTTP/2, etc.

[NOTE]
====
There are 2 versions of the proxy protocol: v1 and v2, both supported by Jetty.

Proxy protocol v1 is human readable, but it only carries information about the client TCP connection (IP address and IP port).

Proxy protocol v2 has a binary format, carries the information about the client TCP connection, and can carry additional arbitrary information encoded in pairs `(type, value)` where `type` is a single byte that indicates the value's meaning, and `value` is a variable length byte array that can encode user-defined data.
====

Support for the proxy protocol can be enabled for the clear-text connector or for the secure connector (or both).

Let's assume you have already configured Jetty with the HTTP/1.1 clear-text protocol with the following command (issued from within the `$JETTY_BASE` directory):

----
$ java -jar $JETTY_HOME/start.jar --add-modules=http
----

To enable proxy protocol support for the clear-text connector, enable the `proxy-protocol` Jetty module:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=proxy-protocol
----

[jetty%nowrap]
....
[jetty]
setupArgs=--add-modules=http
args=--add-modules=proxy-protocol
....

Starting Jetty yields:

----
$ java -jar $JETTY_HOME/start.jar
----

[jetty%nowrap]
....
[jetty]
args=--module=proxy-protocol
highlight=(\{.*:8080})
....

Note how in the example above the list of protocols for the clear-text connector is first `proxy` and then `http/1.1`.
For every new TCP connection, Jetty first interprets the proxy protocol bytes with the client information; after this initial proxy protocol processing, Jetty interprets the incoming bytes as HTTP/1.1 bytes.

Enabling proxy protocol support for the secure connector is similar.

Let's assume you have already configured Jetty with the HTTP/1.1 secure protocol and the test KeyStore with the following command (issued from within the `$JETTY_BASE` directory):

----
$ java -jar $JETTY_HOME/start.jar --add-modules=https,test-keystore
----

Enable the `proxy-protocol-ssl` Jetty module with the following command (issued from within the `$JETTY_BASE` directory):

----
$ java -jar $JETTY_HOME/start.jar --add-modules=proxy-protocol-ssl
----

[jetty%nowrap]
....
[jetty]
setupArgs=--add-modules=https
args=--add-modules=proxy-protocol-ssl
....

Starting Jetty yields:

----
$ java -jar $JETTY_HOME/start.jar
----

[jetty%nowrap]
....
[jetty]
setupArgs=--add-modules=https,test-keystore,proxy-protocol-ssl
highlight=(\{.*:8443})
....

Note how in the example above the list of protocols for the secure connector is first `proxy`, then `ssl` and then `http/1.1`.

[[proxy-haproxy]]
=== HAProxy and Jetty with HTTP/1.1 and HTTP/2

https://haproxy.org[HAProxy] is an open source solution that offers load balancing and proxying for TCP and HTTP based application, and can be used as a replacement for Apache or Nginx when these are used as reverse proxies.

The deployment proposed here has HAProxy playing the role that Apache and Nginx usually do: to perform the TLS offloading (that is, decrypt incoming bytes and encrypt outgoing bytes) and then forwarding the now clear-text traffic to a backend Jetty server, speaking either HTTP/1.1 or HTTP/2.
Since HAProxy's TLS offloading is based on OpenSSL, it is much more efficient than the Java implementation shipped with OpenJDK.

After you have installed HAProxy on your system, you want to configure it so that it can perform TLS offloading.

HAProxy will need a single file containing the X509 certificates and the private key, all in https://en.wikipedia.org/wiki/X.509[PEM format], with the following order:

1.  The site certificate; this certificate's Common Name refers to the site domain (for example: CN=*.webtide.com) and is signed by Certificate Authority #1.
2.  The Certificate Authority #1 certificate; this certificate may be signed by Certificate Authority #2.
3.  The Certificate Authority #2 certificate; this certificate may be signed by Certificate Authority #3; and so on until the Root Certificate Authority.
4.  The Root Certificate Authority certificate.
5.  The private key corresponding to the site certificate.

Refer to the xref:keystore/index.adoc[section about KeyStores] for more information about generating the required certificates and private key.

Now you can create the HAProxy configuration file (in Linux it's typically `/etc/haproxy/haproxy.cfg`).
This is a minimal configuration:

.haproxy.cfg
[source]
----
global
tune.ssl.default-dh-param 1024

defaults
timeout connect 10000ms
timeout client 60000ms
timeout server 60000ms

frontend fe_http <1>
mode http
bind *:80
# Redirect to https
redirect scheme https code 301

frontend fe_https <2>
mode tcp
bind *:443 ssl no-sslv3 crt /path/to/domain.pem ciphers TLSv1.2 alpn h2,http/1.1
default_backend be_http

backend be_http <3>
mode tcp
server domain 127.0.0.1:8282 send-proxy-v2
----
<1> The `fe_http` front-end accepts connections on port 80 and redirects them to use the `https` scheme.
<2> The `fe_https` front-end accepts connections on port 443, and it is where the TLS decryption/encryption happens.
You must specify the path to the PEM file containing the TLS key material (the `crt /path/to/domain.pem` part), the ciphers that are suitable for HTTP/2 (`ciphers TLSv1.2`), and the ALPN protocols supported (`alpn h2,http/1.1`).
This front-end then forwards the now decrypted bytes to the backend in `mode tcp`.
The `mode tcp` says that HAProxy will not try to interpret the bytes but instead opaquely forwards them to the backend.
<3> The `be_http` backend will forward (again in `mode tcp`) the clear-text bytes to a Jetty connector that talks clear-text HTTP/2 and HTTP/1.1 on port 8282.
The `send-proxy-v2` directive sends the proxy protocol v2 bytes to the backend server.

On the Jetty side, you need to enable the following modules:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=proxy-protocol,http2c,http,deploy
----

You need to specify the host (`127.0.0.1`) and port (`8282`) you have configured in HAProxy when you start Jetty:

----
$ java -jar $JETTY_HOME/start.jar jetty.http.host=127.0.0.1 jetty.http.port=8282
----

[NOTE]
====
You want the Jetty connector that listens on port `8282` to be available only to HAProxy, and not to remote clients.

For this reason, you want to specify the `jetty.http.host` property on the command line (or in `$JETTY_BASE/start.d/http.ini` to make this setting persistent) to bind the Jetty connector only on the loopback interface (`127.0.0.1`), making it available to HAProxy but not to remote clients.

If your Jetty instance runs on a different machine and/or on a different (sub)network, you may want to adjust both the back-end section of the HAProxy configuration file and the `jetty.http.host` property to match accordingly.
====

With this configuration for HAProxy and Jetty, browsers supporting HTTP/2 will connect to HAProxy, which will decrypt the traffic and send it to Jetty.
Likewise, HTTP/1.1 clients will connect to HAProxy, which will decrypt the traffic and send it to Jetty.

The Jetty connector, configured with the `http2c` and the `http` modules is able to distinguish whether the incoming bytes are HTTP/2 or HTTP/1.1 and will handle the request accordingly.

The response is relayed back to HAProxy, which will encrypt it and send it back to the remote client.

This configuration offers you efficient TLS offloading, HTTP/2 support and transparent fallback to HTTP/1.1 for clients that don't support HTTP/1.1.

---

### Documentation Jetty Modules Operations Guide Pages Quickstart Index (`documentation/jetty/modules/operations-guide/pages/quickstart/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Faster Jakarta EE Web Application Deployment

The auto discovery features of the Servlet specification can make deployments slow and uncertain.

Auto discovery of Jakarta EE web application configuration can be useful during the development as it allows new features and frameworks to be enabled simply by dropping in a jar file.
However, for production deployment, the need to scan the contents of many jars can have a significant impact at startup time.

The `{ee-all}-quickstart` modules allows a Jakarta EE web application to be pre-scanned, making startup predictable and faster.
During the pre-scanning all declarative configuration (from `web.xml`, `web-fragment.xml` and annotations) are encoded into an effective `web.xml`, called `WEB-INF/quickstart-web.xml`, which can be inspected to understand what will be deployed.

[NOTE]
====
Programmatic configuration is _not_ encoded into the generated `quickstart-web.xml` file.
====

With the `{ee-all}-quickstart` modules, web applications that took many seconds to scan and deploy can now be deployed in a few hundred milliseconds.

== Enabling

You can enable the `{ee-current}-quickstart` module in this way:

[,shell,subs=+attributes]
----
$ cd $JETTY_BASE
$ java -jar $JETTY_HOME/start.jar --add-modules={ee-current}-quickstart
----

The `$JETTY-BASE/start.d/quickstart.ini` file contains these configurable parameters:

jetty.quickstart.mode::
The values are:
GENERATE:::
In this mode, the `quickstart-web.xml` file are generated and then the Jetty `Server` is stopped.
Use this mode first before changing to either `AUTO` or `QUICKSTART`.
AUTO:::
Allows to deploy the Jakarta EE web application either with or without a `quickstart-web.xml` file.
If the `quickstart-web.xml` file is present, then it will be used, otherwise the web application is deployed normally.
QUICKSTART:::
In this mode, if the `quickstart-web.xml` file is not present, then the web application deployment will fail.

jetty.quickstart.origin::
Use this parameter to set the name of the attribute in the `quickstart-web.xml` file that contains the origin of each element.
Knowing the descriptor or annotation from which each element derives can be useful for debugging.
Note that the origin attribute does not conform to the web xml schema, so if you deploy with xml validation, you'll see errors.
It is probably best to do a few trial runs with the attribute set, then turn it off for final generation.

jetty.quickstart.xml::
Use this parameter to change the name of the generated file.
By default this is `quickstart-web.xml` in the Jakarta EE web application's `WEB-INF` directory.
The file named by this parameter will always be interpreted relative to the `WEB-INF` directory.

If your Jakarta EE web application is a `+*.war+` file, you will need to either first unpack it yourself, or use a Jetty context XML file (or code equivalent) that calls `WebAppContext.setExtractWAR(true)`.
If you allow Jetty to do the unpacking, it will use the usual mechanisms to find the location where to unpack.
Note that by default Jetty unpacks to a temporary location which is _not_ reused between executions.
You must either specify the directory where to unpack, or make a `work` directory in your `$JETTY_BASE` to ensure the unpacked war is preserved and reused across restarts.

---

### Documentation Jetty Modules Operations Guide Pages Security Configuring Form Size (`documentation/jetty/modules/operations-guide/pages/security/configuring-form-size.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

[[limit-form-content]]
= Limiting Form Content

Forms can be a vector for denial-of-service attacks, like explained in xref:programming-guide:security/configuring-form-size.adoc[this section] of the Programming Guide.

== Configuring Form Limits for a Web Application

To configure the form limits for a Jakarta EE web application, the `WebAppContext` instance can be configured from a Jetty context XML file or from a `WEB-INF/jetty-{ee-all}-web.xml` file:

[,xml,subs=attributes+]
----
<Configure class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext">
  ...
  <Set name="maxFormContentSize">200000</Set>
  <Set name="maxFormKeys">200</Set>
</Configure>
----

Setting the form limits can also be done, for both Jakarta EE web applications and Jetty Core web applications, by setting the following `Context` attributes.

* `org.eclipse.jetty.server.Request.maxFormKeys`
* `org.eclipse.jetty.server.Request.maxFormContentSize`

For Jakarta EE web applications, these attributes can be set in `web.xml` via the `<context-param>` element.

For Jakarta EE and Jetty web applications, these attributes can be set in the Jetty context XML file.

For Jakarta EE web applications:

[,xml,subs=attributes+]
----
<Configure class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext">
  ...
  <Call name="setAttribute">
    <Arg>org.eclipse.jetty.server.Request.maxFormKeys</Arg>
    <Arg>128</Arg>
  </Call>
</Configure>
----

For Jetty Core web applications:

[,xml]
----
<Configure class="org.eclipse.jetty.coreapp.CoreAppContext">
  ...
  <Call name="setAttribute">
    <Arg>org.eclipse.jetty.server.Request.maxFormKeys</Arg>
    <Arg>128</Arg>
  </Call>
</Configure>
----

---

### Documentation Jetty Modules Operations Guide Pages Security Jaas Support (`documentation/jetty/modules/operations-guide/pages/security/jaas-support.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= JAAS

JAAS implements a Java version of the standard Pluggable Authentication Module (PAM) framework.

JAAS can be used for two purposes:

* for authentication of users, to reliably and securely determine who is currently executing Java code, regardless of whether the code is running as an application, an applet, a bean, or a servlet
* for authorization of users to ensure they have the access control rights (permissions) required to do the actions performed

JAAS authentication is performed in a pluggable fashion.
This permits applications to remain independent from underlying authentication technologies.
New or updated authentication technologies can be plugged under an application without requiring modifications to the application itself.

See Java Authentication and Authorization Service (JAAS) http://java.sun.com/javase/7/docs/technotes/guides/security/jaas/JAASRefGuide.html[Reference Guide] for more information about JAAS.

The Jetty JAAS support aims to dictate as little as possible whilst providing a sufficiently flexible infrastructure to allow users to drop either one of the <<loginmodules,JAAS Login Modules that ships with Jetty>>, or their
own custom https://docs.oracle.com/javase/7/docs/api/javax/security/auth/spi/LoginModule.html[LoginModule]s.

[[configuration]]
== Configuration

[[module]]
=== The `jaas` module

Enable the `jaas` module.

[source]
----
include::{jetty-home}/modules/jaas.mod[]
----

The configurable items in the resulting `$JETTY_BASE/start.d/jaas.ini` file are:

jetty.jaas.login.conf::
This is the location of the file that will be referenced by the system property `java.security.auth.login.config`: Jetty sets this system property for you based on the value of this property.
The value of this property is assumed to be _relative to ``$JETTY_BASE``_.
The default value is `etc/login.conf`, which resolves to  `$JETTY_BASE/etc/login.conf`.
If you don't want to put your login module configuration file here, you can change this property to point to where it is.

See more about the contents of this file in the <<loginconf,Configuring JAAS>> section.

[[webapp]]
=== Configure the Jakarta EE web application for JAAS

The `<realm-name>` in `web.xml` will be used to identify the `org.eclipse.jetty.security.jaas.JAASLoginService` declaration that integrates JAAS with Jetty.

For example, this `web.xml` contains a realm called `Test JAAS Realm`:

[,xml]
----
<login-config>
  <auth-method>FORM</auth-method>
  <realm-name>Test JAAS Realm</realm-name> 
  <form-login-config>
    <form-login-page>/login/login</form-login-page>
    <form-error-page>/login/error</form-error-page>
  </form-login-config>
</login-config>
----
<1> The name of the realm, which must be _identical_ to the name of an `org.eclipse.jetty.security.jaas.JAASLoginService` declaration.

We now need to declare an `org.eclipse.jetty.security.jaas.JAASLoginService` that references the realm name of `Test JAAS Realm`.
Here's an example of a suitable XML snippet:

[,xml]
----
<New class="org.eclipse.jetty.security.jaas.JAASLoginService">
  <Set name="Name">Test JAAS Realm</Set> 
  <Set name="LoginModuleName">xyz</Set> 
</New>
----
<1> The name is the _same_ as that declared in the `<realm-name>` in `web.xml`.
<2> The name that identifies a set of `javax.security.auth.spi.LoginModule` configurations that comprise the <<loginconf,JAAS config file>> identified in the `jetty.jaas.login.conf` property of the <<module,`jaas` module>>.

The `org.eclipse.jetty.security.jaas.JAASLoginService` can be declared in a couple of different places, pick whichever suits your purposes best:

* If you have more than one web application that you would like to use the same security infrastructure, then you can declare your `org.eclipse.jetty.security.jaas.JAASLoginService` as a bean that is added to the `org.eclipse.jetty.server.Server`.
The file in which you declare this needs to be on Jetty's execution path.
The recommended procedure is to create a file in your `$JETTY_BASE/etc` directory and then ensure it is on the classpath either by adding it to the Jetty xref:start/index.adoc[start command line], or more conveniently to a xref:modules/custom.adoc[custom module].
+
Here's an example of this type of XML file:
+
[,xml]
----
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
  <Call name="addBean">
    <Arg>
      <New class="org.eclipse.jetty.security.jaas.JAASLoginService">
        <Set name="name">Test JAAS Realm</Set>
        <Set name="LoginModuleName">xyz</Set>
      </New>
    </Arg>
  </Call>
</Configure>
----

* Alternatively, if you want to use JAAS with a specific web application only, you declare your `org.eclipse.jetty.security.jaas.JAASLoginService` in a Jetty context XML file specific to that web application:
+
[,xml,subs=+attributes]
----
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">
<Configure class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext">
  <Set name="securityHandler">
    <New class="org.eclipse.jetty.{ee-current}.servlet.security.ConstraintSecurityHandler">
     <Set name="loginService">
       <New class="org.eclipse.jetty.security.jaas.JAASLoginService">
         <Set name="name">Test JAAS Realm</Set>
         <Set name="loginModuleName">xyz</Set>
       </New>
     </Set>
    </New>
  </Set>
</Configure>
----

[[loginconf]]
=== Configure JAAS

We now need to set up the contents of the file we specified as the `jetty.jaas.login.conf` property when we <<module,configured the `jaas` module>>.
Refer to the https://docs.oracle.com/javase/7/docs/api/javax/security/auth/login/Configuration.html[syntax rules] of this file for a full description.

Remembering the example we set up <<webapp,previously>>, the contents of the `$JETTY_BASE/etc/login.conf` file could look as follows:

[source]
----
xyz  { <1>
  com.acme.SomeLoginModule required debug=true; <2>
  com.other.OtherLoginModule optional; <3>
};
----
<1> The name of the configuration _exactly_ as specified in your `org.eclipse.jetty.security.jaas.JAASLoginService` declaration.
<2> The first `LoginModule` declaration, containing the classname of the `LoginModule` and its configuration properties.
<3> A second `LoginModule` declaration.
You can provide as many `LoginModule` alternatives as you like, with a minimum of one.
Refer to the https://docs.oracle.com/javase/7/docs/api/javax/security/auth/login/Configuration.html[JAAS documentation] for more information on the standard configuration properties, and how JAAS interprets this file.

[[loginmodules]]
== Provided LoginModules

* link:{javadoc-url}/org/eclipse/jetty/security/jaas/spi/JDBCLoginModule.html[`org.eclipse.jetty.security.jaas.spi.JDBCLoginModule`]
* link:{javadoc-url}/org/eclipse/jetty/security/jaas/spi/PropertyFileLoginModule.html[`org.eclipse.jetty.security.jaas.spi.PropertyFileLoginModule`]
* link:{javadoc-url}/org/eclipse/jetty/security/jaas/spi/DataSourceLoginModule.html[`org.eclipse.jetty.security.jaas.spi.DataSourceLoginModule`]
* link:{javadoc-url}/org/eclipse/jetty/security/jaas/spi/LdapLoginModule.html[`org.eclipse.jetty.security.jaas.ldap.LdapLoginModule`]

[[og-password]]
[NOTE]
====
Passwords can be obfuscated using the xref:tools/index.adoc#password[Jetty Password tool].
====

=== JDBCLoginModule

The `org.eclipse.jetty.security.jaas.spi.JDBCLoginModule` stores user passwords and roles in a database accessed via JDBC calls.
You can configure the JDBC connection information, as well as the names of the table and columns storing the username and credential, and the names of the table and columns storing the roles.

Here is an example <<loginconf,login module configuration file>> entry for it using an HSQLDB driver:

[source]
----
jdbc { <1>
  org.eclipse.jetty.security.jaas.spi.JDBCLoginModule required <2><3>
  dbUrl="jdbc:hsqldb:." <4>
  dbUserName="sa" <5>
  dbDriver="org.hsqldb.jdbcDriver" <6>
  userTable="myusers" <7>
  userField="myuser" <8>
  credentialField="mypassword" <9>
  userRoleTable="myuserroles" <10>
  userRoleUserField="myuser" <11>
  userRoleRoleField="myrole"; <12>
};
----
<1> The name of the configuration.
<2> The name of the `LoginModule` class.
<3> A standard JAAS flag making successful authentication via this `LoginModule` mandatory.
<4> The JDBC url used to connect to the database.
<5> The name of the JDBC user to use for the connection.
<6> The name of the JDBC Driver class.
<7> The name of the table holding the user authenication information.
<8> The name of the column holding the user name.
<9> The name of the column holding the user credential.
<10> The name of the table holding the user authorization information.
<11> The name of the column holding the user name.
<12> The name of the column holding the user role.

The properties *7*-*12* are used to format the following queries:

[,sql]
----
select <credentialField> from <userTable> where <userField>=?
select <userRoleRoleField> from <userRoleTable> where <userRoleUserField>=?
----

Credential and role information is lazily read from the database when a previously unauthenticated user requests authentication.
Note that this information is _only_ cached for the length of the authenticated session.
When the user logs out or the session expires, the information is flushed from memory.

Note that passwords can be stored in the database in plain text or encoded formats -- see the note on "Passwords/Credentials" above.

=== DataSourceLoginModule

Similar to the `org.eclipse.jetty.security.jaas.spi.JDBCLoginModule`, but using a `javax.sql.DataSource` to connect to the database instead of a JDBC driver.
The `javax.sql.DataSource` is obtained at runtime by performing a JNDI lookup on `java:comp/env/$\{dnJNDIName}`.

A sample login module configuration for this `LoginModule`:

[source]
----
ds { <1>
  org.eclipse.jetty.security.jaas.spi.DataSourceLoginModule required <2><3>
  dbJNDIName="ds" <4>
  userTable="myusers" <5>
  userField="myuser" <6>
  credentialField="mypassword" <7>
  userRoleTable="myuserroles" <8>
  userRoleUserField="myuser" <9>
  userRoleRoleField="myrole"; <10>
};
----
<1> The name of the configuration.
<2> The name of the `LoginModule` class.
<3> A standard JAAS flag making successful authentication via this `LoginModule` mandatory.
<4> The JNDI name, relative to `java:comp/env/` to lookup to obtain the `javax.sql.DataSource`.
<5> The name of the table holding the user authenication information.
<6> The name of the column holding the user name.
<7> The name of the column holding the user credential.
<8> The name of the table holding the user authorization information.
<9> The name of the column holding the user name.
<10> The name of the column holding the user role.

=== PropertyFileLoginModule

With this login module implementation, the authentication and role information is read from a property file.

[source]
----
props { <1>
  org.eclipse.jetty.security.jaas.spi.PropertyFileLoginModule required  <2><3>
  file="/somewhere/somefile.props"; <4>
};
----
<1> The name of the configuration.
<2> The name of the `LoginModule` class.
<3> A standard JAAS flag making successful authentication via this `LoginModule` mandatory.
<4> The location of a properties file containing the authentication and authorization information.

The property file must be of the format:

[source]
----
<username>: <password> [,<rolename> ...]
----

Here's an example:

----
fred: OBF:1xmk1w261u9r1w1c1xmq,user,admin
harry: changeme,user,developer
tom: MD5:164c88b302622e17050af52c89945d44,user
dick: CRYPT:adpexzg3FUZAk,admin
----

The contents of the file are fully read in and cached in memory the first time a user requests authentication.

=== LdapLoginModule

The `org.eclipse.jetty.security.jaas.spi.LdapLoginModule` uses LDAP to access authentication and authorization information stored in a directory.
The LDAP connection information and structure of the authentication/authorization data can be configured.

Here's an example:

[source]
----
example  { <1>
  org.eclipse.jetty.security.jaas.spi.LdapLoginModule required  <2><3>
  contextFactory="com.sun.jndi.ldap.LdapCtxFactory" <4>
  hostname="ldap.example.com" <5>
  port="389" <6>
  bindDn="cn=Directory Manager" <7>
  bindPassword="directory" <8>
  authenticationMethod="simple" <9>
  useLdaps="true" <10>
  userBaseDn="ou=people,dc=alcatel" <11>
  userRdnAttribute="uid" <12>
  userIdAttribute="cn" <13>
  userPasswordAttribute="userPassword" <14>
  userObjectClass="inetOrgPerson" <15>
  roleBaseDn="ou=groups,dc=example,dc=com" <16>
  roleNameAttribute="cn" <17>
  roleMemberAttribute="uniqueMember" <18>
  roleObjectClass="groupOfUniqueNames"; <19>
  forceBindingLogin="false" <20>
  debug="false" <21>
};
----
<1> The name of the configuration.
<2> The name of the `LoginModule` class.
<3> A standard JAAS flag making successful authentication via this `LoginModule` mandatory.
<4> The name of the context factory to use for the LDAP connection.
<5> The hostname for the LDAP connection. Optional.
<6> The port for the LDAP connection. Optional.
<7> The caller security Principal. Optional.
<8> The caller security credential. Optional.
<9> The security level for the LDAP connection environment. Optional.
<10> If true, use `ldaps` instead of `ldap` for the connection url.
<11> The distinguished name of the directory to search for user information.
<12> The name of the attribute for the user roles.
<13> The name of the attribute for the user id.
<14> The name of the attribute for the user password.
<15> The `ObjectClass` for users.
<16> The distinguished name of the directory to search for role information.
<17> The name of the attribute for roles.
<18> The name of the attribute storing the user for the roles `ObjectClass`.
<19> The name of the `ObjectClass` for roles.
<20> If true, the authentication proceeds on the basis of a successful LDAP binding using the username and credential provided by the user.
If false, then authentication proceeds based on username and password information retrieved from LDAP.
<21> If true, failed login attempts are logged on the server.

---

### Documentation Jetty Modules Operations Guide Pages Security Jaspi Support (`documentation/jetty/modules/operations-guide/pages/security/jaspi-support.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= JASPI

Enabling this module allows Jetty to utilize authentication modules that implement the Jakarta Authentication (JASPI) specification. JASPI provides an SPI (Service Provider Interface) for pluggable, portable, and standardized authentication modules. Compatible modules are portable between servers that support the JASPI specification. This module provides a bridge from Jakarta Authentication to the Jetty Security framework.

Only modules conforming to the "Servlet Container Profile" with the ServerAuthModule interface within the https://jakarta.ee/specifications/authentication/2.0/jakarta-authentication-spec-2.0.pdf[JakartaAuthentication] are supported. These modules must be configured before start-up.  Operations for runtime registering or de-registering authentication modules are not supported.

[[configuration]]
== Configuration

[[module]]
=== The `jaspi` module

Enable the `jaspi` module:

----
include::{jetty-home}/modules/ee11-jaspi.mod[]
----

[[xml]]
=== Configure JASPI

Activate either the `ee9-jaspi` or `{ee-current}-jaspi` module, whichever matches your EE platform version.

[source,subs=attributes+]
----
$ java -jar $JETTY_HOME/start.jar --add-modules={ee-current}-jaspi
----

You can then register a `AuthConfigProvider` onto the static `AuthConfigFactory` obtained with `AuthConfigFactory.getFactory()`. This registration can be done in the XML configuration file which will be copied to `$JETTY_BASE/etc/jaspi/jaspi-authmoduleconfig.xml` when the module is enabled.

==== JASPI Demo
The `ee9-jaspi-demo` and `{ee-current}-jaspi-demo` modules illustrate setting up HTTP Basic Authentication using the EE9 and {ee-current-caps} Jakarta Authentication modules that come packaged with Jetty.

The following example uses Jetty's {ee-current-caps} implementation of `AuthConfigProvider` to register a `ServerAuthModule` directly.

[,xml]
----
include::{jetty-home}/etc/jaspi/jetty-ee11-jaspi-demo.xml[]
----

Other custom or 3rd party modules that are compatible with the `ServerAuthModule` interface in JASPI can be registered in the same way.

=== Integration with Jetty Authentication Mechanisms

To integrate with Jetty authentication mechanisms you must add a `LoginService` to your context. The `LoginService` provides a way for you to obtain a `UserIdentity` from a username and credentials. JASPI can interact with this Jetty `LoginService` by using the `PasswordValidationCallback`.

The `CallerPrincipalCallback` and `GroupPrincipalCallback` do not require use of a Jetty `LoginService`. The principal from the `CallerPrincipalCallback` will be used directly with the `IdentityService` to produce a `UserIdentity`.

=== Replacing the Jetty DefaultAuthConfigFactory

Jetty provides an implementation of the `AuthConfigFactory` interface which is used to register `AuthConfigProviders`. This can be replaced by a custom implementation by adding a custom module which provides `auth-config-factory`.
This custom module must reference an XML file which sets a new instance of the `AuthConfigFactory` with the static method `AuthConfigFactory.setFactory()`.
For an example of this see the `{ee-current}-jaspi-default-auth-config-factory` module, which provides the default implementation used by Jetty.

----
include::{jetty-home}/modules/ee11-jaspi-default-auth-config-factory.mod[]
----

---

### Documentation Jetty Modules Operations Guide Pages Security Openid Support (`documentation/jetty/modules/operations-guide/pages/security/openid-support.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

[[openid-support]]
= OpenID Support

A more general discussion about OpenID and its support in Jetty is available in the xref:programming-guide:security/openid-support.adoc[programming guide section].

== OpenID Provider Configuration

To enable OpenID support, you need to enable the xref:operations-guide:modules/standard.adoc#openid[`openid` module]:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=openid
----

To configure OpenID Authentication with Jetty you will need to specify the OpenID Provider's issuer identifier (a case-sensitive URL) and the OAuth 2.0 Client ID and Client Secret.

If the OpenID Provider does not allow metadata discovery you will also need to specify the token endpoint and authorization endpoint of the OpenID Provider.

These values can be set as properties in `$JETTY_BASE/start.d/openid.ini` file.
Refer to the xref:modules/standard.adoc#openid[`openid`] Jetty module for the list of configurable properties.

This is an example of an `openid.ini` file which uses discovery of the OpenID endpoints:

----
## The OpenID Identity Provider's issuer ID (the entire URL *before* ".well-known/openid-configuration")
jetty.openid.provider=https://id.example.com/

## The Client Identifier
jetty.openid.clientId=test1234

## The Client Secret
jetty.openid.clientSecret=XT_Mafv_aUCGheuCaKY8P
----

== Jakarta EE Web Application Specific Configuration in `web.xml`

The Jakarta EE web application's `web.xml` file needs some specific configuration to use OpenID.

There must be a `<login-config>` element with an `<auth-method>` value of `OPENID`, and a `<realm-name>` value of the exact URL string used to set the OpenID Provider.

To set the error page, you must set a `<context-param>` named `org.eclipse.jetty.security.openid.error_page` whose value should be a path relative to the web application where authentication errors should be redirected.

For example:

[,xml,subs=attributes+]
----
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_1.xsd"
         version="6.1">
  ...
  <login-config>
    <auth-method>OPENID</auth-method>
    <realm-name>https://accounts.google.com</realm-name>
  </login-config>

  <context-param>
    <param-name>org.eclipse.jetty.security.openid.error_page</param-name>
    <param-value>/error</param-value>
  </context-param>
  ...
</web-app>
----

== Authorization with Security Roles

If security roles are required, they can be configured through a wrapped `LoginService` which is deferred to for role information by the `OpenIdLoginService`, and assigns security roles to users.

You can configure the wrapped `LoginService` by modifying the `$JETTY_BASE/etc/openid-baseloginservice.xml` file.

You can further configure whether to only authenticate users known to the wrapped `LoginService` by configuring the property `jetty.openid.authenticateNewUsers` in the `$JETTY_BASE/start.d/openid.ini` file.

== Supporting Multiple OpenID Providers

By default, Jetty defines one OpenID Provider that you can configure using the properties defined in the `$JETTY_BASE/start.d/openid.ini` file.

You can support multiple OpenID Providers by creating a custom Jetty module (as described in xref:operations-guide:modules/custom.adoc[this section]).
The custom Jetty module XML file should add an additional `OpenIdConfiguration` as a bean on the `Server` instance, for the additional OpenID Provider.

If there are multiple OpenID configuration instances in the `Server`, then the `OpenIdAuthenticationFactory` will select the one with an `issuer` matching the `<realm-name>` of the `<login-config>` element in the `web.xml` of a given web application.

---

### Documentation Jetty Modules Operations Guide Pages Server Index (`documentation/jetty/modules/operations-guide/pages/server/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Jetty Server

The Jetty `Server` object is the central component that links protocol connectors to web applications.

The `Server` component is defined by the xref:modules/standard.adoc#server[`server` Jetty module], that in turn depends on other Jetty modules that provide key functionalities, in particular:

* <<logging,Logging>>
* xref:modules/standard.adoc#bytebufferpool[`ByteBuffer` pooling]
* <<threadpool,`Thread` pooling>>

[[logging]]
== Logging

You can configure two types of logging in Jetty: _server logging_ and _request logging_.

* <<logging-server,Server logging>> refers to the console output produced by Jetty itself.
* <<logging-request,Request logging>> refers to the information that Jetty can capture about incoming HTTP requests and responses.

[[logging-server]]
=== Server Logging

Jetty uses the http://slf4j.org/[SLF4J] API for its logging.
SLF4J is a generic abstraction layer that is supported by many different logging frameworks (or SLF4J _bindings_).

Jetty provides a default binding via the `jetty-slf4j-impl` Maven artifact, but you can plug in the SLF4J _binding_ <<logging-server-custom,of your choice>> provided by other logging frameworks.

Jetty's server logging is enabled by default with the `logging` Jetty module.
You typically won't have to enable the `logging` module directly, since it is a transitive dependency of the `server` module, and thus pulled in by many of the most commonly used modules.

The `logging` module is a xref:modules/index.adoc#names[_virtual_ module] and its default implementation is provided by the `logging-jetty` Jetty module, which uses the Jetty SLF4J binding.

[[logging-server-default]]
==== Default Configuration

Jetty's default SLF4J binding uses an _appender_ (`org.eclipse.jetty.logging.StdErrAppender`) to format a logging message with metadata (like a timestamp) before sending it to `System.err`.
The default `StdErrAppender` format is:

----
<datetime>:<level>:<logger name>:<thread name>:<message>
----

where `<datetime>` is a timestamp with the format `yyyy-MM-dd HH:mm:ss.SSS`.

You can configure the appender via a file named `jetty-logging.properties`, which must be found in the server xref:start/index.adoc#start-class-path[class-path].
When you enable the `logging-jetty` module -- either directly or by transitive dependency, as in the following example -- Jetty automatically generates a `jetty-logging.properties` file in `$JETTY_BASE/resources/`:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=http
----

[jetty%nowrap]
....
[jetty]
args=--add-modules=http
highlight=(logging-jetty *transitively enabled|copy.*\.properties)
....

You can specify the following configuration options in `jetty-logging.properties`:

`org.eclipse.jetty.LEVEL=`<logging level>::
Sets the logging level for the logger tree based at `org.eclipse.jetty`.
You can specify any of the usual SLF4J logging levels -- `TRACE`, `DEBUG`, `INFO` (default), `WARN` and `ERROR` -- plus two additional levels: `ALL` (an alias for `TRACE`) and `OFF` (disables logging entirely).
You can also configure a default logging level for specific loggers, or arbitrary logger trees:
* `com.example.MyComponent.LEVEL=DEBUG` (sets logging level of logger `com.example.MyComponent` to `DEBUG`)
* `com.example.LEVEL=DEBUG` (sets logging level of tree `+com.example.*+` to `DEBUG`)

`com.example.STACKS=`<boolean>::
Specifies whether to hide stack traces for some arbitrary logger tree `+com.example.*+`.
The exception type and message are logged normally; only stack traces are hidden.
Default value is `false`

`org.eclipse.jetty.logging.appender.NAME_CONDENSE=`<boolean>::
Specifies whether to condense logger names, so that for example `org.eclipse.jetty.util.QueuedThreadPool` becomes `oeju.QueuedThreadPool`.
Default value is `true`.

`org.eclipse.jetty.logging.appender.MESSAGE_ALIGN=`<integer>::
Specifies the column at which the logging `<message>` should be printed.
The value `0` specifies no alignment.
Default value is `0`.

`org.eclipse.jetty.logging.appender.MESSAGE_ESCAPE=`<boolean>::
Specifies whether to escape ISO control characters such as `\r` or `\n` present in the message.
Character `\r` is replaced with `<` and character `\n` is replaced with `|`; all other ISO control characters are replaced with `?`.
Default value is `false`.

`org.eclipse.jetty.logging.appender.ZONE_ID=`<timezone id>::
Specifies the timezone ID (such as `PST`, or `America/Los_Angeles` or `GMT-8:00`) for the `<datetime>` part of the logging line.
The empty string specifies the `UTC` timezone.
Default value is the local timezone.

When using the Jetty SLF4J binding, the logging levels can be dynamically changed via JMX, see xref:troubleshooting/index.adoc#logging[the troubleshooting section] for more information.

[[logging-server-default-rolling]]
==== Capturing Logs to a Rolling File

Logging to `System.err` may be fine at development time, but you will typically want to capture logs on disk for later inspection, or if you don't have a terminal access (for example, if you started Jetty as a service).

The `console-capture` Jetty module allows you to capture what is written to `System.out` and `System.err` and write it to a log file.
By default, `console-capture` logs to a file in the `$JETTY_BASE/logs/` directory.

See the xref:modules/standard.adoc#console-capture[`console-capture` module documentation] for details on configuring how logs are written to the `log` directory.

[NOTE]
====
The `console-capture` Jetty module should be used only in conjunction with the `logging-jetty` module, as other SLF4J bindings such as LogBack or Log4j2 have their own, more sophisticated, rolling file appenders.
====

[[logging-server-custom]]
==== Custom Configuration

You can use a different SLF4J binding if you are more familiar with other logging libraries, or if you need custom logging appenders.
There are a number of out-of-the-box Jetty modules that you can use:

* `logging-logback`, to use the http://logback.qos.ch/[LogBack] binding
* `logging-log4j2`, to use the https://logging.apache.org/log4j/2.x/[Log4j2] binding
* `logging-log4j1`, to use the https://logging.apache.org/log4j/1.2/[Log4j1] binding (note that Log4j 1.x is end-of-life)
* `logging-jul`, to use the `java.util.logging` binding
* `logging-noop`, to use the SLF4J no-operation binding (discards all logging)

[[logging-server-custom-logback]]
==== Logging with LogBack

To enable the `logging-logback` module, run:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=logging-logback,http
----

Since LogBack is released under a license that is different from Jetty's, you will be prompted to accept the LogBack license.
Once you accept the LogBack license, your `$JETTY_BASE` directory will have the following structure.

----
$JETTY_BASE
├── lib
│   └── logging
│       ├── logback-classic-<version>.jar
│       └── logback-core-<version>.jar
├── resources
│   └── logback.xml
└── start.d
    ├── http.ini
    └── logging-logback.ini
----

Jetty downloaded the required LogBack `+*.jar+` files, and created a `$JETTY_BASE/resources/logback.xml` file for configuring your LogBack logging.
Please refer to the http://logback.qos.ch/manual/configuration.html[LogBack configuration manual] for more information about how to configure LogBack.

[[logging-server-custom-log4j2]]
==== Logging with Log4j2

To enable the `logging-log4j2` module, run:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=logging-log4j2,http
----

After accepting the Log4j2 license, you will have the following directory structure:

----
$JETTY_BASE
├── lib
│   └── logging
│       ├── log4j-api-<version>.jar
│       ├── log4j-core-<version>.jar
│       └── log4j-slf4j2-impl-<version>.jar
├── resources
│   └── log4j2.xml
└── start.d
    ├── http.ini
    └── logging-log4j2.ini
----

Jetty downloaded the required Log4j2 `+*.jar+` files, and created a `$JETTY_BASE/resources/log4j2.xml` file that you can configure to customize your Log4j2 logging.

Please refer to the https://logging.apache.org/log4j/2.x/manual/configuration.html[Log4j2 configuration manual] for more information about how to configure Log4j2.

[[logging-server-bridges]]
==== Bridging Logging to SLF4J

When you use libraries that provide the features you need (for example, JDBC drivers), it may be possible that those libraries use a different logging framework than SLF4J.

SLF4J provides http://www.slf4j.org/legacy.html[bridges for legacy logging APIs] that allows you to bridge logging from one of these legacy logging frameworks to SLF4J.
Once the logging is bridged to SLF4J, you can use Jetty's <<logging-server-default,default configuration>> or a <<logging-server-custom,custom configuration>> so that your logging is centralized in one place.

Jetty provides the `logging-jul-capture` module for bridging from `java.util.logging` to SLF4J.

IMPORTANT: The modules `logging-jcl-capture` and `logging-log4j1-capture` similarly provide bridges from Jakarta Commons Logging (JCL) and Apache Log4j, respectively; however, these modules are obsolete and should not be used anymore.

[[logging-server-bridge-jul]]
==== Bridging from `java.util.logging`

For libraries that use `java.util.logging` as their logging framework, you can enable Jetty's `logging-jul-capture` module:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=logging-jul-capture
----

The `logging-jul-capture` module implies `--exec` and therefore xref:start/index.adoc#start[spawns a second JVM] because it needs to provide the system property `java.util.logging.config.file` (so that `java.util.logging` can read the configuration from the specified file), and because it needs to make available on the System ClassLoader the class `org.slf4j.bridge.SLF4JBridgeHandler`.

For example, a library that uses `java.util.logging` as its logging library is the Postgresql JDBC driver.
With the `logging-jul-capture` Jetty module, the logging follows this diagram:

[plantuml]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false

participant "Postgresql JDBC" as postgresql
participant java.util.logging
participant SLF4JBridgeHandler
participant Jetty
participant SLF4J
participant "Jetty SLF4J Binding" as binding


postgresql -> java.util.logging
java.util.logging -> SLF4JBridgeHandler
SLF4JBridgeHandler -> SLF4J
SLF4J -> binding
Jetty -> SLF4J
SLF4J -> binding
----

Note how Jetty logs directly to SLF4J, while the Postgresql JDBC driver logs to SLF4J through the `SLF4JBridgeHandler`.
They both arrive to the SLF4J binding, in this case the Jetty SLF4J binding (but could be any other SLF4J binding such as LogBack).

[[logging-request]]
=== Request Logging

HTTP requests and responses can be logged to provide data that can be later analyzed with other tools, that can provide information such as the most frequently accessed request URIs, the response status codes, the request/response content lengths, geographical information about the clients, etc.

Request logging is enabled by enabling the `requestlog` Jetty module.
In the example below, both the `http` Jetty module and the `requestlog` module are enabled, so that you can make HTTP requests to the server and have them logged:

----
$ cd $JETTY_BASE
$ java -jar $JETTY_HOME/start.jar --add-modules=http,requestlog
----

The `$JETTY_BASE` directory looks like this:

[source]
----
$JETTY_BASE
├── logs
├── resources
│   └── jetty-logging.properties
└── start.d
    ├── http.ini
    └── requestlog.ini
----

The `$JETTY_BASE/start.d/requestlog.ini` file is the Jetty module configuration file that allows you to configure the `requestlog` module, see xref:modules/standard.adoc#requestlog[this section] for more details.

By default the `requestlog` Jetty module produces the `$JETTY_BASE/logs/yyyy_MM_dd.request.log`, where the pattern `yyyy_MM_dd` is replaced with the current date, for example `2020_01_31`.

The format of the request log lines is the result of a _format string_ that uses formatting symbols to log relevant request/response data.

The default format is the https://en.wikipedia.org/wiki/Common_Log_Format[NCSA Format] extended with referrer data and user-agent data.
A typical log line looks like this:

[,options=nowrap]
----
192.168.0.100 - - [31/Jan/2020:20:30:40 +0000] "GET / HTTP/1.1" 200 6789 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36"
----

The line above (that uses fake values) shows `192.168.0.100` for the client IP address, a hard-coded `-` for the identity, `-` for the authenticated user name, `[31/Jan/2020:20:30:40 +0000]` for the date and time with timezone, `"GET / HTTP/1.1"` for the HTTP request line, `200` for the HTTP response status code, `6789` for the HTTP response content length, `"-"` for the referrer and `"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36"` for the user-agent.

The format string can be customized as described in xref:modules/standard.adoc#requestlog[this section].
Request log files are rolled every day, and retained for customizable number of days, by default 90 days.

[NOTE]
====
When Jetty is behind a load balancer, you want to log the remote client IP address, not the load balancer IP address. Refer to xref:protocols/index.adoc#proxy[this section] to configure the load balancer and Jetty to retain the remote client IP address information.
====

[[logging-request-slf4j]]
==== Request Logging to SLF4J

The `requestlog-slf4j` Jetty module provides an alternative to the file-based `requestlog` module.
Instead of writing request logs to a file, it writes them to the SLF4J logging framework at INFO level.

This is useful when:

* You want request logs to appear in your application's main log stream
* You're using a centralized logging system (ELK, Splunk, etc.)
* You prefer console output during development
* You want your logging framework (Logback, Log4j2) to handle log rotation and formatting

To enable SLF4J request logging:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=http,requestlog-slf4j
----

By default, request logs are written to the logger named `org.eclipse.jetty.server.RequestLog` at INFO level.
You can configure the logger name in `$JETTY_BASE/start.d/requestlog-slf4j.ini`:

----
jetty.requestlog.slf4j.loggerName=com.example.RequestLog
----

The format string can be customized the same way as the standard `requestlog` module:

----
jetty.requestlog.formatString=%{client}a - %u %t "%r" %s %O
----

[NOTE]
====
The `requestlog-slf4j` module delegates to your SLF4J logging configuration.
To see request logs in your output, ensure that:

* The logger `org.eclipse.jetty.server.RequestLog` (or your custom logger name) is configured at INFO level or lower
* Your SLF4J binding (Logback, Log4j2, etc.) is properly configured

Example Logback configuration:
[source,xml]
----
<logger name="org.eclipse.jetty.server.RequestLog" level="INFO"/>
----
====

[[threadpool]]
== Thread Pooling

Jetty uses thread pooling to efficiently execute tasks that provide Jetty functionalities.

Like any other component, the Jetty thread pool is configured and enabled via the xref:modules/standard.adoc#threadpool[`threadpool` Jetty module], that is transitively enabled by the xref:modules/standard.adoc#server[`server` Jetty module] which, in turn, is transitively enabled by a protocol module such as the xref:protocols/index.adoc#http[`http` Jetty module]:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=http
----

The command above gives you the default configuration for the thread pool.

If you want to explicitly configure the thread pool, it is enough to explicitly specify the xref:modules/standard.adoc#threadpool[`threadpool`] module:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=threadpool,http
----

After the command above, the `$JETTY_BASE` directory looks like this:

[source]
----
$JETTY_BASE
├── resources
│   └── jetty-logging.properties
└── start.d
    ├── http.ini
    └── threadpool.ini
----

Now you can customize the `threadpool.ini` file to explicitly configure the thread pool.

[[threadpool-virtual]]
=== Virtual Threads Support

Virtual threads have been introduced as a preview feature in Java 19 and Java 20, and have become an official feature since Java 21.

The xref:modules/standard.adoc#threadpool-virtual-preview[`threadpool-virtual-preview`] Jetty module provides support for virtual threads in Java 19 and Java 20, and it is mutually exclusive with the `threadpool` Jetty module.

When using Java 21, there are two Jetty modules available:

* xref:modules/standard.adoc#threadpool-virtual[`threadpool-virtual`]
* xref:modules/standard.adoc#threadpool-all-virtual[`threadpool-all-virtual`]

Both are mutually exclusive with the `threadpool` Jetty module.

If you have already enabled the `threadpool` Jetty module, it is sufficient to remove it by removing the `$JETTY_BASE/start.d/threadpool.ini` file.

The xref:modules/standard.adoc#threadpool-virtual[`threadpool-virtual`] Jetty module provides a mixed thread mode, where platform threads are used to run internal Jetty tasks, but application code is invoked using virtual threads.

The xref:modules/standard.adoc#threadpool-all-virtual[`threadpool-all-virtual`] Jetty module provides a thread mode where all threads are virtual threads, including those used internally by Jetty.

You can enable either module using:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=threadpool-virtual,http
----

or

----
$ java -jar $JETTY_HOME/start.jar --add-modules=threadpool-all-virtual,http
----

After the command above, the `$JETTY_BASE/start.d/` directory will contain the corresponding `threadpool-virtual.ini` or `threadpool-all-virtual.ini` file.
You can now explicitly configure the thread pool module properties inside the `+*.ini+` file and then start Jetty.

---

### Documentation Jetty Modules Operations Guide Pages Session Index (`documentation/jetty/modules/operations-guide/pages/session/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= HTTP Session Management

HTTP sessions are a concept within the Servlet API which allow requests to store and retrieve information across the time a user spends in an application.
Jetty offers a number of pluggable alternatives for managing and distributing/persisting sessions.
Choosing the best alternative is an important consideration for every application as is the correct configuration to achieve optimum performance.

[[overview]]
== HTTP Session Overview

=== Terminology

Before diving into the specifics of how to plug-in and configure various alternative HTTP session management modules, let's review some useful terminology:

Session::
is a means of retaining information across requests for a particular user.
The Servlet Specification defines the semantics of sessions.
Some of the most important characteristics of sessions is that they have a unique id and that their contents cannot be shared between different contexts (although the id can be): if a session is invalidated in one context, then all other sessions that share the same id in other contexts will also be invalidated.
Sessions can expire or they can be explicitly invalidated.

SessionIdManager::
is responsible for allocating session ids.
A Jetty server can have at most 1 SessionIdManager.

HouseKeeper::
is responsible for periodically orchestrating the removal of expired sessions.
This process is referred to as <<base-scavenge,"scavenging">>.

SessionHandler::
is responsible for managing the lifecycle of sessions.
A context can have at most 1 `SessionHandler`.

SessionCache::
is a L1 cache of in-use session objects.
The `SessionCache` is used by the `SessionHandler`.

SessionDataStore::
is responsible for all clustering/persistence operations on sessions.
A `SessionCache` uses a `SessionDataStore` as a backing store.

CachingSessionDataStore::
is an L2 cache of session data.
A `SessionCache` can use a `CachingSessionDataStore` as its backing store.

More details on these concepts can be found in the xref:programming-guide:server/session.adoc[Programming Guide].

[NOTE]
====
``SessionDataStore``s implementations interact with other, usually third party, systems responsible for storing and/or distributing session information.
Sessions can be distributed without being persisted.
They can also be persisted without being distributed.
Because persisting session information to a shared store is a very common way of distributing (also known as "clustering") sessions, in the documentation we will often refer to just "persisting".
====

[[modules]]
=== Session Modules

There are a number of modules that offer pluggable alternatives for http session management.
You can design how you want to cache and store http sessions by selecting alternative combinations of session modules.

For example, Jetty ships with two alternative implementations of the `SessionCache`:

* one that caches sessions in memory: <<cache-hash,`session-cache-hash`>>
* one that does not actually cache: <<cache-null,`session-cache-null`>>

There are at least 6 alternative implementations of the `SessionDataStore` that you can use to persist/distribute your http sessions:

* file system storage: <<filesystem,`session-store-file`>>
* relational database storage: <<jdbc,`session-store-jdbc`>>
* NoSQL database storage: <<mongo,`session-store-mongo`>>
* Google Cloud datastore storage: <<gcloud,`session-store-gcloud`>>
* Hazelcast: <<hazelcast-remote,`session-store-hazelcast-remote`>> or <<hazelcast-embedded,`session-store-hazelcast-embedded`>>
* Infinispan: <<infinispan,`session-store-infinispan-remote`>> or <<infinispan-embedded,`session-store-infinispan-embedded`>>

TIP: It is worth noting that if you do not configure _any_ session modules, Jetty will still provide HTTP sessions that are cached in memory but are never persisted.

[[base]]
== The Base Session Module

The `sessions` module is the base module that all other session modules depend upon.
As such it will be _transitively_ enabled if you enable any of the other session modules: you need to _explicitly_ enable it if you wish to _change_ any settings from their defaults.

Enabling the `sessions` module puts the `$JETTY_HOME/etc/sessions/id-manager.xml` file onto the execution path and generates a `$JETTY_BASE/start.d/sessions.ini` file.

The `id-manager.xml` file instantiates a `DefaultSessionIdManager` and `HouseKeeper`.
The former is used to generate and manage session ids whilst the latter is responsible for periodic <<base-scavenge,scavenging>> of expired sessions.

=== Configuration

The `$JETTY_BASE/start.d/sessions.ini` file contains these configuration properties:

jetty.sessionIdManager.workerName::
This uniquely identifies the jetty server instance and is applied to the `SessionIdManager`.
You can either provide a value for this property, or you can allow Jetty to try and synthesize a `workerName` - the latter option is _only_ advisable in the case of a single, non-clustered deployment.
There are two ways a default `workerName` can be synthesized:

* if running on Google AppEngine, the `workerName` will be formed by concatenating the values of the environment variables `JETTY_WORKER_INSTANCE` and `GAE_MODULE_INSTANCE`
* otherwise, the `workerName` will be formed by concatenating the environment variable `JETTY_WORKER_INSTANCE` and the literal `0`.

So, if you're not running on Google AppEngine, and you haven't configured one, the workerName will always be: `node0`.

IMPORTANT: If you have more than one Jetty instance, it is *crucial* that you configure the `workerName` differently for each instance.

jetty.sessionScavengeInterval.seconds::
This is the period in _seconds_ between runs of the `HouseKeeper`, responsible for orchestrating the removal of expired sessions.
By default it will run approximately every 600 secs (ie 10 mins).
As a rule of thumb, you should ensure that the <<base-scavenge,scavenge>> interval is shorter than the `<session-timeout>` of your sessions to ensure that they are promptly scavenged.
On the other hand, if you have a backend store configured for your sessions, <<base-scavenge,scavenging>> too frequently can increase the load on it.

CAUTION: Don't forget that the `<session-timeout>` is specified in `web.xml` in _minutes_ and the value of the `jetty.sessionScavengeInterval.seconds` is in _seconds_.

[[base-scavenge]]
=== Session Scavenging

The `HouseKeeper` is responsible for the periodic initiation of session scavenge cycles.
The `jetty.sessionScavengeInterval.seconds` property in `$JETTY_BASE/start.d/sessions.ini` controls the periodicity of the cycle.

[NOTE]
====
The HouseKeeper semi-randomly adds an additional 10% to the configured `sessionScavengeInterval`.
This is to prevent multiple nodes in a cluster that are all started at once from syncing up scavenge cycles and placing extra load on the configured persistence mechanism.
====

A session whose expiry time has been exceeded is considered eligible for scavenging.
The session might be present in a `SessionCache` and/or present in the session persistence/clustering mechanism.

Scavenging occurs for all contexts on a server at every cycle.
The `HouseKeeper` sequentially asks the `SessionHandler` in each context to find and remove expired sessions.
The `SessionHandler` works with the `SessionDataStore` to evaluate candidates for expiry held in the `SessionCache`, and also to sweep the persistence mechanism to find expired sessions.

The sweep takes two forms: once per cycle the `SessionDataStore` searches for sessions for its own context that have expired; infrequently, the `SessionDataStore` will widen the search to expired sessions in all contexts.
The former finds sessions that are no longer in this context's `SessionCache`, and using some heuristics, are unlikely to be in the `SessionCache` of the same context on another node either.
These sessions will be loaded and fully expired, meaning that `HttpSessionListener.destroy()` will be called for them.
The latter finds sessions that have not been disposed of by scavenge cycles on any other context/node.
As these will be sessions that expired a long time ago, and may not be appropriate to load by the context doing the scavenging, these are summarily deleted without `HttpSessionListener.destroy()` being called.

A combination of these sweeps should ensure that the persistence mechanism does not fill over time with expired sessions.

As aforementioned, the sweep period needs to be short enough to find expired sessions in a timely fashion, but not so often that it overloads the persistence mechanism.

[[cache]]
== Modules for HTTP Session Caching

In this section we will look at the alternatives for the `SessionCache`, i.e. the L1 cache of in-use session objects.
Jetty ships with 2 alternatives: an in-memory cache, and a null cache.
The latter does not actually do any caching of sessions, and can be useful if you either want to minimize your support for sessions, or you are in a clustered deployment without a sticky loadbalancer.

The <<usecases,scenarios>> go into more detail on this.

[[cache-hash]]
=== Caching in Memory

If you wish to change any of the default configuration values you should enable the `session-cache-hash` xref:modules/index.adoc[module].
The name `"hash"` harks back to historical Jetty session implementations, whereby sessions were kept in memory using a HashMap.

==== Configuration

The `$JETTY_BASE/start.d/session-cache-hash.ini` contains the following configurable properties:

jetty.session.evictionPolicy::
Integer, default -1.
This controls whether session objects that are held in memory are subject to eviction from the cache.
Eviction means that the session is removed from the cache.
This can reduce the memory footprint of the cache and can be useful if you have a lot of sessions.
Eviction is usually used in conjunction with a `SessionDataStore` that persists sessions.
The eviction strategies and their corresponding values are:
   -1 (NO EVICTION):::
   sessions are never evicted from the cache.
   The only way they leave are via expiration or invalidation.
   0 (EVICT AFTER USE):::
   sessions are evicted from the cache as soon as the last active request for it finishes.
   The session will be passed to the `SessionDataStore` to be written out before eviction.
   >= 1 (EVICT ON INACTIVITY):::
   any positive number is the time in seconds after which a session that is in the cache but has not experienced any activity will be evicted.
   Use the `jetty.session.saveOnInactiveEvict` property to force a session write before eviction.

NOTE: If you are not using one of the session store modules, ie one of the ``session-store-xxxx``s, then sessions will be lost when the context is stopped, or the session is evicted.

jetty.session.saveOnInactiveEvict::
Boolean, default `false`.
This controls whether a session will be persisted to the `SessionDataStore` if it is being evicted due to the EVICT ON INACTIVITY policy.
Usually sessions will be written to the `SessionDataStore` whenever the last simultaneous request exits the session.
However, as `SessionDataStores` can be configured to skip some writes (see the documentation for the `session-store-xxx` module that you are using), this option is provided to ensure that the session will be written out.

NOTE: Be careful with this option, as in clustered scenarios it would be possible to "re-animate" a session that has actually been deleted by another node.

jetty.session.saveOnCreate::
Boolean, default `false`.
Controls whether a session that is newly created will be immediately saved to the `SessionDataStore` or lazily saved as the last request for the session exits.
This can be useful if the request dispatches to another context and needs to re-use the same session id.

jetty.session.removeUnloadableSessions::
Boolean, default `false`.
Controls whether the session cache should ask a `SessionDataStore` to delete a session that cannot be restored - for example because it is corrupted.

jetty.session.flushOnResponseCommit::
Boolean, default `true`.
If true, if a session is "dirty" - ie it has never been saved or its attributes have changed - it will be written to the `SessionDataStore` as the response is about to commit.
This ensures that all subsequent requests - whether to the same or different node - will see the updated session data.
If false, a dirty session will only be written to the backing store when the last simultaneous request for it leaves the session.
Be aware that in this case, the write can happen _after_ the response is returned to the client.

jetty.session.invalidateOnShutdown::
Boolean, default `false`.
If true, when a context is shutdown, all sessions in the cache are invalidated and deleted both from the cache and from the `SessionDataStore`.

[[cache-null]]
=== No Caching

You may need to use the `session-cache-null` module if your clustering setup does not have a sticky load balancer, or if you want absolutely minimal support for sessions.
If you enable this module, but you don't enable a module that provides session persistence (ie one of the `session-store-xxx` modules), then sessions will _neither_ be retained in memory _nor_ persisted.

==== Configuration

The `$JETTY_BASE/start.d/session-cache-null.ini` contains the following configurable properties:

jetty.session.saveOnCreate::
Boolean, default `false`.
Controls whether a session that is newly created will be immediately saved to the `SessionDataStore` or lazily saved as the last request for the session exits.
This can be useful if the request dispatches to another context and needs to re-use the same session id.

jetty.session.removeUnloadableSessions::
Boolean, default `false`.
Controls whether the session cache should ask a `SessionDataStore` to delete a session that cannot be restored - for example because it is corrupted.

jetty.session.flushOnResponseCommit::
Boolean, default `false`.
If true, if a session is "dirty" - ie its attributes have changed - it will be written to the backing store as the response is about to commit.
This ensures that all subsequent requests whether to the same or different node will see the updated session data.
If false, a dirty session will only be written to the backing store when the last simultaneous request for it leaves the session.

[[filesystem]]
== Modules for Persistent HTTP Sessions: File System

The `session-store-file` Jetty module supports persistent storage of session data in a filesystem.

IMPORTANT: Persisting sessions to the local file system should *never* be used in a clustered environment.

Enabling this module creates the `$JETTY_BASE/sessions` directory.
By default session data will be saved to this directory, one file representing each session.

File names follow this pattern:

`+[expiry]_[contextpath]_[virtualhost]_[id]+`

expiry::
This is the expiry time in milliseconds since the epoch.

contextpath::
This is the context path with any special characters, including `/`, replaced by the `_` underscore character.
For example, a context path of `/catalog` would become `_catalog`.
A context path of simply `/` becomes just `__`.

virtualhost::
This is the first virtual host associated with the context and has the form of 4 digits separated by `.` characters: `+[digit].[digit].[digit].[digit]+`.
If there are no virtual hosts associated with a context, then `0.0.0.0` is used.

id::
This is the unique id of the session.

Putting all of the above together as an example, a session with an id of `node0ek3vx7x2y1e7pmi3z00uqj1k0` for the context with path `/test` with no virtual hosts and an expiry of `1599558193150` would have a file name of:

`1599558193150__test_0.0.0.0_node0ek3vx7x2y1e7pmi3z00uqj1k0`

=== Configuration

The `$JETTY_BASE/start.d/sessions.ini` file contains the following properties which may be modified to customise filesystem session storage:

jetty.session.storeDir::
The default is `$JETTY_BASE/sessions`.
This is a path that defines the location for storage of session files.

jetty.session.file.deleteUnrestorableFiles::
Boolean, default `false`.
If set to `true`, unreadable files will be deleted.
This is useful to prevent repeated logging of the same error when the scavenger periodically (re-)attempts to load the corrupted information for a session in order to expire it.

jetty.session.gracePeriod.seconds::
Integer, default 3600.
Used during session <<base-scavenge,scavenging>>.
Multiples of this period are used to define how long ago a stored session must have expired before it should be <<base-scavenge,scavenged>>.

jetty.session.savePeriod.seconds::
Integer, in seconds, default is `0`.
Whenever a session is accessed by a request, its `lastAccessTime` and `expiry` are updated.
Even if your sessions are read-mostly, the `lastAccessTime` and  `expiry` will always change.
For heavily-used, read-mostly sessions you can save some time by skipping some writes for sessions for which only these fields have changed (ie no session attributes changed).
The value of this property is used to skip writes for these kinds of sessions: the session will only be written out if the time since the last write exceeds the value of this property.

[WARNING]
====
You should be careful in the use of this property in clustered environments: if you set too large a value for this property, the session may not be written out sufficiently often to update its `expiry` time thus making it appear to other nodes that it has expired.
Thorough consideration of the `maxIdleTime` of the session when setting the `savePeriod` is imperative - it would be undesirable to set a `savePeriod` that is larger than the `maxIdleTime`.
====

[[jdbc]]
== Modules for Persistent HTTP Sessions: JDBC

Enabling the `session-store-jdbc` module configures Jetty to persist session data in a relational database.

=== Configuration

After enabling the module, the `$JETTY_BASE/start.d/session-store-jdbc.ini` file contains the following customizable properties:

jetty.session.gracePeriod.seconds::
Integer, default 3600.
Used during session <<base-scavenge,scavenging>>.
Multiples of this period are used to define how long ago a stored session must have expired before it should be <<base-scavenge,scavenged>>.

jetty.session.savePeriod.seconds::
Integer, in seconds, default is `0`.
Whenever a session is accessed by a request, its `lastAccessTime` and `expiry` are updated.
Even if your sessions are read-mostly, the `lastAccessTime` and  `expiry` will always change.
For heavily-used, read-mostly sessions you can save some time by skipping some writes for sessions for which only these fields have changed (ie no session attributes changed).
The value of this property is used to skip writes for these kinds of sessions: the session will only be written out if the time since the last write exceeds the value of this property.

[WARNING]
====
You should be careful in the use of this property in clustered environments: if you set too large a value for this property, the session may not be written out sufficiently often to update its `expiry` time thus making it appear to other nodes that it has expired.
Thorough consideration of the `maxIdleTime` of the session when setting the `savePeriod` is imperative - it would be undesirable to set a `savePeriod` that is larger than the `maxIdleTime`.
====

db-connection-type::
Default `datasource`.
Set to either `datasource` or `driver` depending on the type of connection being used.
Depending which you select, there are additional properties available:

`datasource`:::
jetty.session.jdbc.datasourceName::::
Name of the remote datasource.

`driver`:::
jetty.session.jdbc.driverClass::::
Name of the JDBC driver that controls access to the remote database, such as `com.mysql.jdbc.Driver`
jetty.session.jdbc.driverUrl::::
URL of the database which includes the driver type, host name and port, service name and any specific attributes unique to the database, such as a username.
As an example, here is a mysql connection with the username appended: `jdbc:mysql://127.0.0.1:3306/sessions?user=sessionsadmin`.

jetty.session.jdbc.blobType::
Optional.
Default `blob` or `bytea` for Postgres.
This is the keyword used by the particular database to identify the blob data type.
If netiher default is suitable you can set this value explicitly.

jetty.session.jdbc.longType::
Optional.
Default `bigint` or `number(20)` for Oracle.
This is the keyword used by the particular database to identify the long integer data type.
Set this explicitly if neither of the default values is appropriate.

jetty.session.jdbc.stringType::
Optional.
Default `varchar`.
This is the keyword used by the particular database to identify character type.
If the default is not suitable, you can set this value explicitly.

jetty.session.jdbc.schema.schemaName::
jetty.session.jdbc.schema.catalogName::
Optional.
The exact meaning of these two properties is dependent on your database vendor, but can broadly be described as further scoping for the session table name.
See https://en.wikipedia.org/wiki/Database_schema[] and https://en.wikipedia.org/wiki/Database_catalog[].
These extra scoping names can come into play at startup time when Jetty determines if the session table already exists, or otherwise creates it on-the-fly.
If you have employed either of these concepts when you pre-created the session table, or you want to ensure that Jetty uses them when it auto-creates the session table, then you have two options: either set them explicitly, or let Jetty infer them from a database connection (obtained using either a Datasource or Driver according to the `db-connection-type` you have configured).
To set them explicitly, uncomment and supply appropriate values for the `jetty.session.jdbc.schema.schemaName` and/or `jetty.session.jdbc.schema.catalogName` properties.
Alternatively, to allow Jetty to infer them from a database connection, use the special string `INFERRED` instead.
If you leave them blank or commented out, then the sessions table will not be scoped by schema or catalog name.

jetty.session.jdbc.schema.table::
Default `JettySessions`.
This is the name of the table in which session data is stored.

jetty.session.jdbc.schema.accessTimeColumn::
Default `accessTime`.
This is the name of the column that stores the time - in ms since the epoch - at which a session was last accessed

jetty.session.jdbc.schema.contextPathColumn::
Default `contextPath`.
This is the name of the column that stores the `contextPath` of a session.

jetty.session.jdbc.schema.cookieTimeColumn::
Default `cookieTime`.
This is the name of the column that stores the time - in ms since the epoch - that the cookie was last set for a session.

jetty.session.jdbc.schema.createTimeColumn::
Default `createTime`.
This is the name of the column that stores the time - in ms since the epoch - at which a session was created.

jetty.session.jdbc.schema.expiryTimeColumn::
Default `expiryTime`.
This is name of the column that stores - in ms since the epoch - the time at which a session will expire.

jetty.session.jdbc.schema.lastAccessTimeColumn::
Default `lastAccessTime`.
This is the name of the column that stores the time - in ms since the epoch - that a session was previously accessed.

jetty.session.jdbc.schema.lastSavedTimeColumn::
Default `lastSavedTime`.
This is the name of the column that stores the time - in ms since the epoch - at which a session was last written.

jetty.session.jdbc.schema.idColumn::
Default `sessionId`.
This is the name of the column that stores the id of a session.

jetty.session.jdbc.schema.lastNodeColumn::
Default `lastNode`.
This is the name of the column that stores the `workerName` of the last node to write a session.

jetty.session.jdbc.schema.virtualHostColumn::
Default `virtualHost`.
This is the name of the column that stores the first virtual host of the context of a session.

jetty.session.jdbc.schema.maxIntervalColumn::
Default `maxInterval`.
This is the name of the column that stores the interval - in ms - during which a session can be idle before being considered expired.

jetty.session.jdbc.schema.mapColumn::
Default `map`.
This is the name of the column that stores the serialized attributes of a session.

[[mongo]]
== Modules for Persistent HTTP Sessions: MongoDB

Enabling the `session-store-mongo` module configures Jetty to store session data in MongoDB.

Because MongoDB is not a technology provided by the Eclipse Foundation, you will be prompted to assent to the licenses of the external vendor (Apache in this case) during the install.
Jars needed by MongoDB are downloaded and stored into a directory named `$JETTY_BASE/lib/nosql/`.

IMPORTANT: If you want to use updated versions of the jar files automatically downloaded by Jetty, you can place them in the associated `$JETTY_BASE/lib/` directory and use the `--skip-create-files=<module name>` command line option to prevent errors when starting your server.

=== Configuration

The `$JETTY_BASE/start.d/session-store-mongo.ini` file contains these configurable properties:

jetty.session.mongo.dbName::
Default is "HttpSessions".
This is the name of the database in MongoDB used to store the session collection.

jetty.session.mongo.collectionName::
Default is "jettySessions".
This is the name of the collection in MongoDB used to store all of the sessions.

The connection type-::
You can connect to MongoDB either using a host/port combination, or a URI.
By default, the host/port method is selected, but you can change this by commenting out the unwanted method, and uncommenting the other one.
connection-type=address:::
Used when utilizing a direct connection to the MongoDB server.
jetty.session.mongo.host::::
Host name or address for the remote MongoDB instance.
jetty.session.mongo.port::::
Port number for the remote MongoDB instance.
connection-type=uri:::
Used when utilizing MongoURI for secured connections.
jetty.session.mongo.connectionString::::
The string defining the MongoURI value, such as `+mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]+`.
More information on how to format the MongoURI string can be found in the https://docs.mongodb.com/manual/reference/connection-string/[official documentation for mongo].
[NOTE]
====
You will only use *one* `connection-type` at a time, either `address` or `uri`.
If both are utilized in your `session-store-mongo.ini`, only the _last_ `connection-type` configured in the file will be used.
====

jetty.session.gracePeriod.seconds::
Integer, in seconds.
Default 3600.
Used during session <<base-scavenge,scavenging>>.
Multiples of this period are used to define how long ago a stored session must have expired before it should be <<base-scavenge,scavenged>>.

jetty.session.savePeriod.seconds::
Integer, in seconds, default is `0`.
Whenever a session is accessed by a request, its `lastAccessTime` and `expiry` are updated.
Even if your sessions are read-mostly, the `lastAccessTime` and  `expiry` will always change.
For heavily-used, read-mostly sessions you can save some time by skipping some writes for sessions for which only these fields have changed (ie no session attributes changed).
The value of this property is used to skip writes for these kinds of sessions: the session will only be written out if the time since the last write exceeds the value of this property.

[WARNING]
====
You should be careful in the use of this property in clustered environments: if you set too large a value for this property, the session may not be written out sufficiently often to update its `expiry` time thus making it appear to other nodes that it has expired.
Thorough consideration of the `maxIdleTime` of the session when setting the `savePeriod` is imperative - it would be undesirable to set a `savePeriod` that is larger than the `maxIdleTime`.
====

[[infinispan]]
== Modules for Persistent HTTP Sessions: Infinispan

In order to persist/cluster sessions using Infinispan, Jetty needs to know how to contact Infinispan.
There are two options: a remote Infinispan instance, or an in-process Infinispan instance.
The former is referred to as "remote" Infinispan and the latter as "embedded" Infinispan.
If you wish Jetty to be able to <<base-scavenge,scavenge>> expired sessions, you will also need to enable the appropriate `infinispan-[remote|embedded]-query` module.

[[infinispan-remote]]
=== Remote Infinispan Session Module

The `session-store-infinispan-remote` module configures Jetty to talk to an external Infinispan instance to store session data.

Because Infinispan is not a technology provided by the Eclipse Foundation, you will be prompted to assent to the licenses of the external vendor (Apache in this case).

Infinispan-specific jar files are download to the directory named `$JETTY_BASE/lib/infinispan/`.

In addition to adding these modules to the classpath of the server it also added several ini configuration files to the `$JETTY_BASE/start.d` directory.

NOTE: If you have updated versions of the jar files automatically downloaded by Jetty, you can place them in the associated `$JETTY_BASE/lib/` directory and use the `--skip-create-files=<module name>` command line option to prevent errors when starting your server.

==== Configuration

The `$JETTY_BASE/start.d/session-store-infinispan-remote.ini` contains the following configurable properties:

jetty.session.infinispan.remoteCacheName::
Default `"sessions"`.
This is the name of the cache in Infinispan where sessions will be stored.

jetty.session.infinispan.idleTimeout.seconds::
Integer, in seconds, default `0`.
This is the amount of time, in seconds, that a session entry in Infinispan can be idle (ie neither read nor written) before Infinispan will delete its entry.
Usually, you do *not* want to set a value for this, as you want Jetty to manage all session expiration (and call any HttpSessionListeners).
You *should* enable the <<infinispan-remote-query,infinispan-remote-query>> to allow jetty to <<base-scavenge,scavenge>> for expired sessions.
If you do not, then there is the possibility that sessions can be left in Infinispan but no longer referenced by any Jetty node (so called "zombie" or "orphan" sessions), in which case you can use this feature to ensure their removal.

IMPORTANT: You should make sure that the number of seconds you specify is larger than the configured `maxIdleTime` for sessions.

jetty.session.gracePeriod.seconds::
Integer, default 3600.
Used during session <<base-scavenge,scavenging>>.
Multiples of this period are used to define how long ago a stored session must have expired before it should be <<base-scavenge,scavenged>>.

jetty.session.savePeriod.seconds::
Integer, in seconds, default is `0`.
Whenever a session is accessed by a request, its `lastAccessTime` and `expiry` are updated.
Even if your sessions are read-mostly, the `lastAccessTime` and  `expiry` will always change.
For heavily-used, read-mostly sessions you can save some time by skipping some writes for sessions for which only these fields have changed (ie no session attributes changed).
The value of this property is used to skip writes for these kinds of sessions: the session will only be written out if the time since the last write exceeds the value of this property.

[WARNING]
====
You should be careful in the use of this property in clustered environments: if you set too large a value for this property, the session may not be written out sufficiently often to update its `expiry` time thus making it appear to other nodes that it has expired.
Thorough consideration of the `maxIdleTime` of the session when setting the `savePeriod` is imperative - it would be undesirable to set a `savePeriod` that is larger than the `maxIdleTime`.
====

[[infinispan-remote-query]]
=== Remote Infinispan Query Module

The `infinispan-remote-query` module allows Jetty to <<base-scavenge,scavenge>> expired sessions.
Note that this is an *additional* module, to be used in conjunction with the `session-store-infinispan-remote` module.

There are no configuration properties associated with this module.

[[infinispan-embedded]]
=== Embedded Infinispan Session Module

Enabling the `session-store-infinispan-embedded` module runs an in-process instance of Infinispan.

Because Infinispan is not a technology provided by the Eclipse Foundation, you will be prompted to assent to the licenses of the external vendor (Apache in this case).
Infinispan-specific jar files will be downloaded and saved to a directory named `$JETTY_BASE/lib/infinispan/`.

NOTE: If you have updated versions of the jar files automatically downloaded by Jetty, you can place them in the associated `$JETTY_BASE/lib/` directory and use the `--skip-create-files=<module name>` command line option to prevent errors when starting your server.

==== Configuration

The `$JETTY_BASE/start.d/session-store-infinispan-embedded.ini` contains the following configurable properties:

jetty.session.infinispan.idleTimeout.seconds::
Integer, in seconds, default `0`.
This is the amount of time, in seconds, that a session entry in Infinispan can be idle (ie neither read nor written) before Infinispan will delete its entry.
Usually, you do *not* want to set a value for this, as you want Jetty to manage all session expiration (and call any HttpSessionListeners).
You *should* enable the <<infinispan-embedded-query,infinispan-embedded-query>> to allow Jetty to <<base-scavenge,scavenge>> for expired sessions.
If you do not, then there is the possibility that expired sessions can be left in Infinispan.

IMPORTANT: You should make sure that the number of seconds you specify is larger than the configured `maxIdleTime` for sessions.

jetty.session.gracePeriod.seconds::
Integer, default 3600.
Used during session <<base-scavenge,scavenging>>.
Multiples of this period are used to define how long ago a stored session must have expired before it should be <<base-scavenge,scavenged>>.

jetty.session.savePeriod.seconds::
Integer, in seconds, default is `0`.
Whenever a session is accessed by a request, its `lastAccessTime` and `expiry` are updated.
Even if your sessions are read-mostly, the `lastAccessTime` and  `expiry` will always change.
For heavily-used, read-mostly sessions you can save some time by skipping some writes for sessions for which only these fields have changed (ie no session attributes changed).
The value of this property is used to skip writes for these kinds of sessions: the session will only be written out if the time since the last write exceeds the value of this property.

[WARNING]
====
Thorough consideration of the `maxIdleTime` of the session when setting the `savePeriod` is imperative - it would be undesirable to set a `savePeriod` that is larger than the `maxIdleTime`.
====

[[infinispan-embedded-query]]
=== Embedded Infinispan Query Module

The `infinispan-embedded-query` module allows Jetty to <<base-scavenge,scavenge>> expired sessions.

There are no configuration properties associated with this module.

=== Converting Session Format for Jetty-9.4.13

From Jetty-9.4.13 onwards, we have changed the format of the serialized session when using a remote cache (ie using hotrod).
Prior to release 9.4.13 we used the default Infinispan serialization, however this was not able to store sufficient information to allow Jetty to properly deserialize session attributes in all circumstances.
See issue https://github.com/jetty/jetty.project/issues/2919[] for more background.

We have provided a conversion program which will convert any sessions stored in Infinispan to the new format.

IMPORTANT: We recommend that you backup your stored sessions before running the conversion program.

How to use the converter:

[source,subs=attributes]
----
java -cp jakarta.servlet-api-6.0.0.jar:jetty-util-{jetty-version}.jar:jetty-server-{jetty-version}.jar:infinispan-remote-9.1.0.Final.jar:jetty-infinispan-{jetty-version}.jar:[other classpath]  org.eclipse.jetty.session.infinispan.InfinispanSessionLegacyConverter

Usage:  InfinispanSessionLegacyConverter [-Dhost=127.0.0.1] [-Dverbose=true|false] <cache-name> [check]
----

The classpath::
Must contain the servlet-api, jetty-util, jetty-server, jetty-infinispan and infinispan-remote jars. If your sessions contain attributes that use application classes, you will also need to also put those classes onto the classpath. If your session has been authenticated, you may also need to include the jetty-security and jetty-http jars on the classpath.

Parameters::
When used with no arguments the usage message is printed. When used with the `cache-name` parameter the conversion is performed. When used with both `cache-name` and `check` parameters, sessions are checked for whether or not they are converted.
-Dhost:::  you can optionally provide a system property with the address of your remote Infinispan server. Defaults to the localhost.
-Dverbose::: defaults to false. If true, prints more comprehensive stacktrace information about failures. Useful to diagnose why a session is not converted.
cache-name::: the name of the remote cache containing your sessions. This is mandatory.
check::: the optional check command will verify sessions have been converted. Use it _after_ doing the conversion.

To perform the conversion, run the InfinispanSessionLegacyConverter with just the `cache-name`, and optionally the `host` system property.
The following command will attempt to convert all sessions in the cached named `my-remote-cache` on the machine `myhost`, ensuring that application classes in the `/my/custom/classes` directory are on the classpath:

[source,subs=attributes]
----
java -cp jakarta.servlet-api-6.0.0.jar:jetty-util-{jetty-version}.jar:jetty-server-{jetty-version}.jar:infinispan-remote-9.1.0.Final.jar:jetty-infinispan-{jetty-version}.jar:/my/custom/classes  org.eclipse.jetty.session.infinispan.InfinispanSessionLegacyConverter -Dhost=myhost my-remote-cache
----

If the converter fails to convert a session, an error message and stacktrace will be printed and the conversion will abort. The failed session should be untouched, however _it is prudent to take a backup of your cache before attempting the conversion_.

[[hazelcast]]
== Modules for Persistent HTTP Sessions: Hazelcast

Hazelcast can be used to cluster session information in one of two modes: either remote or embedded.
Remote mode means that Hazelcast will create a client to talk to other instances, possibly on other nodes.
Embedded mode means that Hazelcast will start a local instance and communicate with that.

[[hazelcast-remote]]
=== Remote Hazelcast Clustering

Enabling the `session-store-hazelcast-remote` module allows jetty to communicate with a remote Hazelcast instance to cluster session data.

Because Hazelcast is not a technology provided by the Eclipse Foundation, you will be prompted to assent to the licenses of the external vendor (Apache in this case).

Hazelcast-specific jar files will be downloaded and saved to a directory named `$JETTY_BASE/lib/hazelcast/`.

NOTE: If you have updated versions of the jar files automatically downloaded by Jetty, you can place them in the associated `$JETTY_BASE/lib/` directory and use the `--skip-create-files=<module name>` command line option to prevent errors when starting your server.

==== Configuration

The `start.d/session-store-hazelcast-remote.ini` contains a list of all the configurable options for the Hazelcast module:

jetty.session.hazelcast.mapName::
The default is "jetty-distributed-session-map".
This is the name of the Map in Hazelcast where sessions will be stored.

jetty.session.hazelcast.onlyClient::
Boolean, default `true`.
The Hazelcast instance will be configured in client mode.

jetty.session.hazelcast.configurationLocation::
Optional.
This is the path to an external Hazelcast xml configuration file.

jetty.session.hazelcast.useQueries::
Boolean, default `false`.
If `true`, Jetty will use Hazelcast queries to find sessions to <<base-scavenge,scavenge>>.
If `false` sessions that are not currently in a <<cache,session cache>> cannot be <<base-scavenge,scavenged>>, and will need to be removed by some external process.

jetty.session.hazelcast.addresses::
Optional.
These are the addresses of remote Hazelcast instances with which to communicate.

jetty.session.gracePeriod.seconds::
Integer, in seconds.
Default 3600.
Used during session <<base-scavenge,scavenging>>.
Multiples of this period are used to define how long ago a stored session must have expired before it should be <<base-scavenge,scavenged>>.

jetty.session.savePeriod.seconds::
Integer, in seconds, default is `0`.
Whenever a session is accessed by a request, its `lastAccessTime` and `expiry` are updated.
Even if your sessions are read-mostly, the `lastAccessTime` and  `expiry` will always change.
For heavily-used, read-mostly sessions you can save some time by skipping some writes for sessions for which only these fields have changed (ie no session attributes changed).
The value of this property is used to skip writes for these kinds of sessions: the session will only be written out if the time since the last write exceeds the value of this property.

[WARNING]
====
You should be careful in the use of this property in clustered environments: if you set too large a value for this property, the session may not be written out sufficiently often to update its `expiry` time thus making it appear to other nodes that it has expired.
Thorough consideration of the `maxIdleTime` of the session when setting the `savePeriod` is imperative - it would be undesirable to set a `savePeriod` that is larger than the `maxIdleTime`.
====

IMPORTANT: Be aware that if your session attributes contain classes from inside your web application (or Jetty classes) then you will need to put these classes onto the classpath of all of your Hazelcast instances.

[[hazelcast-embedded]]
=== Embedded Hazelcast Clustering

This will run an in-process instance of Hazelcast.
This can be useful for example during testing.
To enable this you enable the `session-store-hazelcast-embedded` module.

Because Hazelcast is not a technology provided by the Eclipse Foundation, you will be prompted to assent to the licenses of the external vendor (Apache in this case).

Hazelcast-specific jar files will be downloaded to a directory named `$JETTY_BASE/lib/hazelcast/`.

==== Configuration

The `$JETTY_BASE/start.d/start.d/session-store-hazelcast-embedded.ini` contains a list of all the configurable options for the Hazelcast module:

jetty.session.hazelcast.mapName::
The default is "jetty-distributed-session-map".
This is the name of the Map in Hazelcast where sessions will be stored.
jetty.session.hazelcast.hazelcastInstanceName
Default is "JETTY_DISTRIBUTED_SESSION_INSTANCE".
This is the unique name of the Hazelcast instance that will be created.

jetty.session.hazelcast.configurationLocation::
Optional.
This is the path to an external Hazelcast xml configuration file.

jetty.session.hazelcast.useQueries::
Boolean, default `false'.
If `true`, Jetty will use Hazelcast queries to find expired sessions to <<base-scavenge,scavenge>>.
If `false` sessions that are not currently in a <<cache,session cache>> cannot be <<base-scavenge,scavenged>>, and will need to be removed by some external process.

jetty.session.gracePeriod.seconds::
Integer, in seconds.
Default 3600.
Used during session <<base-scavenge,scavenging>>.
Multiples of this period are used to define how long ago a stored session must have expired before it should be <<base-scavenge,scavenged>>.

jetty.session.savePeriod.seconds::
Integer, in seconds, default is `0`.
Whenever a session is accessed by a request, its `lastAccessTime` and `expiry` are updated.
Even if your sessions are read-mostly, the `lastAccessTime` and  `expiry` will always change.
For heavily-used, read-mostly sessions you can save some time by skipping some writes for sessions for which only these fields have changed (ie no session attributes changed).
The value of this property is used to skip writes for these kinds of sessions: the session will only be written out if the time since the last write exceeds the value of this property.

[WARNING]
====
You should be careful in the use of this property in clustered environments: if you set too large a value for this property, the session may not be written out sufficiently often to update its `expiry` time thus making it appear to other nodes that it has expired.
Thorough consideration of the `maxIdleTime` of the session when setting the `savePeriod` is imperative - it would be undesirable to set a `savePeriod` that is larger than the `maxIdleTime`.
====

IMPORTANT: If your session attributes contain classes from inside your web application (or jetty classes) then you will need to put these classes onto the classpath of all of your Hazelcast instances. In the case of embedded Hazelcast, as it is started before your web application, it will NOT have access to your web application's classes - you will need to extract these classes and put them onto the jetty server's classpath.

[[gcloud]]
== Modules for Persistent HTTP Sessions: Google Cloud DataStore

Jetty can store http session information into GCloud by enabling the `session-store-gcloud` module.

=== Preparation

You will first need to create a project and enable the Google Cloud API: https://cloud.google.com/docs/authentication#preparation[].
Take note of the project id that you create in this step as you need to supply it in later steps.

=== Communicating with GCloudDataStore

==== When Running Jetty Outside of Google Infrastructure

Before running Jetty, you will need to choose one of the following methods to set up the local environment to enable remote GCloud DataStore communications.

1. Using the GCloud SDK:
  * Ensure you have the GCloud SDK installed: https://cloud.google.com/sdk/?hl=en[]
  * Use the GCloud tool to set up the project you created in the preparation step: `gcloud config set project PROJECT_ID`
  * Use the GCloud tool to authenticate a Google account associated with the project created in the preparation step: `gcloud auth login ACCOUNT`

2. Using environment variables
  * Define the environment variable `GCLOUD_PROJECT` with the project id you created in the preparation step.
  * Generate a JSON https://cloud.google.com/storage/docs/authentication?hl=en#service_accounts[service account key] and then define the environment variable `GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/key.json`

==== When Running Jetty Inside of Google Infrastructure

The Google deployment tools will automatically configure the project and authentication information for you.

=== Configuring Indexes for Session Data

Using some special, composite indexes can speed up session search operations, although it may make write operations slower.
By default, indexes will _not_ be used.
In order to use them, you will need to manually upload a file that defines the indexes.
This file is named `index.yaml` and you can find it in your distribution in `$JETTY_BASE/etc/sessions/gcloud/index.yaml`.

Follow the instructions https://cloud.google.com/datastore/docs/tools/#the_development_workflow_using_gcloud[here] to upload the pre-generated `index.yaml` file.

=== Communicating with the GCloudDataStore Emulator

To enable communication using the GCloud Emulator:

* Ensure you have the GCloud SDK installed: https://cloud.google.com/sdk/?hl=en[]
* Follow the instructions https://cloud.google.com/datastore/docs/tools/datastore-emulator[here] on how to start the GCloud datastore emulator, and how to propagate the environment variables that it creates to the terminal in which you run Jetty.

=== Enabling the Google Cloud DataStore Module

The `session-store-gcloud` module provides GCloud support for storing session data.

Because the Google Cloud DataStore is not a technology provided by the Eclipse Foundation, when enabling the module you will be prompted to assent to the licenses of the external vendor.

As GCloud requires certain Java Commons Logging features to work correctly, Jetty routes these through SLF4J.
By default, Jetty implements the SLF4J api, but you can choose a different logging implementation by following the instructions xref:server/index.adoc#logging[here]

IMPORTANT: If you want to use updated versions of the jar files automatically downloaded during the module enablement, you can place them in the associated `$JETTY_BASE/lib/` directory and use the `--skip-create-files=<module name>` command line option to prevent errors when starting your server.

== Configuration

The `$JETTY_BASE/start.d/session-store-gcloud.ini` file contains all of the configurable properties for the `session-store-gcloud` module:

jetty.session.gcloud.maxRetries::
Integer.
Default 5.
Maximum number of retries to connect to GCloud DataStore to write a session.

jetty.session.gcloud.backoffMs::
Integer in milliseconds.
Default 1000.
Number of milliseconds between successive attempts to connect to the GCloud DataStore to write a session.

jetty.session.gracePeriod.seconds::
Integer, in seconds.
Default 3600.
Used during session <<base-scavenge,scavenging>>.
Multiples of this period are used to define how long ago a stored session must have expired before it should be <<base-scavenge,scavenged>>.

jetty.session.savePeriod.seconds::
Integer, in seconds, default is `0`.
Whenever a session is accessed by a request, its `lastAccessTime` and `expiry` are updated.
Even if your sessions are read-mostly, the `lastAccessTime` and  `expiry` will always change.
For heavily-used, read-mostly sessions you can save some time by skipping some writes for sessions for which only these fields have changed (ie no session attributes changed).
The value of this property is used to skip writes for these kinds of sessions: the session will only be written out if the time since the last write exceeds the value of this property.

[WARNING]
====
You should be careful in the use of this property in clustered environments: if you set too large a value for this property, the session may not be written out sufficiently often to update its `expiry` time thus making it appear to other nodes that it has expired.
Thorough consideration of the `maxIdleTime` of the session when setting the `savePeriod` is imperative - it would be undesirable to set a `savePeriod` that is larger than the `maxIdleTime`.
====

jetty.session.gcloud.namespace::
Optional.
Sets the namespace for GCloud Datastore to use.
If set, partitions the visibility of session data between web applications, which is helpful for multi-tenant deployments.
More information can be found https://cloud.google.com/datastore/docs/concepts/multitenancy[here.]

Configuration of the stored session object and its fields names-::
You should very rarely, if ever, need to change these defaults.
jetty.session.gcloud.model.kind:::
The default is "GCloudSession".
This is the type of the object that is stored in GCloud.
jetty.session.gcloud.model.id:::
The default is "id".
This is the session id.
jetty.session.gcloud.model.contextPath:::
The default is "contextPath".
This is the canonicalized context path of the context to which the session belongs.
jetty.session.gcloud.model.vhost:::
The default is "vhost".
This is the canonicalized virtual host of the context to which the session belongs.
jetty.session.gcloud.model.accessed:::
The default is "accessed".
This is the current access time of the session.
jetty.session.gcloud.model.lastAccessed:::
The default is "lastAccessed".
This is the last access time of the session.
jetty.session.gcloud.model.createTime:::
The default is "createTime".
This is the time, in ms since the epoch, at which the session was created.
jetty.session.gcloud.model.cookieSetTime:::
The default is "cookieSetTime".
This is the time at which the session cookie was last set.
jetty.session.gcloud.model.lastNode:::
The default is "lastNode".
This is the `workerName` of the last node to manage the session.
jetty.session.gcloud.model.expiry:::
The default is "expiry".
This is the time, in ms since the epoch, at which the session will expire.
jetty.session.gcloud.model.maxInactive:::
The default is "maxInactive".
This is the session timeout in ms.
jetty.session.gcloud.model.attributes:::
The default is "attributes".
This is a map of all the session attributes.

[[memcached]]

== Modules for Persistent HTTP Sessions: The L2 Session Data Cache

If your chosen persistence technology is slow, it can be helpful to locally cache the session data.
The `CachingSessionDataStore` is a special type of `SessionDataStore` that locally caches session data, which makes reads faster. It writes-through to your chosen type of `SessionDataStore` when session data changes.

=== MemcachedSessionDataMap

The `MemcachedSessionDataMap` uses `memcached` to perform caching of `SessionData`.

To enable it with the Jetty distribution, enable the `session-store-cache` module, along with your chosen `session-store-xxxx` module.

==== Configuration

The `$JETTY_BASE/start.d/session-store-cache.ini` contains the following configurable properties:

jetty.session.memcached.host::
Default value is `localhost`.
This is the host on which the memcached server resides.

jetty.session.memcached.port::
Default value is `11211`.
This is the port on which the memcached server is listening.

jetty.session.memcached.expirySec::
Default value `0`.
This is the length of time in seconds that an item can remain in the memcached cache, where 0 indicates indefinitely.

jetty.session.memcached.heartbeats::
Default value `true`.
Whether the memcached system should generate heartbeats.

[[usecases]]
== Session Scenarios

=== Minimizing Support for Sessions

The standard support for web applications in Jetty will use sessions cached in memory, but not persisted/clustered, with a scavenge for expired sessions that occurs every 10 minutes.
If you wish to pare back support for sessions because you know your app doesn't use them (or use JSPs that use them), then you can do the following:

* enable the <<base,base sessions module>> and <<base,configure the scavenge interval>> to 0 to prevent scavenging
* enable the <<cache-null,null session cache module>> to prevent sessions being cached in memory

If you wish to do any further minimization, you should consult the xref:programming-guide:server/session.adoc[Programming Guide].

=== Clustering with a Sticky Load Balancer

Preferably, your cluster will utilize a sticky load balancer.
This will route requests for the same session to the same Jetty instance.
In this case, the <<cache-hash,`DefaultSessionCache`>> can be used to keep in-use session objects <<cache-hash,in memory>>.
You can fine-tune the cache by controlling how long session objects remain in memory with the <<cache-hash,eviction policy settings>>.

If you have a large number of sessions or very large session objects, then you may want to manage your memory allocation by controlling the amount of time session objects spend in the cache.
The `EVICT_ON_SESSION_EXIT` eviction policy will remove a session object from the cache as soon as the last simultaneous request referencing it exits.
Alternatively, the `EVICT_ON_INACTIVITY` policy will remove a session object from the cache after a configurable amount of time has passed without a request referencing it.

If your sessions are very long lived and infrequently referenced, you might use the `EVICT_ON_INACTIVITY_POLICY` to control the size of the cache.

If your sessions are small, or relatively few or stable in number or they are read-mostly, then you might select the `NEVER_EVICT` policy.
With this policy, session objects will remain in the cache until they either expire or are explicitly invalidated.

If you have a high likelihood of simultaneous requests for the same session object, then the `EVICT_ON_SESSION_EXIT` policy will ensure the session object stays in the cache as long as it is needed.

=== Clustering Without a Sticky Load Balancer

Without a sticky load balancer requests for the same session may arrive on any node in the cluster.
This means it is likely that the copy of the session object in any `SessionCache` is likely to be out-of-date, as the session was probably last accessed on a different node.
In this case, your choices are to use either the <<cache-null,`NullSessionCache`>> or to de-tune the <<cache-hash,`DefaultSessionCache`>>.
If you use the `NullSessionCache` all session object caching is avoided.
This means that every time a request references a session it must be read in from persistent storage.
It also means that there can be no sharing of session objects for multiple requests for the same session: each will have their own independent session object.
Furthermore, the outcome of session writes are indeterminate because the Servlet Specification does not mandate ACID transactions for sessions.

If you use the `DefaultSessionCache`, there is a risk that the caches on some nodes will contain out-of-date session information as simultaneous requests for the same session are scattered over the cluster.
To mitigate this somewhat you can use the `EVICT_ON_SESSION_EXIT` eviction policy: this will ensure that the session is removed from the cache as soon as the last simultaneous request for it exits.
Again, due to the lack of session transactionality, the ordering outcome of write operations cannot be guaranteed.
As the session is cached while at least one request is accessing it, it is possible for multiple simultaneous requests to share the same session object.

=== Handling Corrupted or Unreadable Session Data

For various reasons it might not be possible for the `SessionDataStore` to re-read a stored session.
One scenario is that the session stores a serialized object in its attributes, and after a re-deployment there in an incompatible class change.
Setting the `$JETTY_BASE/start.d/session-cache-hash.ini` or `$JETTY_BASE/start.d/session-cache-null.ini` property `jetty.session.removeUnloadableSessions` to `true` will allow the unreadable session to be removed from persistent storage.
This can be useful for preventing the <<base-scavenge,scavenger>> from continually generating errors on the same expired, but un-readable session.

---

### Documentation Jetty Modules Operations Guide Pages Start Index (`documentation/jetty/modules/operations-guide/pages/start/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Jetty Start Mechanism

NOTE: Make sure you have read the xref:arch/index.adoc[Jetty architecture section] if you are not familiar with the terms used in this section.

The Jetty start mechanism is invoked by executing `$JETTY_HOME/start.jar`, from within a `$JETTY_BASE` directory, with zero or more command line options:

[source,bash]
----
$ cd $JETTY_BASE
$ java -jar $JETTY_HOME/start.jar ...
----

The Jetty start mechanism has two main modes of operation:

* The _tool_ mode, detailed in <<configure,this section>>, when it is used as a command line tool to configure the `$JETTY_BASE` directory by enabling modules, creating sub-directories and files, downloading files, etc.
In this mode, the JVM started with `java -jar $JETTY_HOME/start.jar` performs the specified command and then exits.
* The _start_ mode, detailed in <<start,this section>>, when it is used to start the JVM that runs Jetty with the specified configuration.
In this mode, the JVM started with `java -jar $JETTY_HOME/start.jar` starts Jetty and does not exit until stopped, for example by hitting kbd:[Ctrl+C] on the terminal.

Refer to the <<reference,Jetty start mechanism reference section>> for the complete list of the available command line options.

You want to use the Jetty start mechanism to <<configure,configure your $JETTY_BASE>> and then to <<start,start Jetty>>.

[[configure]]
== Configuring $JETTY_BASE

Within the Jetty start mechanism, the source of configurations is layered in this order, from higher priority to lower priority:

* The command line options.
* The `$JETTY_BASE` directory, and its files.
* The directory specified with the `--add-config-dir` option, and its files.
* The `$JETTY_HOME` directory, and its files.

[[configure-enable]]
=== Enabling Modules

You can enable Jetty modules persistently across restarts with the `--add-modules` command:

[source,bash]
----
$ java -jar $JETTY_HOME/start.jar --add-modules=https,http2
----

The Jetty start mechanism will look for the specified modules following the order specified above.
In the common case (without a `--add-config-dir` directory), it will look in `$JETTY_BASE/modules/` first and then in `$JETTY_HOME/modules/`.

Since the `https` and `http2` modules are standard Jetty modules, they are present in `$JETTY_HOME/modules/` and loaded from there.

When you enable a Jetty module, the Jetty start mechanism:

* Creates the correspondent `+$JETTY_BASE/start.d/*.ini+` module configuration file.
The content of these `+*.ini+` files is copied from the `[ini-template]` section of the correspondent `+*.mod+` file.
* Executes the directives specified in `[files]` section (if present) of the `+*.mod+` file.
This may simply create a file or a directory, or download files from the Internet.

Both steps are performed transitively for all module dependencies.

For example, enabling the `http` module results in the `$JETTY_BASE` directory to have the following structure:

----
$JETTY_BASE
├── resources
│   └── jetty-logging.properties
└── start.d
    ├── bytebufferpool.ini
    ├── http-config.ini
    ├── http.ini
    ├── scheduler.ini
    ├── server.ini
    └── threadpool.ini
----

The `$JETTY_BASE/resources/jetty-logging.properties` is created by the `[files]` directives of the `logging-jetty` module, which is a transitive dependency of the `server` module.

[[configure-disable]]
=== Disabling Modules

A module is enabled because the correspondent `+$JETTY_BASE/start.d/*.ini+` file contains a `--module=<name>` directive.

Commenting out the `--module=<name>` directive effectively disables the module.

Deleting the correspondent `+$JETTY_BASE/start.d/*.ini+` file also disables the module.

[[configure-edit-ini]]
=== Editing `+*.ini+` Files

You can now edit the `+$JETTY_BASE/start.d/*.ini+` configuration files, typically by uncommenting properties to change their default value.

You want to edit the `+$JETTY_BASE/start.d/*.ini+` configuration files so that the configuration is applied every time Jetty is started (or re-started).

For example, `$JETTY_BASE/start.d/http.ini` contains the following property, commented out:

.http.ini
----
# jetty.http.port=8080
----

You can change the clear-text HTTP port Jetty listens to by uncommenting that property and changing its value:

.http.ini
----
jetty.http.port=9876
----

When Jetty is started (or re-started) this configuration is applied and Jetty will listen for clear-text HTTP/1.1 on port `9876`.

[[configure-module-command-line]]
=== Enabling Modules on Command Line

You can also enable a module transiently, only for the current execution of the `java -jar $JETTY_HOME/start.jar` command.

If you have an empty `$JETTY_BASE`, the following command enables the `http` module, but does not create any `+$JETTY_BASE/start.d/*.ini+` files.

[source,bash]
----
$ java -jar $JETTY_HOME/start.jar --module=http
----

Since there are no `+$JETTY_BASE/start.d/*.ini+` files, you can only customize the properties via the command line, for example:

[source,bash]
----
$ java -jar $JETTY_HOME/start.jar --module=http jetty.http.port=9876
----

Enabling modules on the command line is useful to verify that the modules work as expected, or to try different configurations.

NOTE: It is possible to enable some module persistently via `--add-modules` and some other module transiently via `--module`.

Remember that once the current execution terminates, the modules enabled transiently on the command line via `--module` and their configuration are not saved and will not be enabled on the next execution (unless you specify them again on the command line).

[[configure-properties-command-line]]
=== Specifying Properties on Command Line

You can also specify module properties transiently, only for the current execution of the `java -jar $JETTY_HOME/start.jar` command.

Properties specified on the command line override the same properties specified in `+*.ini+` files.

For example, you can enable the `server` module in `$JETTY_BASE` and the `http` module on the command line, and specify properties for both on the command line:

[source,bash]
----
# Enable the server module in $JETTY_BASE.
$ java -jar $JETTY_HOME/start.jar --add-modules=server

# Start Jetty enabling the http module on the command line,
# and specifying properties for both modules.
$ java -jar $JETTY_HOME/start.jar --module=http jetty.server.dumpAfterStart=true jetty.http.port=9876
----

[[configure-custom-module]]
=== Adding Your Own Modules

NOTE: Refer to the xref:modules/custom.adoc[custom module section] for the details about how to create your own modules.

You can add your own modules by adding a `+$JETTY_BASE/modules/*.mod+` file.

For example, you may want to add a Postgres JDBC driver to the server class-path, to avoid that each deployed web application bring its own version. This allows you to control the exact Postgres JDBC driver version for all web applications.

Create the `$JETTY_BASE/modules/postgresql.mod` file:

.postgresql.mod
----
include::code:example$jetty-modules/postgresql.mod[]
----

Then enable it:

[source,bash]
----
$ java -jar $JETTY_HOME/start.jar --add-modules=postgresql
----

Enabling the `postgresql` module will execute the `[files]` directive (downloading the `+*.jar+` file from Maven Central if not already present) and create the `$JETTY_BASE/start.d/postgresql.ini` with the content of the `[ini-template]` section.

The `[lib]` section ensures that the specified file is in the server class-path when Jetty is started.

You can <<configure-display,display the Jetty configuration>> to verify that the server class-path is correct.

[[configure-custom-module-exec]]
=== Custom Module with JVM Options

Using a custom Jetty module, you can customize the JVM startup options.

This is useful if you need to start Jetty and want to specify JVM options such as:

* `+-Xmx+`, to specify the max heap size
* `+-Xlog:gc+`, to specify the GC log file and options
* `+-javaagent+`, to specify Java agents
* `+-XX:+` options, for example to specify the GC implementation
* `+--enable-preview+`, to enable Java preview features

Start by creating `$JETTY_BASE/modules/jvm.mod`:

.jvm.mod
----
include::code:example$jetty-modules/jvm.mod[]
----

Enable it:

[source,bash]
----
$ java -jar $JETTY_HOME/start.jar --add-modules=jvm
----

Since the module defines an `[exec]` section, it will fork _another_ JVM when Jetty is started.

This means that when you start Jetty, there will be _two_ JVMs running: one created by you when you run `java -jar $JETTY_HOME/start.jar`, and another forked by the Jetty start mechanism with the JVM options you specified (that cannot be applied to an already running JVM).

Again, you can <<configure-dry-run,display the JVM command line>> to verify that it is correct.

[TIP]
====
The second JVM forked by the Jetty start mechanism when one of the modules requires forking, for example a module that contains an `[exec]` section, may not be desirable, and may be avoided as explained in <<configure-dry-run,this section>>.
====

[[configure-display]]
=== Displaying the Configuration

Once you have enabled and configured the `$JETTY_BASE`, you can display the configuration to verify that it is correct.

Using the standard `server` and `http` Jetty modules, and the `postgresql` and `jvm` custom Jetty module defined above, you obtain:

[source,bash]
----
$ java -jar $JETTY_HOME/start.jar --list-config
----

[jetty%nowrap]
....
[jetty]
setupModules=code:example$jetty-modules/jvm.mod,code:example$jetty-modules/postgresql.mod
setupArgs=--add-modules=server,http,postgresql,jvm
args=--list-config
....

Note how the configuration displayed above includes:

* In the list of enabled modules, the `postgresql` and `jvm` modules
* In the list of JVM arguments, those specified by the `jvm` module
* In the server class-path, the `+*.jar+` file specified by the `postgresql` module

[[configure-dry-run]]
=== Displaying the JVM Command Line

The Jetty start mechanism can display a full JVM command line that will start Jetty with the configuration you specified, with the `--dry-run` option:

[source,bash]
----
$ java -jar $JETTY_HOME/start.jar --dry-run
----

The full JVM command line generated by `--dry-run` can be split in various parts that can be used individually, for example in scripts.

Furthermore, Jetty modules may specify the `--exec` option that will fork a second JVM to start Jetty, which may not be desirable.
Some option, such as `--jpms`, imply `--exec`, as it won't be possible to modify the module-path in the already started JVM.

To start Jetty without forking a second JVM, the `--dry-run` option can be used to generate a command line that is then executed so that starting Jetty only spawns one JVM.

IMPORTANT: You can use the `--dry-run` option as explained below to avoid forking a second JVM when using modules that have the `[exec]` section, or the `--exec` option, or when using the `--jpms` option.

For example, using the `--dry-run` option with the `jvm.mod` introduced in <<configure-custom-module-exec,this section>> produces the following command line:

[source,bash]
----
$ java -jar $JETTY_HOME/start.jar --dry-run
----

[jetty%nowrap]
....
[jetty]
setupModules=code:example$jetty-modules/jvm.mod
setupArgs=--add-modules=http,jvm
args=--dry-run
replace=( ),$1\\\n
....

You can then run the generated command line by copying the output generated using the `--dry-run` option, and pasting it in a terminal window.

The `--dry-run` option is quite flexible and below you can find a few examples of how to use it to avoid forking a second JVM, or generating scripts or creating an arguments file that can be passed to (a possibly alternative) `java` executable.

To display the `java` executable used to start Jetty:

[source,bash,subs=+quotes]
----
$ java -jar $JETTY_HOME/start.jar --dry-run=##java##
----

[jetty%nowrap]
....
[jetty]
setupArgs=--add-modules=http
args=--dry-run=java
....

To display the JVM options:

[source,bash,subs=+quotes]
----
$ java -jar $JETTY_HOME/start.jar --dry-run=##opts##
----

[jetty%nowrap]
....
[jetty]
setupModules=code:example$jetty-modules/jvm.mod
setupArgs=--add-modules=http,jvm
args=--dry-run=opts
replace=( ),$1\\\n
....

To display the JVM class-path:

[source,bash,subs=+quotes]
----
$ java -jar $JETTY_HOME/start.jar --dry-run=##path##
----

[jetty%nowrap]
....
[jetty]
setupModules=code:example$jetty-modules/postgresql.mod
setupArgs=--add-modules=http,jvm
args=--dry-run=path
replace=( |:),$1\\\n
....

To display the JVM class-path and module-path, if you want to xref:start/start-jpms.adoc[start Jetty using JPMS] with the `--jpms` option:

[source,bash,subs=+quotes]
----
$ java -jar $JETTY_HOME/start.jar ##--jpms## --dry-run=##path##
----

[jetty%nowrap]
....
[jetty]
setupModules=code:example$jetty-modules/postgresql.mod
setupArgs=--add-modules=http,jvm
args=--jpms --dry-run=path
replace=( |:),$1\\\n
....

To display the JVM main class:

[source,bash,subs=+quotes]
----
$ java -jar $JETTY_HOME/start.jar --dry-run=##main##
----

[jetty%nowrap]
....
[jetty]
setupArgs=--add-modules=http
args=--dry-run=main
....

To display the JVM main class when xref:start/start-jpms.adoc[starting Jetty using JPMS]:

[source,bash,subs=+quotes]
----
$ java -jar $JETTY_HOME/start.jar --jpms --dry-run=##main##
----

[jetty%nowrap]
....
[jetty]
setupArgs=--add-modules=http
args=--jpms --dry-run=main
....

The main class is typically Jetty's `XmlConfiguration` class that accepts, as program arguments, a list of properties and a list of Jetty XML files to process.
The Jetty XML files compose together the Jetty components that are then configured with the values from the command line properties.

To display the program arguments passed to the main class:

[source,bash,subs=+quotes]
----
$ java -jar $JETTY_HOME/start.jar --dry-run=##args##
----

[jetty%nowrap]
....
[jetty]
setupModules=code:example$jetty-modules/postgresql.mod
setupArgs=--add-modules=http
args=--dry-run=args
replace=( ),$1\\\n
....

Note how the program arguments are a list of properties in the form `<name>=<value>` and a list of Jetty XML files.

To display the xref:deploy/index.adoc[environments] configuration:

[source,bash,subs=+quotes]
----
$ java -jar $JETTY_HOME/start.jar --dry-run=##envs##
----

[jetty%nowrap]
....
[jetty]
setupModules=code:example$jetty-modules/postgresql.mod
setupArgs=--add-modules=http,ee10-deploy
args=--dry-run=args
replace=( ),$1\\\n
....

Note how the environments configuration specifies additional class-path and Jetty XML files for every environment.

The various parts of the full JVM command line can be combined to leverage the JVM arguments file feature (that is, specify the JVM options in a file rather than on the command line) that is built-in in the `java` executable:

[source,bash,subs=+quotes]
----
$ java -jar $JETTY_HOME/start.jar --dry-run=##opts,path,main,args,envs## > /tmp/jvm_cmd_line.txt
$ /some/other/java @/tmp/jvm_cmd_line.txt
----

Using `--dry-run=opts,path,main,args,envs` can be used to avoid that the Jetty start mechanism forks a second JVM when using modules that require forking.

In the Linux `bash` shell you can run the output of `--dry-run` in this way:

[source,bash]
----
$ java -jar $JETTY_HOME/start.jar --dry-run=opts,path,main,args,envs | xargs java
----

The output of different `--dry-run` executions can be creatively combined in a script, for example in the Linux `bash` shell:

[source,bash,subs=+quotes]
----
$ OPTS=$(java -jar start.jar --dry-run=##opts,path##)
$ MAIN=$(java -jar start.jar --dry-run=##main##)
$ ARGS=$(java -jar start.jar --dry-run=##args##)
$ ENVS=$(java -jar start.jar --dry-run=##envs##)
$ echo $OPTS -Dextra=opt $MAIN $ARGS $ENVS extraProp=value extra.xml | xargs java
----

[[start]]
== Starting Jetty

After you have configured the `$JETTY_BASE` directory, as explained in <<configure,this section>>, you can start Jetty as a standalone server.

In the _start_ mode, the Jetty start mechanism computes a JVM command line with JVM options, system properties, class-path, module-path, main class and program arguments, and then executes it, forking a new JVM if necessary.

The Jetty start mechanism performs these steps:

. Loads all the Jetty modules files (that have extension `+*.mod+`) from the `modules/` subdirectory of each configuration source directory (see <<configure,this section>> for the list of configuration sources).
In this way, a Jetty module graph can be built in memory, where the module dependencies form the edges of the graph and each node contains the metadata information declared by each module (for example, the libraries that it needs, the XML files to process, and so on), in preparation for the next step.
. Reads the Jetty module configuration files (that have extension `+*.ini+`) from the `start.d/` subdirectory of each configuration source directory and from the command line.
This step produces a list of _enabled_ modules; for each enabled module all its dependencies are transitively resolved by navigating the graph built in the previous steps.
. Processes the list of enabled (explicitly and transitively) modules, gathering the list of libraries to add to the class-path or module-path, the JPMS directives to add to the command line if necessary, the properties and XML files to add as program arguments, etc., so that a full JVM command line can be generated.
. Executes the command line, either in-JVM or by forking a second JVM (if the `--exec` option is present or implied by other options such as `--jpms`), and waits for the JVM, or the forked JVM, to exit.

[[start-class-path]]
=== Server Class-Path

When the Jetty server is started in-JVM, the server class-path gathered by processing the enabled modules is organized in a `URLClassLoader`, the Jetty Start ClassLoader, that is a child of the System ClassLoader:

[plantuml]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false
skinparam roundCorner 10

rectangle "Jetty Start JVM" {
  rectangle "System ClassLoader" as system
  rectangle "Jetty Start ClassLoader" as start

  note right of system: start.jar
  note right of start: jetty-server.jar\njetty-http.jar\njetty-io.jar\njetty-util.jar\njetty-xml.jar\netc.

  system <.. start
}
----

The System ClassLoader only has `$JETTY_HOME/start.jar` in its class-path, since the JVM was started with `java -jar $JETTY_HOME/start.jar`.
The Jetty Start ClassLoader has in its class-path the `+*.jar+` files gathered by processing the enabled modules, typically from `+$JETTY_HOME/lib/jetty-*.jar+`, but possibly also from `+$JETTY_BASE/lib/*.jar+` if custom modules extend the server class-path with their own `+*.jar+` files.

When the Jetty server is started in a forked JVM, there will be two JVMs: one started by you with `java -jar $JETTY_HOME/start.jar` and one forked by the Jetty start mechanism.
In the forked JVM, the System ClassLoader has the server class-path and/or module-path in its class-path, since the forked JVM is started with `+java --class-path $JETTY_HOME/lib/jetty-server-<version>.jar:...+`:

[plantuml]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false
skinparam roundCorner 10

rectangle "Jetty Start JVM" as startJVM {
  rectangle "System ClassLoader" as system1
  note right of system1: start.jar
}

rectangle "Forked JVM" as forkedJVM {
  rectangle "System ClassLoader" as system2

  note right of system2: jetty-server.jar\njetty-http.jar\njetty-io.jar\njetty-util.jar\njetty-xml.jar\netc.
}

startJVM --> forkedJVM: " waits for"
----

It is worth mentioning that there are two standard Jetty modules that allow you to easily add entries to the Jetty server class-path:

* The xref:modules/standard.adoc#resources[`resources` module], which adds the `$JETTY_BASE/resources` directory to the server class-path.
This is useful if you have third party libraries that lookup resources from the class-path: just put those resources in the `$JETTY_BASE/resources/` directory. +
Logging libraries often perform class-path lookup of their configuration files (for example, `log4j.properties`, `log4j.xml`, `logging.properties`, and `logback.xml`), so `$JETTY_BASE/resources/` is the ideal place to add those files. +
* The `ext` module, that adds all the `+*.jar+` files under the `$JETTY_BASE/lib/ext/` directory, and subdirectories recursively, to the server class-path. +
+
[CAUTION]
====
On one hand, the `ext` module provides a handy place to put third party libraries and their dependencies; on the other hand, the `$JETTY_BASE/lib/ext/` directory may become a confused mixture of many `+*.jar+` files from different third party libraries.

Prefer to group third party libraries and their dependencies into their own directories using xref:modules/custom.adoc[custom modules], or at least group them into `$JETTY_BASE/lib/ext/` subdirectories such as `$JETTY_BASE/lib/ext/util/` or `$JETTY_BASE/lib/ext/acme/`.
====

[[start-class-path-environments]]
==== Server Jakarta EE Environment Class-Path

When a Jakarta EE Jetty module is enabled, for example the `{ee-current}-webapp` and/or the `{ee-current}-deploy` module, the server ClassLoader hierarchy is modified as follows:

[plantuml,subs=+attributes]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false
skinparam roundCorner 10

rectangle "Jetty Start JVM" {
  rectangle "System ClassLoader" as system
  rectangle "Jetty Start ClassLoader" as start
  rectangle "Jetty {ee-current-caps} ClassLoader" as env

  note right of system: start.jar
  note right of start: jetty-server.jar\njetty-http.jar\njetty-io.jar\njetty-util.jar\njetty-xml.jar\netc.
  note right of env: jakarta.servlet-api.jar\njetty-{ee-current}-servlet.jar\njetty-{ee-current}-deploy.jar\netc.

  system <.. start
  start <.. env
}
----

When different versions of the Jakarta EE `ee<N>-deploy` or `ee<N>-webapp` modules are enabled, the server ClassLoader hierarchy is the following:

[plantuml,subs=+attributes]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false
skinparam roundCorner 10

rectangle "Jetty Start JVM" {
  rectangle "System ClassLoader" as system
  rectangle "Jetty Start ClassLoader" as start
  rectangle "Jetty {ee-prev-caps} ClassLoader" as env1
  rectangle "Jetty {ee-current-caps} ClassLoader" as env2

  note right of system: start.jar
  note right of start: jetty-server.jar\njetty-http.jar\njetty-io.jar\njetty-util.jar\njetty-xml.jar\netc.
  note left of env1: jakarta.servlet-api.jar ({ee-prev})\njetty-{ee-prev}-servlet.jar\njetty-{ee-prev}-deploy.jar\netc.
  note right of env2: jakarta.servlet-api.jar ({ee-current})\njetty-{ee-current}-servlet.jar\njetty-{ee-current}-deploy.jar\netc.

  system <.. start
  start <.. env1
  start <.. env2
}
----

Note how the Jetty {ee-prev-caps} ClassLoader and the Jetty {ee-current-caps} ClassLoader are siblings.
In this way, they can load independently their own version of the Jakarta EE APIs `+*.jar+` files, and the Jetty implementation of the Jakarta EE APIs, without conflicts.

[[start-xml]]
=== Assembling Jetty Components

The Jetty start mechanism eventually invokes, by default, main class `org.eclipse.jetty.xml.XmlConfiguration`, passing properties and xref:xml/index.adoc[Jetty XML files] as program arguments.

The Jetty XML files are nothing more than Java code in XML format.

The XML files are processed to instantiate Jetty components such as `org.eclipse.jetty.server.Server` or `org.eclipse.jetty.util.ssl.SslContextFactory$Server`.
The components are then assembled together to provide the configured Jetty features.

The Jetty XML files are parametrized using properties, and a property is just a name/value pair.

This parametrization of the XML files allows an XML file that resides in `$JETTY_HOME/etc/` to _declare_ a property such as `jetty.http.port`, and allow this property to be set in a `$JETTY_BASE/start.d/http.ini` file, so that you don't need to change the XML files in `$JETTY_HOME`, but only change `+*.ini+` files in your `$JETTY_BASE`.

You can write your own xref:modules/custom.adoc[custom modules] with your own Jetty XML files, and your own properties, to further customize Jetty.

[[stop]]
== Stopping Jetty

When Jetty is started, the Jetty components that you have configured by enabling Jetty modules are assembled and started.

If you have started Jetty from a terminal, you can exit the Jetty JVM by hitting kbd:[Ctrl+C] on the same terminal.

Similarly, from a different terminal, you can exit the Jetty JVM using `kill -INT <pid>` or `kill -TERM <pid>`.

In the three cases above, the JVM is exited, but by default Jetty components are not stopped.
If you want to stop the Jetty components, to stop Jetty more gracefully, you can start Jetty with this property:

[source,bash,subs=+quotes]
----
$ java -jar $JETTY_HOME/start.jar ##jetty.server.stopAtShutdown=true##
----

This property can also be set in `$JETTY_BASE/start.d/server.ini` so that it is persistently configured across Jetty restarts (see also xref:modules/standard.adoc#server[the `server` module]).

The `jetty.server.stopAtShutdown` property configures a JVM shutdown hook that is run, stopping the `Server` instance, when the JVM exits.

Obviously, the JVM can also be stopped with `kill -KILL <pid>` that exits the process abruptly without running the JVM shutdown hooks.

[[stop-graceful]]
=== Stopping Gracefully

Stopping Jetty abruptly when there are active HTTP requests being handled may result in a variety or errors, because Jetty components that are used to handle requests are being stopped concurrently.

For example, when the Jetty thread pool is stopped, an attempt to submit a task would throw `RejectedExecutionException`; when a component is stopped, its fields may be nulled-out, resulting in a `NullPointerException` being thrown if the component is used; etc.

You can stop Jetty _gracefully_ by adding the `graceful` Jetty module (see xref:modules/standard.adoc#graceful[this section] for more information).

When Jetty is stopped, the `graceful` module organizes to reject new requests, but allows existing requests to finish within a configurable timeout; then closes all the connections, stops all the Jetty components, and then exits the JVM.
In this way, existing requests are not responded with an error caused by the server stopping, provided they complete within the timeout.

[[stop-remote]]
=== Stopping Jetty from Remote

You can configure a Jetty server so that it can be stopped by remote clients using a command sent through a TCP socket.

You can start Jetty with the following properties:

* `stop.host`, the host name Jetty will bind to listen for stop commands. Defaults to `127.0.0.1` which means that the stop command can be issued only clients that run on the same host as Jetty.
* `stop.port`, the port number Jetty will listen to for stop commands. Defaults to `-1`, which means that Jetty will not listen to any port.
* `stop.key`, the password to verify when a stop command is received. Defaults to a password that is randomly generated and printed when Jetty starts.

[source,bash,subs=+quotes]
----
$ java -jar $JETTY_HOME/start.jar ##stop.port=8181##
----

[jetty%nowrap]
....
[jetty]
setupArgs=--add-modules=http
args=stop.port=8181
highlight=(?i)stop.key
....

In the example above, Jetty is started with just the `stop.port` property, and the `stop.key` is printed on the terminal when Jetty starts.

CAUTION: You can choose your own `stop.key`, but make sure it's a strong password.

A remote client can now use the Jetty start mechanism to stop the remote Jetty server:

[source,bash,subs=+quotes]
----
$ java -jar $JETTY_HOME/start.jar ##--stop## stop.port=8181 stop.key=<stop.key>
----

Note the `--stop` command along with the `stop.port` and `stop.key` properties.
The `stop.key` must be the same as the one of remote Jetty server, either the one you chose, or the one printed on the terminal when Jetty starts.

Remote clients can wait for the remote Jetty server to shut down by specifying the `stop.wait` property with the number of seconds to wait:

[source,bash]
----
$ java -jar $JETTY_HOME/start.jar --stop stop.port=8181 stop.key=<stop.key> stop.wait=15
----

If the time specified elapses, without the confirmation that the remote Jetty server stopped, then the `--stop` command exits with a non-zero return code.

== Start Mechanism Logging

The steps performed by the Jetty start mechanism are logged by the `StartLog` class, that outputs directly, by default, to `System.err`.

This is necessary to avoid that the Jetty start mechanism depend on logging libraries that may clash with those defined by Jetty logging modules, when Jetty is started in-VM.

[NOTE]
====
This section is about the logging performed by the Jetty start mechanism _before_ it configures and starts Jetty.
See the xref:server/index.adoc#logging[logging section] for information about logging when Jetty starts.
====

You can enable DEBUG level logging with the `--debug` command line option, for both the _tool_ and _start_ modes:

[source,bash]
----
$ java -jar $JETTY_HOME/start.jar --debug ...
----

You can send the start log output to a file, by default relative to `$JETTY_BASE`, with the `--start-log-file=<file>` option:

[source,bash]
----
$ java -jar $JETTY_HOME/start.jar --debug --start-log-file=start.log ...
----

This is useful for capturing startup issues where the Jetty-specific logger has not yet kicked in due to a possible startup configuration error.

[[reference]]
== Usage Reference

[jetty]
....
[jetty]
args=--help
....

---

### Documentation Jetty Modules Operations Guide Pages Start Start Jpms (`documentation/jetty/modules/operations-guide/pages/start/start-jpms.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Starting Jetty using JPMS

Jetty modules are proper https://en.wikipedia.org/wiki/Java_Platform_Module_System[JPMS] modules: each Jetty module has a `module-info.class` file.
This makes possible to run Jetty from the module-path, rather than the class-path.

To start Jetty on the module-path rather than the class-path, it is enough to add the `--jpms` option to the command line, for example:

[source,bash]
----
$ java -jar $JETTY_HOME/start.jar --jpms
----

[NOTE]
====
The `--jpms` option implies the `--exec` option.

When running on the module-path using the `--jpms` option, the Jetty start mechanism will fork a second JVM passing it the right JVM options to run on the module-path.

Therefore, you will have two JVMs running: one that runs `start.jar` and one that runs Jetty on the module-path.

Forking a second JVM may be avoided as explained in xref:start/index.adoc#configure-dry-run[this section].
====

When Jetty is started in JPMS mode, all JPMS modules in the module-path are added to the set of JPMS _root modules_ through the JVM option `--add-modules=ALL-MODULE-PATH`.

For a `+*.jar+` file that is not a JPMS module, but is on the module-path, the JVM will assume internally it is an automatic JPMS module, with a JPMS module name derived from the `+*.jar+` file name.

As an alternative to adding the `--jpms` option to the command line, you can use a custom Jetty module to centralize your JPMS configuration, where you can specify additional JPMS directives.

Create the `$JETTY_BASE/modules/jpms.mod` file:

.jpms.mod
----
include::code:example$jetty-modules/jpms.mod[]
----

The `[ini]` section with `--jpms` is equivalent to passing the `--jpms` option to the command line (see also xref:modules/index.adoc#directive-ini[this section]).

The `[jpms]` section allows you to specify additional JPMS configuration, for example additional `--add-modules` options, or `--add-opens` options, etc. (see also xref:modules/index.adoc#directive-jpms[this section]).

Then enable it:

[source,bash]
----
$ java -jar $JETTY_HOME/start.jar --add-modules=jpms
----

Now you can start Jetty without extra command line options, and it will start in JPMS mode because you have enabled the `jpms` module.

[[advantages]]
== Advantages of JPMS

The main advantage of running Jetty in JPMS mode is that it provides early error reporting if a dependency is missing, especially in case of custom Jetty modules.

If a Jetty module requires a specific library `+*.jar+` file, and the `+*.jar+` file is missing, this will be detected early when running in JPMS mode, since the `+*.jar+` file would be a JPMS module, and the JPMS module graph resolution would detect this problem as the JVM starts up.
When running in class-path mode, this problem can only be detected when the library classes are loaded, therefore much later at run-time, when an HTTP request arrives and its processing requires to load the library classes.

Similarly, duplicate `+*.jar+` files, of possibly different versions, are not allowed by JPMS.
This is again detected early, at JVM startup, when running in JPMS mode, rather than passing completely unnoticed when running in class-path mode, where classes may be loaded from either of those `+*.jar+` files, possibly from the wrong version.

Another advantage of running Jetty in JPMS mode is that JPMS provides strong encapsulation.
This means that it is not possible to use reflection to access non-exported classes in JPMS modules within the `ModuleLayer` hierarchy.
This is enforced at the JVM level, not at the application level through, for example, a custom `ClassLoader` that performs class loading filtering.

It also means that it is not possible to use reflection to access sibling ``ModuleLayer``s.
For example a Jakarta {ee-prev-caps} web application would not be able to use reflection to access a sibling Jakarta {ee-current-caps} web application.

[[hierarchy]]
== Server Module-Path and ModuleLayers

When the Jetty server is started in JPMS mode, there will be two JVMs: one started by you with `java -jar $JETTY_HOME/start.jar` and one forked by the Jetty start mechanism.

NOTE: Remember that you can avoid to fork a second JVM, as described in xref:start/index.adoc#configure-dry-run[this section].

The forked JVM is started with `+java --module-path $JETTY_HOME/lib/jetty-server-<version>.jar:...+`, and therefore has the server libraries in its module-path, loaded by the System ClassLoader, in the boot `ModuleLayer`:

[plantuml]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false
skinparam roundCorner 10

rectangle "Jetty Start JVM" as startJVM {
  rectangle "System ClassLoader" as system1
  note right of system1: start.jar
}

rectangle "Forked JVM" as forkedJVM {
  rectangle "Boot ModuleLayer" as bootLayer {
    rectangle "System ClassLoader" as system2

    note right of system2: jetty-server.jar\njetty-http.jar\njetty-io.jar\njetty-util.jar\njetty-xml.jar\netc.
  }
}

startJVM --> forkedJVM: " waits for"
----

When a Jakarta EE Jetty module is enabled, for example the `{ee-current}-webapp` and/or the `{ee-current}-deploy` module, the forked JVM is configured as follows:

[plantuml,subs=+attributes]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false
skinparam roundCorner 10

rectangle "Forked JVM" as forkedJVM {
  rectangle "Boot ModuleLayer" as bootLayer {
    rectangle "System ClassLoader" as systemCL

    note right of systemCL: jetty-server.jar\njetty-http.jar\njetty-io.jar\njetty-util.jar\njetty-xml.jar\netc.
  }
  rectangle "Jetty {ee-current-caps} ModuleLayer" as envLayer {
    rectangle "Jetty {ee-current-caps} ClassLoader" as envCL

    note right of envCL: jakarta.servlet-api.jar\njetty-{ee-current}-servlet.jar\njetty-{ee-current}-deploy.jar\netc.
  }
}

bootLayer <-- envLayer
systemCL <.. envCL
----

Note how the parent of the Jetty {ee-current-caps} `ClassLoader` is the System `ClassLoader`, and how the parent of the Jetty {ee-current-caps} `ModuleLayer` is the Boot `ModuleLayer`.

When different versions of the Jakarta EE Jetty modules are enabled, the forked JVM is configured as follows:

[plantuml,subs=+attributes]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false
skinparam roundCorner 10

rectangle "Forked JVM" as forkedJVM {
  rectangle "Boot ModuleLayer" as bootLayer {
    rectangle "System ClassLoader" as systemCL

    note right of systemCL: jetty-server.jar\njetty-http.jar\njetty-io.jar\njetty-util.jar\njetty-xml.jar\netc.
  }
  rectangle "Jetty {ee-prev-caps} ModuleLayer" as envLayer1 {
    rectangle "Jetty {ee-prev-caps} ClassLoader" as envCL1

    note left of envCL1: jakarta.servlet-api.jar ({ee-prev})\njetty-{ee-prev}-servlet.jar\njetty-{ee-prev}-deploy.jar\netc.
  }
  rectangle "Jetty {ee-current-caps} ModuleLayer" as envLayer2 {
    rectangle "Jetty {ee-current-caps} ClassLoader" as envCL2

    note right of envCL2: jakarta.servlet-api.jar ({ee-current})\njetty-{ee-current}-servlet.jar\njetty-{ee-current}-deploy.jar\netc.
  }
}

bootLayer <-- envLayer1
bootLayer <-- envLayer2
systemCL <.. envCL1
systemCL <.. envCL2
----

Note how the Jetty EE ``ClassLoaders``s are siblings, and both have the System `ClassLoader` as parent.
Note also how the Jetty EE ``ModuleLayer``s are siblings, and both have the Boot `ModuleLayer` as parent.

[[advanced]]
== Advanced JPMS Configuration

Web applications may need additional services from the Servlet Container, such as JDBC `DataSource` references or JTA `UserTransaction` references.

For example, for JDBC it is typical to store, in JNDI, a reference to the connection pool's `DataSource` or directly a reference to the JDBC driver's `DataSource` (for example, `org.postgresql.ds.PGConnectionPoolDataSource`).
Jetty needs to be able to instantiate those classes and therefore needs to be able to load those classes and all their super-classes, among which includes `javax.sql.DataSource`.

When Jetty runs on the class-path, this is easily achieved by using a xref:modules/custom.adoc[custom module] as explained in xref:start/index.adoc#configure-custom-module[this section].

However, when running on the module-path, things are quite different.

When Jetty tries to load, for example, class `org.postgresql.ds.PGConnectionPoolDataSource`, it must be in a JPMS module that is resolved in the run-time module graph.
Furthermore, any dependency, for example classes from the `java.sql` JPMS module, must also be in a module present in the resolved module graph.

Thanks to the fact that when Jetty starts in JPMS mode the `--add-modules=ALL-MODULE-PATH` option is added to the JVM command line, every `+*.jar+` file in the module-path is also present in the module graph.

There are now two cases for the `postgresql-<version>.jar` file: either it is a proper JPMS module, or it is an automatic JPMS module (either an explicit automatic JPMS module with the `Automatic-Module-Name` attribute in the manifest, or an implicit automatic JPMS module whose name is derived from the `+*.jar+` file name).

If the `postgresql-<version>.jar` file is a proper JPMS module, then there is nothing more that you should do: the `postgresql-<version>.jar` file is in the module-path, and all the modules in the module-path are in the module graph (thanks to `--add-modules=ALL-MODULE-PATH`), and any dependency declared in the `module-info.class` will be added to the module graph.

Otherwise, `postgresql-<version>.jar` file is an automatic module, and will likely have a dependency on the JDK-bundled `java.sql` JPMS module.
Fortunately, when Jetty starts in JPMS mode, the `--add-modules=ALL-DEFAULT` option is also added to the JVM command line, so that all the Java JPMS modules are added to the module graph, and this would include the `java.sql` JPMS module.

Thanks to the fact that Jetty starts by default adding `--add-modules=ALL-DEFAULT,ALL-MODULE-PATH`, it is enough that you put custom libraries (and their dependencies) in the module-path, and you should not need any extra JPMS configuration.

However, you can explicitly configure JPMS directives in a custom Jetty module, for example to _open_ or _patch_ a JPMS module.
Using the `postgresql.mod` introduced in xref:start/index.adoc#configure-custom-module[this section] as an example, modify your custom Jetty module and add the JPMS directives you need, like so:

.postgresql.mod
----
[jpms]
add-modules: <module>(,<module>)*
patch-module: <module>=<file>(<path-separator><file>)*
add-opens: <source-module>/<package>=<target-module>(,<target-module>)*
add-exports: <source-module>/<package>=<target-module>(,<target-module>)*
add-reads: <source-module>=<target-module>(,<target-module>)*
----

The Jetty JPMS directives are equivalent to the JDK JPMS directives.

The `[jpms]` section is only used when Jetty is started on the module-path.

---

### Documentation Jetty Modules Operations Guide Pages Tools Index (`documentation/jetty/modules/operations-guide/pages/tools/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Jetty Tools

[[password]]
== Password Tool

The `org.eclipse.jetty.util.security.Password` class present in the `org.eclipse.jetty:jetty-util` artifact provides support for <<password-obfuscation,password obfuscation>> and <<password-checksum,password checksums>>.

[[password-obfuscation]]
=== Password Obfuscation

There are many cases where you might need to provide credentials such as usernames and passwords to authenticate your access to certain services, for example KeyStore and TrustStore passwords, JDBC credentials, Basic or Digest authentication credentials, etc.

Passwords are typically stored in clear-text in configuration files; a program such as Jetty reading the configuration file must be able to retrieve the original password to pass it to the service (for example, a KeyStore or a JDBC driver).

You can protect clear-text stored passwords from _casual view_ by obfuscating them using class link:{javadoc-url}/org/eclipse/jetty/util/security/Password.html[`org.eclipse.jetty.util.security.Password`]:

[,bash,subs=attributes+]
----
$ java -cp jetty-util-{jetty-version}.jar org.eclipse.jetty.util.security.Password --prompt
Username: <1>
Password: secret <2>
OBF:1yta1t331v8w1v9q1t331ytc <3>
MD5:5eBe2294EcD0E0F08eAb7690D2A6Ee69 <4>
...
MD:SHA-1:E5E9Fa1bA31eCd1aE84f75CaAa474f3a663f05F4
...
MD:SHA3-256:F5A5207a8729B1F709Cb710311751eB2Fc8aCaD5A1Fb8aC991B736E69b6529A3
...
----
<1> Hit kbd:[Enter] to specify a blank user.
<2> Enter the password you want to obfuscate.
<3> The obfuscated password.
<4> MD5 and other `MessageDigest` checksums of the password, using different algorithms.

The `Password` tool produced an obfuscated string for the password `secret`, namely `OBF:1yta1t331v8w1v9q1t331ytc` (the prefix `OBF:` must be retained).
The obfuscated string can be de-obfuscated to obtain the original password.

Now you can use the obfuscated password in Jetty configuration files, for example to specify the KeyStore password in `ssl-context.ini` when configuring secure connectors, as explained xref:protocols/index.adoc#ssl-customize[here].
For example:

.ssl-context.ini
[,properties]
----
jetty.sslContext.keyStorePassword=OBF:1yta1t331v8w1v9q1t331ytc
----

NOTE: Remember that password obfuscation only protects from _casual view_ -- it can be de-obfuscated to obtain the original password.

TIP: You can also use the obfuscated password in your Java source code.

[CAUTION]
====
Extreme care should be taken when using the `Password` tool from the command line, especially in cases where the username or password contains characters that may be interpreted by the shell, such as the character `$`, and the `--prompt` command line option is not used.

For example, running the following produces the wrong password:

[,bash,subs=attributes+]
----
$ java -cp jetty-util-{jetty-version}.jar org.eclipse.jetty.util.security.Password bob secret$phrase
----

where `bob` is the user, and `secret$phrase` is the desired password.

The shell may interpret `$phrase` as a shell variable, likely an empty string, so the `Password` class only receives `secret` as the password to obfuscate and hash.

Always use the `--prompt` command line option, or disable shell interpretation for the parameters, for example by surrounding the parameters with single quotes:

[,bash,subs=attributes+]
----
$ java -cp jetty-util-{jetty-version}.jar org.eclipse.jetty.util.security.Password 'bob' 'secret$phrase'
----
====

You can also use obfuscated passwords in Jetty XML files where a clear-text password is usually required.
Here is an example, setting an obfuscated password for a JDBC `DataSource`:

[,xml,subs=attributes+]
----
<New id="myDS" class="org.eclipse.jetty.plus.jndi.Resource">
  <Arg></Arg>
  <Arg>jdbc/myDS</Arg>
  <Arg>
    <New class="com.zaxxer.hikari.HikariDataSource">
      <Arg>
        <New class="com.zaxxer.hikari.HikariConfig">
          <Set name="dataSourceClassName">org.postgresql.ds.PGSimpleDataSource</Set>
          <Set name="username">dbuser</Set>
          <Set name="password">
            <Call class="org.eclipse.jetty.util.security.Password" name="deobfuscate"> 
              <Arg>OBF:1yta1t331v8w1v9q1t331ytc</Arg>
            </Call>
          </Set>
          ...
        </New>
      </Arg>
    </New>
  </Arg>
</New>
----
<1> Note the usage of `Password.deobfuscate(\...)` to avoid storing the clear-text password in the XML file.

[[password-checksum]]
=== Password Checksums

Password checksums are computed using the JDK `MessageDigest` class and are useful when Jetty receives a password and needs to verify it without storing the original password (for example, with Basic authentication).

Differently from obfuscated passwords, password checksums are not reversible and cannot be used when Jetty needs to pass the original password to other services; they can only be used to verify that the received password matches the password checksum.

You can store the Basic authentication credentials in checksum form on the server and verify the password received from the client by comparing the stored checksum with the checksum of the received password computed on-the-fly.
If the checksum is identical, then the password was correct.

[NOTE]
====
`MessageDigest` algorithms vary in strength, that is the probability that two different inputs produce the same checksum.

The `MD5` and `SHA-1` algorithms are not recommended, as they are weak and insecure.
Use at least `SHA-256`, typically available in all modern JVMs.
====

---

### Documentation Jetty Modules Operations Guide Pages Troubleshooting Index (`documentation/jetty/modules/operations-guide/pages/troubleshooting/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Troubleshooting

To troubleshoot Jetty when used as a standalone server, there are two main tools: the Jetty Server Dump and enabling DEBUG level logging.

It is typically very helpful to enable JMX, as detailed in xref:jmx/index.adoc[this section].
Enabling JMX allows to access the Jetty server (possibly a production server) from a remote location, and obtain the information exported by Jetty via JMX.

Access to the Jetty server via JMX allows to either diagnose the problem, possibly reconfigure and restart the Jetty server.

IMPORTANT: Make sure you read about how to secure the access to Jetty when using xref:jmx/index.adoc#remote[remote JMX].

[[dump]]
== Jetty Server Dump

The most useful too to troubleshoot a Jetty server is the xref:programming-guide:troubleshooting/component-dump.adoc[Jetty Component Dump], that reports the _internal_ state of the Jetty server and all its components (the thread pool, the `ByteBuffer` pool, the currently established connections, and so on).

The Jetty server dump can easily be obtained via JMX using a JMX console such as Java Mission Control (JMC), as explained in xref:programming-guide:troubleshooting/component-dump.adoc#dump-invoke[this section].

[[dump-start-stop]]
=== Dump at Server Start/Stop

The Jetty server dump may be reported just after the `Server` starts.
This is useful to verify that all the expected components have been correctly configured and started for the optimal functioning of the server.

Similarly, the Jetty server dump may be reported just before the `Server` stops.
This is useful to log the state of a server that is not working properly.

The xref:modules/standard.adoc#server[server] Jetty module defines, in xref:modules/standard.adoc#server-config[this section], the properties `jetty.server.dumpAfterStart` and `jetty.server.dumpBeforeStop` that you can configure to report the Jetty server dump.

These properties can be specified temporarily as command line arguments, as explained in xref:start/index.adoc#configure-enable-command-line[this section]:

[source,subs=+quotes]
----
$ java -jar $JETTY_HOME/start.jar *jetty.server.dumpAfterStart=true*
----

Alternatively, you can specify these properties in the `$JETTY_BASE/start.d/server.ini` file, as explained in xref:start/index.adoc#configure-edit-ini[this section], to make the values persistent across server restarts.

[[logging]]
== Enabling DEBUG Logging

Enabling DEBUG level logging for the `org.eclipse.jetty` logger name provides the maximum amount of information to troubleshoot Jetty issues.

Refer to the xref:server/index.adoc#logging[logging section] for more information about how to configure logging in Jetty.

[CAUTION]
====
Enabling DEBUG level logging for `org.eclipse.jetty` is very, *very* expensive.

Your server could be slowed down to almost a halt, especially if it is under heavy load.
Furthermore, the log file could quickly fill up the entire filesystem (unless configured to roll over), so you want to be really careful using DEBUG logging.

For production servers, consider using the xref:dump[Jetty Server Dump] first, and enable DEBUG logging only as a last resort.
====

However, sometimes issues are such that only DEBUG logging can really tell what's going on in the system, and enabling DEBUG logging is your best chance to figure the issue out.
Below you can find few suggestions that can help you reduce the impact when you have to enable DEBUG logging.

[[logging-backend]]
=== Jetty Behind a Load Balancer

If Jetty instances are behind a load balancer, you may configure the load balancer to send less load to a particular Jetty instance, and enable DEBUG logging in that instance only.

[[logging-jmx]]
=== Enabling DEBUG Logging for a Short Time

In certain cases the issue can be reproduced reliably, but only in the production environment.

You can use JMX to temporarily enable DEBUG logging, reproduce the issue, and then disable DEBUG logging.

Alternatively, if you cannot reliably reproduce the issue, but you _know_ it is happening, you can temporarily enable DEBUG logging for a small period of time, let's say 10-60 seconds, and then disable DEBUG logging.

Changing the log level at runtime is a feature of the logging implementation that you are using.

The Jetty SLF4J implementation, used by default, exposes via JMX method `boolean JettyLoggerFactoryMBean.setLoggerLevel(String loggerName, String levelName)` that you can invoke via a JMX console to change the level for the specified logger name.
The method returns `true` if the logger level was successfully changed.

For example, you can pass the string `org.eclipse.jetty` as the first parameter, and the string `DEBUG` (upper case) as the second parameter.
You can then use the string `INFO` or `WARN` (upper case) to restore the logging level to its previous value.

[[logging-subpackages]]
=== Enabling DEBUG Logging for SubPackages

Enabling DEBUG logging for the `org.eclipse.jetty` logger name implies that all children logger names, recursively, inherit the DEBUG level.

Processing a single HTTP request involves many Jetty components: the I/O subsystem (under `org.eclipse.jetty.io`), the thread pool (under `org.eclipse.jetty.util`), the HTTP/1.1 parsing (under `org.eclipse.jetty.http`), etc.

If you can cut the amount of DEBUG logging to just what you need to troubleshoot the issue, the impact of enabling DEBUG logging will be much less than enabling it for all Jetty components.

For example, if you need to troubleshoot a client that sends bad HTTP/1.1 requests, it may be enough to enable only the `org.eclipse.jetty.http` logger name, therefore saving the large amount of DEBUG logging produced by the I/O subsystem and by the thread pool.

In another case, you may need to troubleshoot only HTTP/2 requests, and therefore enabling only the `org.eclipse.jetty.http2` logger name could be enough.

[[debugging]]
== Remote Debugging

The Java Virtual Machines allows remote processes on different hosts to connect for debugging purposes, by using specific command line options.

[CAUTION]
====
While it is possible to enable remote debugging on a Jetty server, it is typically not recommended for security and performance reasons.
Only enable remote debugging on a Jetty server as a last resort to troubleshoot issues that could not be troubleshot otherwise.
====

You can easily create a custom Jetty module (see xref:modules/custom.adoc[this section]) with the following content:

.remote-debug.mod
----
include::code:example$jetty-modules/remote-debug.mod[]
----

The `[exec]` directive (documented xref:modules/index.adoc#directive-exec[here]) is necessary to pass the `-agentlib:jdwp` JVM option to the forked JVM that runs Jetty, so that you can attach with a debugger.

[NOTE]
====
The `address` parameter of the `-agentlib:jdwp` command line option specifies the network address and port the Jetty JVM listens on for remote debugging.

Please refer to the https://docs.oracle.com/en/java/javase/17/docs/specs/jpda/conninv.html[Java Debug Wire Protocol documentation] for additional information about the `-agentlib:jdwp` command line option and its parameters.
====

You can now enable the `remote-debug` Jetty module with the following command issued from the `$JETTY_BASE` directory:

----
$ java -jar $JETTY_HOME/start.jar --add-modules=server,remote-debug
----

The command above minimally adds a Jetty server without connectors (via the `server` Jetty module) and the `remote-debug` Jetty module, and produces the following `$JETTY_BASE` directory structure:

[source,subs=+quotes]
----
$JETTY_BASE
├── modules
│   └── remote-debug.mod
├── resources
│   └── jetty-logging.properties
└── start.d
    ├── ##remote-debug.ini##
    └── server.ini
----

You can easily disable the `remote-debug` Jetty module as explained in xref:start/index.adoc#configure-disable[this section].

Alternatively, you can enable the `remote-debug` module on the command line, as explained in xref:start/index.adoc#configure-enable-command-line[this section].

Starting the Jetty server with the `remote-debug` module enabled yields:

[jetty%nowrap]
....
[jetty]
setupModules=code:example$jetty-modules/remote-debug.mod
setupArgs=--add-modules=server,remote-debug
highlight=5005
....

Note how the JVM is listening on port `5005` to allow remote debuggers to connect.

If you want to avoid to fork a second JVM to pass the `-agentlib:jdwp` JVM option, please read xref:start/index.adoc#configure-dry-run[this section].

[[handlers]]
== Troubleshooting Handlers

[[handlers-state-tracking]]
=== `StateTrackingHandler`

Jetty's `StateTrackingHandler` (described in xref:modules/standard.adoc#state-tracking[this module]) can be used to troubleshoot problems in web applications.

`StateTrackingHandler` tracks the usages of `Handler`/`Request`/`Response` asynchronous APIs by web applications, emitting events (logged at `WARN` level) when an invalid usage of the APIs is detected.

In conjunction with <<dump,dumping the Jetty component tree>>, it dumps the state of current requests, detailing whether they have reads or writes that are pending, whether callbacks have been completed, along with thread stack traces (including virtual threads) of operations that have been started but not completed, or are stuck in blocking code.

You need to enable the `state-tracking` Jetty module, and configure it to track what you are interested in tracking (for more details, see the link:{javadoc-url}/org/eclipse/jetty/server/handler/StateTrackingHandler.html[javadocs]).

---

### Documentation Jetty Modules Operations Guide Pages Xml Index (`documentation/jetty/modules/operations-guide/pages/xml/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Jetty XML

// TODO: merge this small section into the syntax and maybe call it "Jetty XML Reference".

The Jetty XML format is a straightforward mapping of XML elements to Java APIs so that any object can be instantiated and getters, setters, and methods can be called.

The Jetty XML format is very similar to that of frameworks like Spring or Plexus, although it predates all of them and it's typically more powerful as it can invoke any Java API.

The Jetty XML format is used in xref:modules/index.adoc[Jetty modules] to create the Jetty server components, as well as in xref:deploy/index.adoc[Jetty XML context files] to configure web applications, but it can be used to call any Java API.

[[syntax]]
== Jetty XML Syntax

The Jetty XML syntax defines XML element that allow you to call any Java API and that allow you to interact in a simpler way with the xref:modules/index.adoc[Jetty module system] and the xref:deploy/index.adoc[Jetty deploy system].

The Jetty XML elements define attributes such as `id`, `name`, `class`, etc. that may be replaced by correspondent elements, so that these XML documents are equivalent:

[,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure>
  <Get id="stderr" class="java.lang.System" name="err">
    <Call name="println" arg="HELLO" />
  </Get>
</Configure>
----

[,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure>
  <Get>
    <Id>stderr</Id>
    <Name>err</Name>
    <Class>java.lang.System</Class>
    <Call>
      <Name>println</Name>
      <Arg>HELLO</Arg>
    </Call>
  </Get>
</Configure>
----

The version using attributes is typically shorter and nicer to read, but sometimes the attribute value cannot be a literal string (for example, it could be the value of a system property) and that's where elements gives you the required flexibility.

[[syntax-configure]]
=== `<Configure>`

Element `Configure` must be the root element of the XML document.

The following Jetty XML creates an empty `String` and assigns it the id `mystring`:

[,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure id="mystring" class="java.lang.String" />
----

This is equivalent to the following Java code:

[,java]
----
var mystring = new String();
----

If an object with the id `mystring` already exists, then it is not created again but rather just referenced.

Within element `<Configure>`, the created object (if any) is in <<syntax-scope,scope>> and may be the implicit target of other, nested, elements.

Typically, the `<Configure>` element is used to configure a `Server` instance or `ContextHandler` subclasses such as `WebAppContext` that represent web applications.

[[syntax-arg]]
=== `<Arg>`

Element `Arg` is used to pass arguments to <<syntax-new,constructors>> and <<syntax-call,method calls>>.

The following example creates a minimal Jetty `Server`:

[,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure class="org.eclipse.jetty.server.Server">
  <Arg type="int">8080</Arg>
</Configure>
----

Arguments may have a `type` attribute that explicitly performs <<syntax-types,type coercion>>.

Arguments may also have a `name` attribute, which is matched with the corresponding Java annotation in the source class, that helps to identify arguments:

[,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure class="org.eclipse.jetty.server.Server">
  <Arg name="port" type="int">8080</Arg>
</Configure>
----

[[syntax-new]]
=== `<New>`

Element `<New>` creates a new object of the type specified by the mandatory `class` attribute.
A sequence of `Arg` elements, that must be contiguous and before other elements, may be present to specify the constructor arguments.

Within element `<New>` the newly created object is in <<syntax-scope,scope>> and may be the implicit target of other, nested, elements.

The following example creates an `ArrayList`:

[,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure>
  <New id="mylist" class="java.util.ArrayList">
    <Arg type="int">16</Arg>
  </New>
</Configure>
----

This is equivalent to the following Java code:

[,java]
----
var mylist = new ArrayList(16);
----

[[syntax-call]]
=== `<Call>`

Element `<Call>` invokes a method specified by the mandatory `name` attribute.
A sequence of `Arg` elements, that must be contiguous and before other elements, may be present to specify the method arguments.

Within element `<Call>` the return value, if the return type is not `void`, is in <<syntax-scope,scope>> and may be the implicit target of other, nested, elements.

[,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure>
  <New class="java.util.ArrayList">
    <Call name="listIterator">
      <Arg type="int">0</Arg>
    </Call>
    <Call name="next" />
  </New>
</Configure>
----

This is equivalent to the following Java code:

[,java]
----
new ArrayList().listIterator(0).next();
----

It is possible to call `static` methods by specifying the `class` attribute:

[,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure>
  <Call id="myhost" name="getByName" class="java.net.InetAddress">
    <Arg>jdk.java.net</Arg>
  </Call>
</Configure>
----

This is equivalent to the following Java code:

[,java]
----
var myhost = InetAddress.getByName("jdk.java.net");
----

The `class` attribute (or `<Class>` element) can also be used to specify the Java class or interface to use to lookup the non-``static`` method name.
This is necessary when the object in scope, onto which the `<Call>` would be applied, is an instance of a class that is not visible to Jetty classes, or not accessible because it is not `public`.
For example:

[,xml,subs=+quotes]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure>
  <Call class="java.util.concurrent.Executors" name="newSingleThreadScheduledExecutor">
    #<Call class="java.util.concurrent.ExecutorService" name="shutdown" />#
  </Call>
</Configure>
----

In the example above, `Executors.newSingleThreadScheduledExecutor()` returns an object whose class is a private JDK implementation class.
Without an explicit `class` attribute (or `<Class>` element), it is not possible to invoke the method `shutdown()` when it is obtained via reflection from the private JDK implementation class, because while the method is `public`, the private JDK implementation class is not, therefore this exception is thrown:

[source]
----
java.lang.IllegalAccessException: class org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration (in module org.eclipse.jetty.xml) cannot access a member of class java.util.concurrent.Executors$DelegatedExecutorService (in module java.base) with modifiers "public"
----

The solution is to explicitly use the `class` attribute (or `<Class>` element) of the `<Call>` element that is invoking the `shutdown()` method, specifying a publicly accessible class or interface that the object in scope extends or implements (in the example above `java.util.concurrent.ExecutorService`).

[[syntax-get]]
=== `<Get>`

Element `<Get>` retrieves the value of a JavaBean property specified by the mandatory `name` attribute.

If the JavaBean property is `foo` (or `Foo`), `<Get>` first attempts to invoke _method_ `getFoo()` or _method_ `isFoo()`; failing that, attempts to retrieve the value from _field_ `foo` (or `Foo`).

[,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure id="server" class="org.eclipse.jetty.server.Server">
  
  <Get id="version" name="version" />

  
  <Get class="java.lang.System" name="err">
    <Call name="println">
      <Arg>Jetty</Arg>
    </Call>
  </Get>
</Configure>
----

The `class` attribute (or `<Class>` element) allows to perform `static` calls, or to lookup the getter method from the specified class, as described in the <<syntax-call,`<Call>` section>>.

[[syntax-set]]
=== `<Set>`

Element `<Set>` stores the value of a JavaBean property specified by the mandatory `name` attribute.

If the JavaBean property is `foo` (or `Foo`), `<Set>` first attempts to invoke _method_ `setFoo(...)` with the value in the <<syntax-scope,scope>> as argument; failing that, attempts to store the value in the scope to _field_ `foo` (or `Foo`).

[,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure id="server" class="org.eclipse.jetty.server.Server">
  
  <Set name="dryRun">true</Set>

  
  <Set name="requestLog">
    <New class="org.eclipse.jetty.server.CustomRequestLog" />
  </Set>
</Configure>
----

The `class` attribute (or `<Class>` element) allows to perform `static` calls, or to lookup the setter method from the specified class, as described in the <<syntax-call,`<Call>` section>>.

[[syntax-map]]
=== `<Map>` and `<Entry>`

Element `<Map>` allows the creation of a new `java.util.Map` implementation, specified by the `class` attribute -- by default a `HashMap`.

The map entries are specified with a sequence of `<Entry>` elements, each with exactly 2 `<Item>` elements, for example:

[,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure>
  <Map class="java.util.concurrent.ConcurrentHashMap">
    <Entry>
      <Item>host</Item>
      <Item>
        <Call class="java.net.InetAddress" name="getByName">
          <Arg>localhost</Arg>
        </Call>
      </Item>
    </Entry>
  </Map>
</Configure>
----

[[syntax-put]]
=== `<Put>`

Element `<Put>` is a convenience element that puts a key/value pair into objects that implement `java.util.Map`.
You can only specify the key value via the `name` attribute, so the key can only be a literal string (for keys that are not literal strings, use the `<Call>` element).

[,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure>
  <New class="java.util.Properties">
    <Put name="host">
      <Call class="java.net.InetAddress" name="getByName">
        <Arg>localhost</Arg>
      </Call>
    </Put>
  </New>
</Configure>
----

[[syntax-array]]
=== `<Array>` and `<Item>`

Element `<Array>` creates a new array, whose component type may be specified by the `type` attribute, or by a `Type` child element.

[,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure>
  <Array type="java.lang.Object">
    <Item /> 
    <Item>literalString</Item>
    <Item type="String"></Item> 
    <Item type="Double">1.0D</Item>
    <Item>
      <New class="java.lang.Exception" />
    </Item>
  </Array>
</Configure>
----

[[syntax-ref]]
=== `<Ref>`

Element `<Ref>` allows you to reference an object via the `refid` attribute`, putting it into <<syntax-scope,scope>> so that nested elements can operate on it.
You must give a unique `id` attribute to the objects you want to reference.

[,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">


<Configure id="server" class="org.eclipse.jetty.server.Server">
  <Get class="java.lang.System" name="err">
    
    <Ref refid="server">
      
      <Get id="myversion" name="version" />
    </Ref>

    <Call name="println">
      
      <Arg>Server version is: <Ref refid="myversion" /></Arg>
    </Call>
  </Get>
</Configure>
----

[[syntax-property]]
=== `<Property>`

Element `<Property>` retrieves the value of the Jetty module property specified by the `name` attribute, and it is mostly used when creating xref:modules/custom.adoc[custom Jetty modules] or when using xref:deploy/index.adoc#jetty[Jetty context XML files].

The `deprecated` attribute allows you to specify a comma separated list of old, deprecated, property names for backward compatibility.

The `default` attribute allows you to specify a default value for the property, if it has not been explicitly defined.

For example, you may want to configure the context path of your web application in this way:

[,xml,subs="+attributes,+quotes"]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext">
  <Set name="contextPath">
    #<Property name="com.myapps.mywiki.context.path" default="/wiki" />#
  </Set>
  <Set name="war">/opt/myapps/mywiki.war</Set>
</Configure>
----

The `contextPath` value is resolved by looking for the Jetty module property `com.myapps.mywiki.context.path`; if this property is not set, then the default value of `/wiki` is used.

[[syntax-system-property]]
=== `<SystemProperty>`

Element `<SystemProperty>` retrieves the value of the JVM system property specified by the `name` attribute, via `System.getProperty(...)`.

The `deprecated` attribute allows you to specify a comma separated list of old, deprecated, system property names for backward compatibility.

The `default` attribute allows you to specify a default value for the system property value, if it has not been explicitly defined.

The following example creates a minimal Jetty `Server` that listens on a port specified by the `com.acme.http.port` system property:

[,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure id="server" class="org.eclipse.jetty.server.Server">
  <Arg type="int">
    <SystemProperty name="com.acme.http.port" default="8080" />
  </Arg>
</Configure>
----

[[syntax-env]]
=== `<Env>`

Element `<Env>` retrieves the value of the environment variable specified by the `name` attribute, via `System.getenv(...)`.

The `deprecated` attribute allows you to specify a comma separated list of old, deprecated, environment variable names for backward compatibility.

The `default` attribute allows you to specify a default value for the environment variable value, if it has not been explicitly defined.

The following example creates a minimal Jetty `Server` that listens on a port specified by the `COM_ACME_HTTP_PORT` environment variable:

[,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure id="server" class="org.eclipse.jetty.server.Server">
  <Arg type="int">
    <Env name="COM_ACME_HTTP_PORT" default="8080" />
  </Arg>
</Configure>
----

[[syntax-types]]
=== Type Coercion

Elements that have the `type` attribute explicitly perform the type coercion of the string value present in the XML document to the Java type specified by the `type` attribute.

Supported types are the following:

* all primitive types and their boxed equivalents, for example `type="int"` but also `type="Integer"` (short form) and `type="java.lang.Integer"` (fully qualified form)
* `java.lang.String`, in both short form and fully qualified form
* `java.net.URL`, in both short form and fully qualified form
* `java.net.InetAddress`, in both short form and fully qualified form

[[syntax-scope]]
=== Scopes

Elements that create new objects or that return a value create a _scope_.
Within these elements there may be nested elements that will operate on that scope, i.e. on the new object or returned value.

The following example illustrates how scopes work:

[,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">

<Configure id="server" class="org.eclipse.jetty.server.Server">
  <Arg type="int">8080</Arg>
  

  
  <Set name="stopTimeout">5000</Set>

  
  <New id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
    

    
    <Set name="secureScheme">https</Set>
  </New>

  
  <Get name="ThreadPool">
    

    
    <Set name="maxThreads" type="int">256</Set>
  </Get>

  
  <Get class="java.lang.System" name="err">
    

    
    <Ref refid="server">
      
      <Get id="myversion" name="version" />
    </Ref>

    
    <Call name="println">
      <Arg>Server version is: <Ref refid="myversion" /></Arg>
    </Call>
  </Get>
</Configure>
----

// TODO: port the documentation from old_docs/jetty-xml/*.adoc

---

### Documentation Jetty Modules Programming Guide Pages Arch Bean (`documentation/jetty/modules/programming-guide/pages/arch/bean.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Jetty Component Architecture

Applications that use the Jetty libraries (both client and server) create objects from Jetty classes and compose them together to obtain the desired functionalities.

A client application creates a `ClientConnector` instance, a `HttpClientTransport` instance and an `HttpClient` instance and compose them to have a working HTTP client that uses to call third party services.

A server application creates a `ThreadPool` instance, a `Server` instance, a `ServerConnector` instance, a `Handler` instance and compose them together to expose an HTTP service.

Internally, the Jetty libraries create even more instances of other components that also are composed together with the main ones created by applications.

The end result is that an application based on the Jetty libraries is a _tree_ of components.
In server application the root of the component tree is a `Server` instance, while in client applications the root of the component tree is an `HttpClient` instance.

Having all the Jetty components in a tree is beneficial in a number of use cases.
It makes possible to register the components in the tree as xref:arch/jmx.adoc[JMX MBeans] so that a JMX console can look at the internal state of the components.
It also makes possible to xref:troubleshooting/component-dump.adoc[dump the component tree] (and therefore each component's internal state) to a log file or to the console for xref:troubleshooting/index.adoc[troubleshooting purposes].
// TODO: add a section on Dumpable?

[[lifecycle]]
== Jetty Component Lifecycle

Jetty components typically have a life cycle: they can be started and stopped.
The Jetty components that have a life cycle implement the `org.eclipse.jetty.util.component.LifeCycle` interface.

Jetty components that contain other components implement the `org.eclipse.jetty.util.component.Container` interface and typically extend the `org.eclipse.jetty.util.component.ContainerLifeCycle` class.
`ContainerLifeCycle` can contain these type of components, also called __bean__s:

* _managed_ beans, `LifeCycle` instances whose life cycle is tied to the life cycle of their container
* _unmanaged_ beans, `LifeCycle` instances whose life cycle is _not_ tied to the life cycle of their container
* _POJO_ (Plain Old Java Object) beans, instances that do not implement `LifeCycle`

`ContainerLifeCycle` uses the following logic to determine if a bean should be _managed_, _unmanaged_ or _POJO_:

* the bean implements `LifeCycle`
** the bean is not started, add it as _managed_
** the bean is started, add it as _unmanaged_
* the bean does not implement `LifeCycle`, add it as _POJO_

When a `ContainerLifeCycle` is started, it also starts recursively all its managed beans (if they implement `LifeCycle`); unmanaged beans are not started during the `ContainerLifeCycle` start cycle.
Likewise, stopping a `ContainerLifeCycle` stops recursively and in reverse order all its managed beans; unmanaged beans are not stopped during the `ContainerLifeCycle` stop cycle.

Components can also be started and stopped individually, therefore activating or deactivating the functionalities that they offer.

Applications should first compose components in the desired structure, and then start the root component:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ComponentDocs.java[tags=start]
----

The component tree is the following:

[,screen]
----
Root
├── Monitor (MANAGED)
└── Service (MANAGED)
    └── ScheduledExecutorService (POJO)
----

When the `Root` instance is created, also the `Monitor` instance is created and added as bean, so `Monitor` is the first bean of `Root`.
`Monitor` is a _managed_ bean, because it has been explicitly added to `Root` via `ContainerLifeCycle.addManaged(...)`.

Then, the application creates a `Service` instance and adds it to `Root` via `ContainerLifeCycle.addBean(...)`, so `Service` is the second bean of `Root`.
`Service` is a _managed_ bean too, because it is a `LifeCycle` and at the time it was added to `Root` is was not started.

The `ScheduledExecutorService` within `Service` does not implement `LifeCycle` so it is added as a _POJO_ to `Service`.

It is possible to stop and re-start any component in a tree, for example:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ComponentDocs.java[tags=restart]
----

`Service` can be stopped independently of `Root`, and re-started.
Starting and stopping a non-root component does not alter the structure of the component tree, just the state of the subtree starting from the component that has been stopped and re-started.

`Container` provides an API to find beans in the component tree:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ComponentDocs.java[tags=getBeans]
----

You can add your own beans to the component tree at application startup time, and later find them from your application code to access their services.

[TIP]
====
The component tree should be used for long-lived or medium-lived components such as thread pools, web application contexts, etc.

It is not recommended adding to, and removing from, the component tree short-lived objects such as HTTP requests or TCP connections, for performance reasons.

If you need component tree features such as automatic xref:arch/jmx.adoc[export to JMX] or xref:troubleshooting/component-dump.adoc[dump capabilities] for short-lived objects, consider having a long-lived container in the component tree instead.
You can make the long-lived container efficient at adding/removing the short-lived components using a data structure that is not part of the component tree, and make the long-lived container handle the JMX and dump features for the short-lived components.
====

[[listener]]
== Jetty Component Listeners

A component that extends `AbstractLifeCycle` inherits the possibility to add/remove event _listeners_ for various events emitted by components.

A component that implements `java.util.EventListener` that is added to a `ContainerLifeCycle` is also registered as an event listener.

The following sections describe in details the various listeners available in the Jetty component architecture.

[[listener-lifecycle]]
=== LifeCycle.Listener

A `LifeCycle.Listener` emits events for life cycle events such as starting, stopping and failures:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ComponentDocs.java[tags=lifecycleListener]
----

For example, a life cycle listener attached to a `Server` instance could be used to create (for the _started_ event) and delete (for the _stopped_ event) a file containing the process ID of the JVM that runs the `Server`.

[[listener-container]]
=== Container.Listener

A component that implements `Container` is a container for other components and `ContainerLifeCycle` is the typical implementation.

A `Container` emits events when a component (also called _bean_) is added to or removed from the container:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ComponentDocs.java[tags=containerListener]
----

A `Container.Listener` added as a bean will also be registered as a listener:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ComponentDocs.java[tags=containerSiblings]
----

[[listener-inherited]]
=== Container.InheritedListener

A `Container.InheritedListener` is a listener that will be added to all descendants that are also ``Container``s.

Listeners of this type may be added to the component tree root only, but will be notified of every descendant component that is added to or removed from the component tree (not only first level children).

The primary use of `Container.InheritedListener` within the Jetty Libraries is `MBeanContainer` from the xref:arch/jmx.adoc[Jetty JMX support].

`MBeanContainer` listens for every component added to the tree, converts it to an MBean and registers it to the MBeanServer; for every component removed from the tree, it unregisters the corresponding MBean from the MBeanServer.

---

### Documentation Jetty Modules Programming Guide Pages Arch Io (`documentation/jetty/modules/programming-guide/pages/arch/io.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Jetty I/O Architecture

The Jetty libraries (both client and server) use Java NIO to handle I/O, so that at its core Jetty I/O is completely non-blocking.

[[selector-manager]]
== Jetty I/O: `SelectorManager`

The main class of The Jetty I/O library is link:{javadoc-url}/org/eclipse/jetty/io/SelectorManager.html[`SelectorManager`].

`SelectorManager` manages internally a configurable number of link:{javadoc-url}/org/eclipse/jetty/io/ManagedSelector.html[`ManagedSelector`]s.
Each `ManagedSelector` wraps an instance of `java.nio.channels.Selector` that in turn manages a number of `java.nio.channels.SelectionKey` instances, that each wrap a `java.nio.channels.SelectableChannel` instance such as `java.nio.channels.SocketChannel`.

[plantuml]
----
object SM as "SelectorManager"
object MS1 as "ManagedSelector" {
  java.nio.channels.Selector
}
object MS2 as "ManagedSelector" {
  java.nio.channels.Selector
}
object SC1A as "SelectionKey" {
  SelectableChannel
}
object SC1B as "SelectionKey" {
  SelectableChannel
}
object SC2A as "SelectionKey" {
  SelectableChannel
}
object SC2B as "SelectionKey" {
  SelectableChannel
}
object SC2C as "SelectionKey" {
  SelectableChannel
}

SM o-- MS1
SM o-- MS2
MS1 o-- SC1A
MS1 o-- SC1B
MS2 o-- SC2A
MS2 o-- SC2B
MS2 o-- SC2C
----

`SocketChannel` instances are typically created by the Jetty implementation, on client-side when connecting to a server and on server-side when accepting connections from clients.
In both cases the `SocketChannel` instance is passed to `SelectorManager` (which passes it to `ManagedSelector` and eventually to `java.nio.channels.Selector`) to be registered for use within Jetty.

It is possible for an application to create the `SocketChannel` instances outside Jetty, even perform some initial network traffic also outside Jetty (for example for authentication purposes), and then pass the `SocketChannel` instance to `SelectorManager` for use within Jetty.

This example shows how a client can connect to a server:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/SelectorManagerDocs.java[tags=connect]
----

This example shows how a server accepts a client connection:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/SelectorManagerDocs.java[tags=accept]
----

[[endpoint-connection]]
== Jetty I/O: `EndPoint` and `Connection`

``SocketChannel``s that are passed to `SelectorManager` are wrapped into two related components: an link:{javadoc-url}/org/eclipse/jetty/io/EndPoint.html[`EndPoint`] and a link:{javadoc-url}/org/eclipse/jetty/io/Connection.html[`Connection`].

`EndPoint` is the Jetty abstraction for a `SocketChannel` or a `DatagramChannel`: you can read bytes from an `EndPoint`, you can write bytes to an `EndPoint` , you can close an `EndPoint`, etc.

`Connection` is the Jetty abstraction that is responsible to read bytes from the `EndPoint` and to deserialize the read bytes into objects.
For example, an HTTP/1.1 server-side `Connection` implementation is responsible to deserialize HTTP/1.1 request bytes into an HTTP request object.
Conversely, an HTTP/1.1 client-side `Connection` implementation is responsible to deserialize HTTP/1.1 response bytes into an HTTP response object.

`Connection` is the abstraction that implements the reading side of a specific protocol such as HTTP/1.1, or HTTP/2, or HTTP/3, or WebSocket: it is able to read and parse incoming bytes in that protocol.

The writing side for a specific protocol _may_ be implemented in the `Connection` but may also be implemented in other components, although eventually the bytes to write will be written through the `EndPoint`.

While there are primarily only two implementations of `EndPoint`,link:{javadoc-url}/org/eclipse/jetty/io/SocketChannelEndPoint.html[`SocketChannelEndPoint`] for TCP and link:{javadoc-url}/org/eclipse/jetty/io/DatagramChannelEndPoint.html[`DatagramChannelEndPoint`] for UDP (used both on the client-side and on the server-side), there are many implementations of `Connection`, typically two for each protocol (one for the client-side and one for the server-side).

The `EndPoint` and `Connection` pairs can be chained, for example in case of encrypted communication using the TLS protocol.
There is an `EndPoint` and `Connection` TLS pair where the `EndPoint` reads the encrypted bytes from the socket and the `Connection` decrypts them; next in the chain there is an `EndPoint` and `Connection` pair where the `EndPoint` "reads" decrypted bytes (provided by the previous `Connection`) and the `Connection` deserializes them into specific protocol objects (for example HTTP/2 frame objects).

Certain protocols, such as WebSocket, start the communication with the server using one protocol (for example, HTTP/1.1), but then change the communication to use another protocol (for example, WebSocket).
`EndPoint` supports changing the `Connection` object on-the-fly via `EndPoint.upgrade(Connection)`.
This allows to use the HTTP/1.1 `Connection` during the initial communication and later to replace it with a WebSocket `Connection`.

// TODO: add a section on `UpgradeFrom` and `UpgradeTo`?

`SelectorManager` is an abstract class because while it knows how to create concrete `EndPoint` instances, it does not know how to create protocol specific `Connection` instances.

Creating `Connection` instances is performed on the server-side by link:{javadoc-url}/org/eclipse/jetty/server/ConnectionFactory.html[`ConnectionFactory`]s and on the client-side by link:{javadoc-url}/org/eclipse/jetty/io/ClientConnectionFactory.html[`ClientConnectionFactory`]s.

On the server-side, the component that aggregates a `SelectorManager` with a set of ``ConnectionFactory``s is link:{javadoc-url}/org/eclipse/jetty/server/ServerConnector.html[`ServerConnector`] for TCP sockets, link:{javadoc-url}/org/eclipse/jetty/quic/server/QuicServerConnector.html[`QuicServerConnector`] for QUIC sockets, and link:{javadoc-url}/org/eclipse/jetty/unixdomain/server/UnixDomainServerConnector.html[`UnixDomainServerConnector`] for Unix-Domain sockets (see the xref:server/io-arch.adoc[server-side architecture section] for more information).

On the client-side, the components that aggregates a `SelectorManager` with a set of ``ClientConnectionFactory``s are link:{javadoc-url}/org/eclipse/jetty/client/HttpClientTransport.html[`HttpClientTransport`] subclasses (see the xref:client/io-arch.adoc[client-side architecture section] for more information).

[[endpoint]]
== Jetty I/O: `EndPoint`

The Jetty I/O library use Java NIO to handle I/O, so that I/O is non-blocking.

At the Java NIO level, in order to be notified when a `SocketChannel` or `DatagramChannel` has data to be read, the `SelectionKey.OP_READ` flag must be set.

In the Jetty I/O library, you can call `EndPoint.fillInterested(Callback)` to declare interest in the "read" (also called "fill") event, and the `Callback` parameter is the object that is notified when such an event occurs.

At the Java NIO level, a `SocketChannel` or `DatagramChannel` is always writable, unless it becomes congested.
In order to be notified when a channel uncongests and it is therefore writable again, the `SelectionKey.OP_WRITE` flag must be set.

In the Jetty I/O library, you can call `EndPoint.write(Callback, ByteBuffer\...)` to write the ``ByteBuffer``s and the `Callback` parameter is the object that is notified when the whole write is finished (i.e. _all_ ``ByteBuffer``s have been fully written, even if they are delayed by congestion/uncongestion).

The `EndPoint` APIs abstract out the Java NIO details by providing non-blocking APIs based on `Callback` objects for I/O operations.
The `EndPoint` APIs are typically called by `Connection` implementations, see <<connection,this section>>.

[[connection]]
== Jetty I/O: `Connection`

`Connection` is the abstraction that deserializes incoming bytes into objects, for example an HTTP request object or a WebSocket frame object, that can be used by more abstract layers.

`Connection` instances have two lifecycle methods:

* `Connection.onOpen()`, invoked when the `Connection` is associated with the `EndPoint`.
* `Connection.onClose(Throwable)`, invoked when the `Connection` is disassociated from the `EndPoint`, where the `Throwable` parameter indicates whether the disassociation was normal (when the parameter is `null`) or was due to an error (when the parameter is not `null`).

When a `Connection` is first created, it is not registered for any Java NIO event.
It is therefore typical to implement `onOpen()` to call `EndPoint.fillInterested(Callback)` so that the `Connection` declares interest for read events, and it is invoked (via the `Callback`) when the read event happens.

The abstract class `AbstractConnection` partially implements `Connection` and provides simpler APIs.
The example below shows a typical implementation that extends `AbstractConnection`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/SelectorManagerDocs.java[tags=connection]
----

[[connection-listener]]
=== Jetty I/O: `Connection.Listener`

The Jetty I/O library allows applications to register xref:arch/listener.adoc[event listeners] for the `Connection` events "opened" and "closed" via the interface `Connection.Listener`.

This is useful in many cases, for example:

* Gather statistics about connection lifecycle, such as time of creation and duration.
* Gather statistics about the number of concurrent connections, and take action if a threshold is exceeded.
* Gather statistics about the number of bytes read and written, and the number of "messages" read and written, where "messages" may mean HTTP/1.1 requests or responses, or WebSocket frames, or HTTP/2 frames, etc.
* Gather statistics about the different types of connections being opened (TLS, HTTP/1.1, HTTP/2, WebSocket, etc.).
* Etc.

`Connection.Listener` implementations must be added as beans to a server-side `Connector`, or to client-side `HttpClient`, `WebSocketClient`, `HTTP2Client` or `HTTP3Client`.
You can add as beans many `Connection.Listener` objects, each with its own logic, so that you can separate different logics into different `Connection.Listener` implementations.

The Jetty I/O library provides useful `Connection.Listener` implementations that you should evaluate before writing your own, for example link:{javadoc-url}/org/eclipse/jetty/io/ConnectionStatistics.html[`ConnectionStatistics`].

Here is a simple example of a `Connection.Listener` used both on the client and on the server:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/SelectorManagerDocs.java[tags=connectionListener]
----

[[echo]]
== Jetty I/O: TCP Network Echo

With the concepts above it is now possible to write a simple, fully non-blocking, `Connection` implementation that simply echoes the bytes that it reads back to the other peer.

A naive, but wrong, implementation may be the following:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/SelectorManagerDocs.java[tags=echo-wrong]
----

WARNING: The implementation above is wrong and leads to `StackOverflowError`.

The problem with this implementation is that if the writes always complete synchronously (i.e. without being delayed by TCP congestion), you end up with this sequence of calls:

----
Connection.onFillable()
  EndPoint.write()
    Connection.succeeded()
      Connection.onFillable()
        EndPoint.write()
          Connection.succeeded()
          ...
----

which leads to `StackOverflowError`.

This is a typical side effect of asynchronous programming using non-blocking APIs, and happens in the Jetty I/O library as well.

NOTE: The callback is invoked synchronously for efficiency reasons.
Submitting the invocation of the callback to an `Executor` to be invoked in a different thread would cause a context switch and make simple writes extremely inefficient.

This side effect of asynchronous programming leading to `StackOverflowError` is so common that the Jetty libraries have a generic solution for it: a specialized `Callback` implementation named `org.eclipse.jetty.util.IteratingCallback` that turns recursion into iteration, therefore avoiding the `StackOverflowError`.

`IteratingCallback` is a `Callback` implementation that should be passed to non-blocking APIs such as `EndPoint.write(Callback, ByteBuffer\...)` when they are performed in a loop.

`IteratingCallback` works by starting the loop with `IteratingCallback.iterate()`.
In turn, this calls `IteratingCallback.process()`, an abstract method that must be implemented with the code that should be executed for each loop.

Method `process()` must return:

* `Action.SCHEDULED`, to indicate whether the loop has performed a non-blocking, possibly asynchronous, operation
* `Action.IDLE`, to indicate that the loop should temporarily be suspended to be resumed later with another call to iterate
* `Action.SUCCEEDED` to indicate that the loop exited successfully

Any exception thrown within `process()` exits the loops with a failure.

Now that you know how `IteratingCallback` works, a correct implementation for the echo `Connection` is the following:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/SelectorManagerDocs.java[tags=echo-correct]
----

When `onFillable()` is called, for example the first time that bytes are available from the network, the iteration is started.
Starting the iteration calls `process()`, where a buffer is allocated and filled with bytes read from the network via `EndPoint.fill(ByteBuffer)`; the buffer is subsequently written back via `EndPoint.write(Callback, ByteBuffer\...)` -- note that the callback passed to `EndPoint.write()` is `this`, i.e. the `IteratingCallback` itself; finally `Action.SCHEDULED` is returned, returning from the `process()` method.

At this point, the call to `EndPoint.write(Callback, ByteBuffer\...)` may have completed synchronously; `IteratingCallback` would know that and call `process()` again; within `process()`, the buffer has already been allocated so it will be reused, saving further allocations; the buffer will be filled and possibly written again; `Action.SCHEDULED` is returned again, returning again from the `process()` method.

At this point, the call to `EndPoint.write(Callback, ByteBuffer\...)` may have not completed synchronously, so `IteratingCallback` will not call `process()` again; the processing thread is free to return to the Jetty I/O system where it may be put back into the thread pool.
If this was the only active network connection, the system would now be idle, with no threads blocked, waiting that the `write()` completes. This thread-less wait is one of the most important features that make non-blocking asynchronous servers more scalable: they use less resources.

Eventually, the Jetty I/O system will notify that the `write()` completed; this notifies the `IteratingCallback` that can now resume the loop and call `process()` again.

When `process()` is called, it is possible that zero bytes are read from the network; in this case, you want to deallocate the buffer since the other peer may never send more bytes for the `Connection` to read, or it may send them after a long pause -- in both cases we do not want to retain the memory allocated by the buffer; next, you want to call `fillInterested(this)` to declare again interest for read events, and return `Action.SCHEDULED` since a callback is scheduled to occur once filling is possible.

Another possibility is that during `process()` the read returns `-1` indicating that the other peer has closed the connection; this means that there will not be more bytes to read and the loop can be exited, so you return `Action.SUCCEEDED`; `IteratingCallback` will then call `onCompleteSuccess()` where you can close the `EndPoint`.

The last case is that during `process()` an exception is thrown, for example by `EndPoint.fill(ByteBuffer)` or, in more advanced implementations, by code that parses the bytes that have been read and finds them unacceptable; any exception thrown within `process()` will be caught by `IteratingCallback` that will exit the loop with a failure and call `onCompleteFailure(Throwable)` with the exception that has been thrown, where you can close the `EndPoint`, passing the exception that is the reason for closing prematurely the `EndPoint`.

Note that some failures may occur whilst a scheduled operation is in progress.
Such failures are notified immediately via the `onFailure(Throwable)` method, but care must be taken to not release any resources that may still be in use by the scheduled operation.
The `onCompleteFailure(Throwable)` method is called when both a failure has occurred and any scheduled operation has completed.
An example of this issue is that a buffer used for a write operation cannot be returned to a pool in `onFailure(Throwable)` as the write may still be progressing.
Either the buffer must be removed from the pool in `onFailure(Throwable)` or the release of the buffer deferred until `onCompleteFailure(Throwable)` is called.

[IMPORTANT]
====
Asynchronous programming is hard.

Rely on the Jetty classes to implement `Connection` to avoid mistakes that will be difficult to diagnose and reproduce.
====

[[content-source]]
== `Content.Source`

The high-level abstraction that Jetty offers to read bytes is `org.eclipse.jetty.io.Content.Source`.

`Content.Source` offers a non-blocking demand/read model where a read returns a `Content.Chunk` (see also <<content-source-chunk,this section>>).

A `Content.Chunk` groups the following information:

* A `ByteBuffer` with the bytes that have been read; it may be empty.
* Whether the read reached end-of-file, via its `last` flag.
* A failure that might have happened during the read, via its `getFailure()` method.

The `Content.Chunk` returned from `Content.Source.read()` can either be a _normal_ chunk (a chunk containing a `ByteBuffer` and a `null` failure), or a _failure_ chunk (a chunk containing an empty `ByteBuffer` and a non-`null` failure).

A failure chunk also indicates (via the `last` flag) whether the failure is a fatal (when `last=true`) or transient (when `last=false`) failure.

A transient failure is a temporary failure that happened during the read, it may be ignored, and it is recoverable: it is possible to call `read()` again and obtain a normal chunk (or a `null` chunk).
Typical cases of transient failures are idle timeout failures, where the read timed out, but the application may decide to insist reading until some other event happens.
The application may convert a transient failure into a fatal failure by calling `Content.Source.fail(Throwable)`.

A `Content.Source` must be fully consumed by reading all its content, or failed by calling `Content.Source.fail(Throwable)` to signal that the reader is not interested in reading anymore, otherwise it may leak underlying resources.

Fully consuming a `Content.Source` means reading from it until it returns a `Content.Chunk` whose `last` flag is `true`.
Reading or demanding from an already fully consumed `Content.Source` is always immediately serviced with the last state of the `Content.Source`: a `Content.Chunk` with the `last` flag set to `true`, either an end-of-file chunk, or a failure chunk.

Once failed, a `Content.Source` is considered fully consumed.
Further attempts to read from a failed `Content.Source` return a failure chunk whose `getFailure()` method returns the exception passed to `Content.Source.fail(Throwable)`.

When reading a normal chunk, its `ByteBuffer` is typically a slice of a different `ByteBuffer` that has been read by a lower layer.
There may be multiple layers between the bottom layer (where the initial read typically happens) and the application layer that calls `Content.Source.read()`.

By slicing the `ByteBuffer` (rather than copying its bytes), there is no copy of the bytes between the layers, which yields greater performance.
However, this comes with the cost that the `ByteBuffer`, and the associated `Content.Chunk`, have an intrinsic lifecycle: the final consumer of a `Content.Chunk` at the application layer must indicate when it has consumed the chunk, so that the bottom layer may reuse/recycle the `ByteBuffer`.

Consuming the chunk means that the bytes in the `ByteBuffer` are read (or ignored), and that the application will not look at or reference that `ByteBuffer` ever again.

`Content.Chunk` offers a retain/release model to deal with the `ByteBuffer` lifecycle, with a simple rule:

IMPORTANT: A `Content.Chunk` returned by a call to `Content.Source.read()` **must** be released, except for ``Content.Chunk``s that are failure chunks.
Failure chunks _may_ be released, but they do not _need_ to be.

The example below is the idiomatic way of reading from a `Content.Source`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ContentDocs.java[tags=idiomatic]
----
<1> The `read()` that must be paired with a `release()`.
<2> The `release()` that pairs the `read()`.

Note how the reads happen in a loop, consuming the `Content.Source` as soon as it has content available to be read, and therefore no backpressure is applied to the reads.

IMPORTANT: Calling `Content.Chunk.release()` must be done only after the bytes in the `ByteBuffer` returned by `Content.Chunk.getByteBuffer()` have been consumed.
When the `Content.Chunk` is released, the implementation may reuse the `ByteBuffer` and overwrite the bytes with different bytes; if the application looks at the `ByteBuffer` _after_ having released the `Content.Chunk` is may see other, unrelated, bytes.

An alternative way to read from a `Content.Source`, to use when the chunk is consumed asynchronously, and you don't want to read again until the `Content.Chunk` is consumed, is the following:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ContentDocs.java[tags=async]
----
<1> The `read()` that must be paired with a `release()`.
<2> The `release()` that pairs the `read()`.

Note how the reads do not happen in a loop, and therefore backpressure is applied to the reads, because there is not a next read until the chunk from the previous read has been consumed (and this may take time).

Since the `Chunk` is consumed asynchronously, you may need to retain it to extend its lifecycle, as explained in <<content-source-chunk,this section>>.

You can use `Content.Source` static methods to conveniently read (in a blocking way or non-blocking way), for example via `static Content.Source.asStringAsync(Content.Source, Charset)`, or via an `InputStream` using `static Content.Source.asInputStream(Content.Source)`.

Refer to the `Content.Source` link:{javadoc-url}/org/eclipse/jetty/io/Content.Source.html[`javadocs`] for further details.

[[content-source-chunk]]
=== `Content.Chunk`

`Content.Chunk` offers a retain/release API to control the lifecycle of its `ByteBuffer`.

When ``Content.Chunk``s are consumed synchronously, no additional retain/release API call is necessary, for example:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ContentDocs.java[tags=chunkSync]
----

On the other hand, if the `Content.Chunk` is not consumed immediately, then it must be retained, and you must arrange for the `Content.Chunk` to be released at a later time, thus pairing the retain.
For example, you may accumulate the ``Content.Chunk``s in a `List` to convert them to a `String` when all the ``Content.Chunk``s have been read.

Since reading from a `Content.Source` is asynchronous, the `String` result is produced via a `CompletableFuture`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ContentDocs.java[tags=chunkAsync]
----
<1> The `read()` that must be paired with a `release()`.
<2> The `release()` that pairs the `read()`.
<3> The `retain()` that must be paired with a `release()`.
<4> The `release()` that pairs the `retain()`.

Note how method `consume(Content.Chunk)` retains the `Content.Chunk` because it does not consume it, but rather stores it away for later use.
With this additional retain, the retain count is now `2`: one implicitly from the `read()` that returned the `Content.Chunk`, and one explicit in `consume(Content.Chunk)`.

However, just after returning from `consume(Content.Chunk)` the `Content.Chunk` is released (pairing the implicit retain from `read()`), so that the retain count goes to `1`, and an additional release is still necessary.

Method `getResult()` arranges to release all the ``Content.Chunk``s that have been accumulated, pairing the retains done in `consume(Content.Chunk)`, so that the retain count for the ``Content.Chunk``s goes finally to `0`.

[[content-sink]]
== `Content.Sink`

The high-level abstraction that Jetty offers to write bytes is `org.eclipse.jetty.io.Content.Sink`.

The primary method to use is `Content.Sink.write(boolean, ByteBuffer, Callback)`, which performs a non-blocking write of the given `ByteBuffer`, with the indication of whether the write is the last.

The `Callback` parameter is completed, successfully or with a failure, and possibly asynchronously by a different thread, when the write is complete.

Your application can typically perform zero or more non-last writes, and one final last write.

However, because the writes may be asynchronous, you cannot start a next write before the previous write is completed.

This code is wrong:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ContentDocs.java[tags=sinkWrong]
----

You must initiate a second write only when the first is finished, for example:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ContentDocs.java[tags=sinkMany]
----

When you need to perform an unknown number of writes, you may use an `IteratingCallback`, explained in <<echo,this section>>, to avoid ``StackOverFlowError``s.

For example, to download a large content in smaller chunks, you can do this:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ContentDocs.java[tags=sinkChunks]
----

If you want to copy from a `Content.Source` to a `Content.Sink` you could use the convenience method `Content.copy(Content.Source, Content.Sink, Callback)`.

For illustrative purposes, below you can find a more elaborate example with an implementation of `copy(Content.Source, Content.Sink, Callback)` that uses `IteratingCallback`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ContentDocs.java[tags=copy]
----

Non-blocking writes can be easily turned in blocking writes.
This leads to perhaps code that is simpler to read, but that also comes with a price: greater resource usage that may lead to less scalability and less performance.

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ContentDocs.java[tags=blocking]
----

---

### Documentation Jetty Modules Programming Guide Pages Arch Jmx (`documentation/jetty/modules/programming-guide/pages/arch/jmx.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Jetty JMX Support

The Java Management Extensions (JMX) APIs are standard API for managing and monitoring resources such as applications, devices, services, and the Java Virtual Machine itself.

The JMX API includes remote access, so a remote management console such as https://openjdk.java.net/projects/jmc/[Java Mission Control] can interact with a running application for these purposes.

Jetty architecture is based on xref:arch/bean.adoc[components] organized in a tree. Every time a component is added to or removed from the component tree, an event is emitted, and xref:arch/bean.adoc#listener-container[Container.Listener] implementations can listen to those events and perform additional actions.

`org.eclipse.jetty.jmx.MBeanContainer` listens to those events and registers/unregisters the Jetty components as MBeans into the platform MBeanServer.

The Jetty components are annotated with <<annotation,Jetty JMX annotations>> so that they can provide specific JMX metadata such as attributes and operations that should be exposed via JMX.

Therefore, when a component is added to the component tree, `MBeanContainer` is notified, it creates the MBean from the component POJO and registers it to the `MBeanServer`.
Similarly, when a component is removed from the tree, `MBeanContainer` is notified, and unregisters the MBean from the `MBeanServer`.

The Maven coordinates for the Jetty JMX support are:

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-jmx</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

== Enabling JMX Support

Enabling JMX support is always recommended because it provides valuable information about the system, both for monitoring purposes and for troubleshooting purposes in case of problems.

To enable JMX support on the server:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/JMXDocs.java[tags=server]
----

Similarly on the client:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/JMXDocs.java[tags=client]
----

[NOTE]
====
The MBeans exported to the platform MBeanServer can only be accessed locally (from the same machine), not from remote machines.

This means that this configuration is enough for development, where you have easy access (with graphical user interface) to the machine where Jetty runs, but it is typically not enough when the machine where Jetty runs is remote, or only accessible via SSH or otherwise without graphical user interface support.
In these cases, you have to enable <<remote,JMX Remote Access>>.
====

// TODO: add a section about how to expose logging once #4830 is fixed.

[[remote]]
== Enabling JMX Remote Access

There are two ways of enabling remote connectivity so that JMC can connect to the remote JVM to visualize MBeans.

* Use the `com.sun.management.jmxremote` system property on the command line.
Unfortunately, this solution does not work well with firewalls and is not flexible.
* Use Jetty's `ConnectorServer` class.

`org.eclipse.jetty.jmx.ConnectorServer` will use by default RMI to allow connection from remote clients, and it is a wrapper around the standard JDK class `JMXConnectorServer`, which is the class that provides remote access to JMX clients.

Connecting to the remote JVM is a two step process:

* First, the client will connect to the RMI _registry_ to download the RMI stub for the `JMXConnectorServer`; this RMI stub contains the IP address and port to connect to the RMI server, i.e. the remote `JMXConnectorServer`.
* Second, the client uses the RMI stub to connect to the RMI _server_ (i.e. the remote `JMXConnectorServer`) typically on an address and port that may be different from the RMI registry address and port.

The host and port configuration for the RMI registry and the RMI server is specified by a `JMXServiceURL`.
The string format of an RMI `JMXServiceURL` is:

[,screen]
----
service:jmx:rmi://<rmi_server_host>:<rmi_server_port>/jndi/rmi://<rmi_registry_host>:<rmi_registry_port>/jmxrmi
----

Default values are:

[,screen]
----
rmi_server_host = localhost
rmi_server_port = 1099
rmi_registry_host = localhost
rmi_registry_port = 1099
----

With the default configuration, only clients that are local to the server machine can connect to the RMI registry and RMI server - this is done for security reasons.
With this configuration it would still be possible to access the MBeans from remote using a <<remote-ssh-tunnel,SSH tunnel>>.

By specifying an appropriate `JMXServiceURL`, you can fine tune the network interfaces the RMI registry and the RMI server bind to, and the ports that the RMI registry and the RMI server listen to.
The RMI server and RMI registry hosts and ports can be the same (as in the default configuration) because RMI is able to multiplex traffic arriving to a port to multiple RMI objects.

If you need to allow JMX remote access through a firewall, you must open both the RMI registry and the RMI server ports.

`JMXServiceURL` common examples:

[,screen]
----
service:jmx:rmi:///jndi/rmi:///jmxrmi
  rmi_server_host = local host address
  rmi_server_port = randomly chosen
  rmi_registry_host = local host address
  rmi_registry_port = 1099

service:jmx:rmi://0.0.0.0:1099/jndi/rmi://0.0.0.0:1099/jmxrmi
  rmi_server_host = any address
  rmi_server_port = 1099
  rmi_registry_host = any address
  rmi_registry_port = 1099

service:jmx:rmi://localhost:1100/jndi/rmi://localhost:1099/jmxrmi
  rmi_server_host = loopback address
  rmi_server_port = 1100
  rmi_registry_host = loopback address
  rmi_registry_port = 1099
----

[NOTE]
====
When `ConnectorServer` is started, its RMI stub is exported to the RMI registry.
The RMI stub contains the IP address and port to connect to the RMI object, but the IP address is typically the machine host name, not the host specified in the `JMXServiceURL`.

To control the IP address stored in the RMI stub you need to set the system property `java.rmi.server.hostname` with the desired value.
This is especially important when binding the RMI server host to the loopback address for security reasons. See also <<remote-ssh-tunnel,JMX Remote Access via SSH Tunnel.>>
====

To allow JMX remote access, create and configure a `ConnectorServer`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/JMXDocs.java[tags=remote]
----

[[remote-authorization]]
=== JMX Remote Access Authorization

The standard `JMXConnectorServer` provides several options to authorize access, for example via JAAS or via configuration files.
For a complete guide to controlling authentication and authorization in JMX, see https://docs.oracle.com/en/java/javase/11/management/[the official JMX documentation].

In the sections below we detail one way to setup JMX authentication and authorization, using configuration files for users, passwords and roles:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/JMXDocs.java[tags=remoteAuthorization]
----

The `users.access` file format is defined in the `$JAVA_HOME/conf/management/jmxremote.access` file.
A simplified version is the following:

.users.access
[,screen]
----
user1 readonly
user2 readwrite
----

The `users.password` file format is defined in the `$JAVA_HOME/conf/management/jmxremote.password.template` file.
A simplified version is the following:

.users.password
[,screen]
----
user1 password1
user2 password2
----

CAUTION: The `users.access` and `users.password` files are not standard `*.properties` files -- the user must be separated from the role or password by a space character.

=== Securing JMX Remote Access with TLS

The JMX communication via RMI happens by default in clear-text.

It is possible to configure the `ConnectorServer` with a `SslContextFactory` so that the JMX communication via RMI is encrypted:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/JMXDocs.java[tags=tlsRemote]
----

It is possible to use the same `SslContextFactory.Server` used to configure the Jetty `ServerConnector` that supports TLS also for the JMX communication via RMI.

The keystore must contain a valid certificate signed by a Certification Authority.

The RMI mechanic is the usual one: the RMI client (typically a monitoring console) will connect first to the RMI registry (using TLS), download the RMI server stub that contains the address and port of the RMI server to connect to, then connect to the RMI server (using TLS).

This also mean that if the RMI registry and the RMI server are on different hosts, the RMI client must have available the cryptographic material to validate both hosts.

Having certificates signed by a Certification Authority simplifies by a lot the configuration needed to get the JMX communication over TLS working properly.

If that is not the case (for example the certificate is self-signed), then you need to specify the required system properties that allow RMI (especially when acting as an RMI client) to retrieve the cryptographic material necessary to establish the TLS connection.

For example, trying to connect using the JDK standard `JMXConnector` with both the RMI server and the RMI registry via TLS to `domain.com` with a self-signed certificate:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/JMXDocs.java[tags=tlsJMXConnector]
----

Similarly, to launch JMC:

[,screen]
----
$ jmc -vmargs -Djavax.net.ssl.trustStore=/path/to/trustStore -Djavax.net.ssl.trustStorePassword=secret
----

IMPORTANT: These system properties are required when launching the `ConnectorServer` too, on the server, because it acts as an RMI client with respect to the RMI registry.

[[remote-ssh-tunnel]]
=== JMX Remote Access with Port Forwarding via SSH Tunnel

You can access JMX MBeans on a remote machine when the RMI ports are not open, for example because of firewall policies, but you have SSH access to the machine using local port forwarding via an SSH tunnel.

In this case you want to configure the `ConnectorServer` with a `JMXServiceURL` that binds the RMI server and the RMI registry to the loopback interface only: `service:jmx:rmi://localhost:1099/jndi/rmi://localhost:1099/jmxrmi`.

Then you setup the local port forwarding with the SSH tunnel:

[,screen]
----
$ ssh -L 1099:localhost:1099 <user>@<machine_host>
----

Now you can use JConsole or JMC to connect to `localhost:1099` on your local computer.
The traffic will be forwarded to `machine_host` and when there, SSH will forward the traffic to `localhost:1099`, which is exactly where the `ConnectorServer` listens.

When you configure `ConnectorServer` in this way, you must set the system property `-Djava.rmi.server.hostname=localhost`, on the server.
This is required because when the RMI server is exported, its address and port are stored in the RMI stub. You want the address in the RMI stub to be `localhost` so that when the RMI stub is downloaded to the remote client, the RMI communication will go through the SSH tunnel.

[[annotation]]
== Jetty JMX Annotations

The Jetty JMX support, and in particular `MBeanContainer`, is notified every time a bean is added to the component tree.

The bean is scanned for Jetty JMX annotations to obtain JMX metadata: the JMX attributes and JMX operations.

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/JMXDocs.java[tags=jmxAnnotation]
----

The JMX metadata and the bean are wrapped by an instance of `org.eclipse.jetty.jmx.ObjectMBean` that exposes the JMX metadata and, upon request from JMX consoles, invokes methods on the bean to get/set attribute values and perform operations.

You can provide a custom subclass of `ObjectMBean` to further customize how the bean is exposed to JMX.

The custom `ObjectMBean` subclass must respect the following naming convention: `<package>.jmx.<class>MBean`.
For example, class `com.acme.Foo` may have a custom `ObjectMBean` subclass named `com.acme.**jmx**.Foo**MBean**`.

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/JMXDocs.java[tags=jmxCustomMBean]
----

The custom `ObjectMBean` subclass is also scanned for Jetty JMX annotations and overrides the JMX metadata obtained by scanning the bean class.
This allows to annotate only the custom `ObjectMBean` subclass and keep the bean class free of the Jetty JMX annotations.

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/JMXDocs.java[tags=jmxCustomMBeanOverride]
----

The scan for Jetty JMX annotations is performed on the bean class and all the interfaces implemented by the bean class, then on the super-class and all the interfaces implemented by the super-class and so on until `java.lang.Object` is reached.
For each type -- class or interface, the corresponding `+*.jmx.*MBean+` is looked up and scanned as well with the same algorithm.
For each type, the scan looks for the class-level annotation `@ManagedObject`.
If it is found, the scan looks for method-level `@ManagedAttribute` and `@ManagedOperation` annotations; otherwise it skips the current type and moves to the next type to scan.

=== @ManagedObject

The `@ManagedObject` annotation is used on a class at the top level to indicate that it should be exposed as an MBean.
It has only one attribute to it which is used as the description of the MBean.

=== @ManagedAttribute

The `@ManagedAttribute` annotation is used to indicate that a given method is exposed as a JMX attribute.
This annotation is placed always on the getter method of a given attribute.
Unless the `readonly` attribute is set to `true` in the annotation, a corresponding setter is looked up following normal naming conventions.
For example if this annotation is on a method called `String getFoo()` then a method called `void setFoo(String)` would be looked up, and if found wired as the setter for the JMX attribute.

=== @ManagedOperation

The `@ManagedOperation` annotation is used to indicate that a given method is exposed as a JMX operation.
A JMX operation has an _impact_ that can be `INFO` if the operation returns a value without modifying the object, `ACTION` if the operation does not return a value but modifies the object, and "ACTION_INFO" if the operation both returns a value and modifies the object.
If the _impact_ is not specified, it has the default value of `UNKNOWN`.

=== @Name

The `@Name` annotation is used to assign a name and description to parameters in method signatures so that when rendered by JMX consoles it is clearer what the parameter meaning is.

---

### Documentation Jetty Modules Programming Guide Pages Arch Json (`documentation/jetty/modules/programming-guide/pages/arch/json.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Jetty JSON Support

link:https://json.org[JSON] is a wide-spread format for data exchange.

Jetty offers a JSON library to parse JSON into objects, both synchronously and asynchronously, and to generate JSON from objects.

The Maven coordinates for the Jetty JSON support are:

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-util-ajax</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

[[parsing]]
== JSON Parsing

JSON parsing is available in two flavors:

* Synchronous parsing, where the JSON document is fully available
* Asynchronous parsing, where the JSON document is not fully available, and it is provided in chunks to the parser.

You can use asynchronous parsing also if the JSON document is fully available (there will be just one chunk representing the whole JSON document).
You cannot use synchronous parsing without the full JSON document (the synchronous parser will produce an error, complaining that the JSON document is incomplete).

For synchronous parsing, you can use the `org.eclipse.jetty.util.ajax.JSON` class:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/JSONDocs.java[tags=sync]
----

For asynchronous parsing, you can use `org.eclipse.jetty.util.ajax.AsyncJSON`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/JSONDocs.java[tags=async]
----

By default, both parsers produce a `Map<String, Object>` for JSON objects.

By default, the `JSON` parser produces `Object[]` for JSON arrays, while the `AsyncJSON` parser produces `List<Object>` for JSON arrays.
For both `JSON` and `AsyncJSON.Factory` you can call `setArrayConverter(Function<List<?>, Object>)` to convert JSON arrays, provided as `List<Object>` by the implementation, into the preferred data structure, typically either a Java array or a Java list.

By default, numbers are `Long` when they are integer (without a fractional part), `Double` when they are non-integer (with a fractional part).

[[parsing-custom]]
=== Custom Objects Parsing

The Jetty JSON library supports converting JSON object into custom objects, using the `class` field (or the `x-class` field, in case `class` cannot be used).

The Java class specified in the `class` or `x-class` field must either implement `JSON.Convertible` or be mapped to a `JSON.Convertor` in the `JSON` instance or the `AsyncJSON.Factory` instance.
For example:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/JSONDocs.java[tags=xclass;!xclass-helper]
----

NOTE: Unlike other popular JSON libraries, the Jetty JSON library does not use Java annotations to map Java object and fields to JSON objects and fields.

[TIP]
====
The Jetty JSON library provides useful convertors out of the box:

* `JSONCollectionConvertor` that converts `Collection` instances as JSON objects, so that the specific `Collection` implementation (if it is a `public` class) can be restored. This is useful when the `Collection` is a `HashSet` or `ArrayDeque`, or when it needs to be a specific `List` implementation such as `LinkedList`.
* `JSONDateConvertor`, that converts from/to `java.util.Date`.
* `JSONEnumConvertor`, that converts from/to `enum` constants.
* `JSONPojoConvertor`, that converts from/to plain old Java objects (POJO) using reflection to invoke getters and setters.
====

[[generation]]
== JSON Generation

JSON generation is available via the `org.eclipse.jetty.util.ajax.JSON` class.

Simple `Map<String, Object>` objects can be directly generated:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/JSONDocs.java[tags=simpleGenerate]
----

NOTE: The `JSON` object used as the parser and as the generator is typically just one instance, used for both parsing and generation.
This also centralizes the configuration of convertors.

[[generation-custom]]
=== Custom Objects Generation

Similarly to the <<parsing-custom,custom objects parsing>>, generation of custom objects into the JSON format relies on:

* The object is composed (recursively) only of _simple_ JSON types: `Map<String, Object>`, `String`, `Number` (including primitive number types), `Boolean` (including `boolean`), and `Collection` or arrays of these simple types.
* The object being an instance of a Java `record`.
* The class of the object implementing `JSON.Convertible`.
* A `JSON.Convertor` configured in the `JSON` instance for the class of the object.

CAUTION: Generic objects that do not match the requisites above are converted to strings by calling `toString()`, and therefore the object structure will be lost (and the object cannot be reconstructed during JSON parsing).

---

### Documentation Jetty Modules Programming Guide Pages Arch Listener (`documentation/jetty/modules/programming-guide/pages/arch/listener.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Jetty Listeners

The Jetty architecture is based on xref:arch/bean.adoc[components], typically organized in a component tree.
These components have an internal state that varies with the component life cycle (that is, whether the component is started or stopped), as well as with the component use at runtime.
The typical example is a thread pool, whose internal state -- such as the number of pooled threads or the job queue size -- changes as the thread pool is used by the running client or server.

In many cases, the component state change produces an event that is broadcast to listeners.
Applications can register listeners to these components to be notified of the events they produce.

This section lists the listeners available in the Jetty components, but the events and listener APIs are discussed in the component specific sections.

Listeners common to both client and server:

* xref:arch/bean.adoc#listener[Component Listeners]
* xref:arch/io.adoc#connection-listener[Connection Listeners]
* xref:client/http.adoc#configuration-tls-listener[TLS/SSL Listeners]

Listeners that are server specific:

* xref:server/http.adoc#request-processing-events[Request Processing Listeners]

Listeners that are client specific:

* xref:client/http.adoc#listeners[Request and Response Listeners]
* xref:client/http.adoc#connection-events[Connect Listeners]

---

### Documentation Jetty Modules Programming Guide Pages Arch Threads (`documentation/jetty/modules/programming-guide/pages/arch/threads.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Jetty Threading Architecture

Writing a performant client or server is difficult, because it should:

* Scale well with the number of processors.
* Be efficient at using processor caches to avoid https://en.wikipedia.org/wiki/Parallel_slowdown[parallel slowdown].
* Support multiple network protocols that may have very different requirements. +
For example, multiplexed protocols such as HTTP/2 introduce new challenges that are not present in non-multiplexed protocols such as HTTP/1.1.
* Support different application threading models. +
For example, when a Jetty server invokes application code that calls blocking APIs, the Jetty server should not be affected by how long the blocking API calls take, and must process other connections or other requests in a timely fashion.

[[execution-strategy]]
== Execution Strategies

The Jetty threading architecture can be modeled with a producer/consumer pattern, where produced tasks needs to be consumed efficiently.

For example, Jetty produces (among others) these tasks:

* A task that wraps a NIO selection event, see the xref:arch/io.adoc[Jetty I/O architecture].
* A task that wraps the invocation of application code that may block (for example, the invocation of a Servlet to handle an HTTP request).

A task is typically a `Runnable` object that may implement `org.eclipse.jetty.util.thread.Invocable`.
The `Invocable` interface contains a single method that returns the `Invocable.InvocationType` of the task, that indicates the behavior of the task (in particular, whether the task may block or not).

Applications can influence the behavior of the tasks by properly configuring Jetty components, as explained in xref:server/threads.adoc[this section] for the server-side and in xref:client/threads.adoc[this section] for the client-side.

Once a task has been produced, it may be consumed using these modes:

* <<execution-strategy-pc,`Produce-Consume`>>
* <<execution-strategy-pec,`Produce-Execute-Consume`>>
* <<execution-strategy-epc,`Execute-Produce-Consume`>>
* <<execution-strategy-adaptive,Adaptive Execution Strategy>>

In the diagrams below, the green color refers to task production, and the blue color refers to task consumption.

[plantuml]
----
skinparam backgroundColor transparent

Title Legend

compact concise "Producer Thread" as PT
compact concise "Consumer Thread" as CT
hide time-axis

@PT
0 is "This Thread Produced Task T1" #lightgreen
4 is {hidden}

@CT
4 is "This Thread Consumes Task T1" #dodgerblue
9 is {hidden}
----

In the diagrams below, `Pn` represents production of task `n` and `Cn` represents consuming task 'n' by running it.

[[execution-strategy-pc]]
=== Produce-Consume
In the `Produce-Consume` mode, the `Producer Thread` loops producing tasks (`T1`, `T2`, ..., `Tn`) and directly consumes those tasks itself by running them.

[plantuml]
----
skinparam backgroundColor transparent

compact concise "Producer Thread" as PT
hide time-axis

@PT
0 is T1 #lightgreen
1 is T1 #dodgerblue
5 is T2 #lightgreen
6 is T2 #dodgerblue
8 is T3 #lightgreen
9 is T3 #dodgerblue
12 is {hidden}
----

If the task is a NIO selection event, then this mode is the thread-per-selector mode which is very CPU core cache efficient, but suffers from the http://en.wikipedia.org/wiki/Head-of-line_blocking[head-of-line blocking]: if one of the tasks blocks or runs slowly, then subsequent tasks cannot be produced (and therefore cannot be consumed either) and will pay in latency the cost of consuming previous, possibly unrelated, tasks.

This mode should only be used if the produced task is known to never block, or if the system tolerates well (or does not care about) head-of-line blocking.

This mode is equivalent to the "selector loop" or "event loop" models typical of non-blocking I/O frameworks such as link:https://nodejs.org/[NodeJS], link:https://netty.io/[Netty] and others.

[[execution-strategy-pec]]
=== Produce-Execute-Consume
In the `Produce-Execute-Consume` mode, the `Producer Thread` loops producing tasks (`T1`, `T2`, ..., `Tn`) that are submitted to a `java.util.concurrent.Executor` to be consumed by a `Worker Thread` different from the `Producer Thread`.

[plantuml]
----
skinparam backgroundColor transparent

compact concise "Producer Thread" as PT
compact concise "Worker Thread A" as WTA
compact concise "Worker Thread B" as WTB
compact concise "Worker Thread C" as WTC
hide time-axis

@PT
0 is T1 #lightgreen
1 is T2 #lightgreen
2 is T3 #lightgreen
3 is T4 #lightgreen
4 is {hidden}

@WTA
1 is T1 #dodgerblue
5 is {hidden}

@WTB
2 is T2 #dodgerblue
4 is T4 #dodgerblue
8 is {hidden}

@WTC
3 is T3 #dodgerblue
6 is {hidden}
----

The `Executor` implementation typically adds the task to a queue, and dequeues the task when there is a worker thread available to consume it.

This mode solves the head-of-line blocking discussed in the <<execution-strategy-pc,`Produce-Consume` section>>, but suffers from other issues:

* It is not CPU cache efficient, because the data available to the producer thread will likely be consumed by another thread running on a different CPU core, which won’t already have that data in its cache.
* The `Executor` queue may grow indefinitely, if there are fewer threads than tasks, and the tasks take time to run.
* A small latency is added to the processing of every task: the time it waits in the `Executor` queue.

[[execution-strategy-epc]]
=== Execute-Produce-Consume
In the `Execute-Produce-Consume` mode, there is no dedicated producer thread.
Rather, a thread that is acting as the producer will consume the tasks that it produces whilst it submits an internal task to an `Executor` so that a different thread can take over production.

[plantuml]
----
skinparam backgroundColor transparent

compact concise "Thread A" as WTA
compact concise "Thread B" as WTB
compact concise "Thread C" as WTC
compact concise "Thread D" as WTD
hide time-axis

@WTA
0 is T1 #lightgreen
1 is T1 #dodgerblue
5 is {hidden}

@WTB
1 is T2 #lightgreen
2 is T2 #dodgerblue
4 is T5 #lightgreen
5 is T5 #dodgerblue
10 is {hidden}

@WTC
2 is T3 #lightgreen
3 is T3 #dodgerblue
6 is {hidden}

@WTD
3 is T4 #lightgreen
4 is T4 #dodgerblue
8 is {hidden}

----

This mode may operate similar to <<execution-strategy-pc,`Produce-Consume`>> because after a thread consumes a task it may become the producer again, as `Thread B` does above.

Differently from `Produce-Execute-Consume`, a task is consumed by the same thread that produced it (which is CPU core cache efficient).
A different thread will be used to continue production.
However, if the executor does not have an idle thread immediately available to continue production, then the production of tasks may be delayed or even hang.

[[execution-strategy-adaptive]]
=== Adaptive Execution Strategy
The modes of task consumption discussed above are captured by the `org.eclipse.jetty.util.thread.ExecutionStrategy` interface and its implementations:

* `org.eclipse.jetty.util.thread.strategy.ProduceConsume` for the `Produce-Consume` mode.
* `org.eclipse.jetty.util.thread.strategy.ProduceExecuteConsume` for the `Produce-Execute-Consume` mode.
* `org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume` for the `Execute-Produce-Consume` mode.

There is an additional implementation of `ExecutionStrategy`, namely `org.eclipse.jetty.util.thread.strategy.AdaptiveExecutionStrategy`, that heuristically chooses between the other strategies.

`AdaptiveExecutionStrategy` chooses one of the previous strategies taking into account both the immediate availability of another thread, and the `InvocationType` of the task when the task implements the `Invocable` interface.

For example, a task that declares itself with `InvocationType` `NON_BLOCKING` can be consumed using the `Produce-Consume` mode, since there is no risk to stop production because the task will not block (although there is the risk of <<execution-strategy-pc,head-of-line blocking>>).

Conversely, a task that declares itself with `InvocationType` `BLOCKING` (or a task that does not implement `Invocable`) may block and therefore stop production, and must be consumed using either the `Produce-Execute-Consume` mode or the `Execute-Produce-Consume` mode.
Deciding between these two modes depends on whether there is an idle thread immediately available to take over production, and this is captured by the `org.eclipse.jetty.util.thread.TryExecutor` interface.

The immediate availability of another thread is determined by an implementation of `TryExecutor` that can be asked whether a thread can be immediately assigned to run a task, as opposed to a normal `Executor` that can only queue the task in the expectation that there will be a thread available in the near future to run the task.

The concept of task consumption modes, coupled with `Invocable` tasks that expose their own `InvocationType`, coupled with a `TryExecutor` that guarantees whether production can be immediately taken over, are all captured by `AdaptiveExecutionStrategy`, which is the default Jetty execution strategy.

`AdaptiveExecutionStrategy` can seamlessly switch among consumption modes on a per-task basis.

When all tasks are `NON_BLOCKING`, `AdaptiveExecutionStrategy` functions in `Produce-Consume` mode, resembling "selector loop" or "event loop" frameworks.

When tasks are `BLOCKING` and no reserved threads are available, `AdaptiveExecutionStrategy` functions in `Produce-Execute-Consume` mode, resembling the classic model of a dedicated "selector thread" that always queues tasks to an `Executor`, to be executed by worker threads.

When tasks are `BLOCKING` and reserved threads are available, it functions in `Execute-Produce-Consume` mode, so that tasks are processed more efficiently.

[plantuml]
----
skinparam backgroundColor transparent

compact concise "Thread A" as WTA
compact concise "Thread B" as WTB
compact concise "Thread C" as WTC
hide time-axis

@WTA
0 is T1 #lightgreen
1 is T1 #dodgerblue
5 is T3 #dodgerblue
8 is {hidden}

@WTB
1 is T2 #lightgreen
2 is T2 #dodgerblue
4 is T5 #lightgreen
5 is T5 #dodgerblue
10 is {hidden}

@WTC
2 is T3 #lightgreen
3 is T4 #lightgreen
4 is T4 #dodgerblue
6 is {hidden}
----

In the diagram above, `Thread C` produces task `T3`, and because there is not an immediately available thread, it uses `Produce-Execute-Consume`, queueing task `T3` for later execution.
It then continues production by producing `T4`, but now `Thread B` has become immediately available to take over production, so `Thread C` uses `Execute-Produce-Consume` to consume `T4` directly.

[NOTE]
====
`AdaptiveExecutionStrategy` was previously named `EatWhatYouKill`, named after a hunting proverb in the sense that one should produce (kill) only what it consumes (eats).
====

[[thread-pool]]
== Thread Pool
Jetty's xref:arch/threads.adoc[threading architecture] requires a more sophisticated thread pool than what offered by Java's `java.util.concurrent.ExecutorService`.

Jetty's default thread pool implementation is link:{javadoc-url}/org/eclipse/jetty/util/thread/QueuedThreadPool.html[`QueuedThreadPool`].

`QueuedThreadPool` integrates with the xref:arch/bean.adoc[Jetty component model], implements `Executor`, provides a `TryExecutor` implementation (discussed in the <<execution-strategy-adaptive,adaptive execution strategy section>>), and supports <<thread-pool-virtual-threads,virtual threads>> (introduced as a preview feature in Java 19 and Java 20, and as an official feature since Java 21).

[[thread-pool-queue]]
=== Thread Pool Queue

`QueuedThreadPool` uses a `BlockingQueue` to store tasks that will be executed as soon as a thread is available.

It is common, but too simplistic, to think that an upper bound to the thread pool queue is a good way to limit the number of concurrent HTTP requests.

In case of asynchronous servers like Jetty, applications may have more than one thread handling a single request.
Furthermore, the server implementation may produce a number of tasks that _must_ be run by the thread pool, otherwise the server stops working properly.

Therefore, the "one-thread-per-request" model is too simplistic, and the real model that predicts the number of threads that are necessary is too complicated to produce an accurate value.

For example, a sudden large spike of requests arriving to the server may find the thread pool in an idle state where the number of threads is shrunk to the minimum.
This will cause many tasks to be queued up, way before an HTTP request is even read from the network.
Add to this that there could be I/O failures processing requests, which may be submitted as a new task to the thread pool.
Furthermore, multiplexed protocols like HTTP/2 have a much more complex model (due to xref:server/http2.adoc#flow-control[data flow control]).
For multiplexed protocols, the implementation must be able to write in order to progress reads (and must be able to read in order to progress writes), possibly causing more tasks to be submitted to the thread pool.

If any of the submitted tasks is rejected because the queue is bounded the server may grind to a halt, because the task _must_ be executed, sometimes _necessarily_ in a different thread.

For these reasons:

IMPORTANT: The thread pool queue must be unbounded.

There are better strategies to limit the number of concurrent requests, discussed in xref:server/http.adoc#handler-use-qos[this section].

[[thread-pool-configuration]]
=== `QueuedThreadPool` configuration

`QueuedThreadPool` can be configured with a `maxThreads` value.

However, some of the Jetty components (such as the xref:arch/io.adoc#selector-manager[selectors]) steal threads for their internal use, or rather `QueuedThreadPool` leases some threads to these components.
These threads are reported by `QueuedThreadPool.leasedThreads` and are not available to run application code.

`QueuedThreadPool` can be configured with a `reservedThreads` value.
This value represents the maximum number of threads that can be reserved and used by the `TryExecutor` implementation.
A negative value for `QueuedThreadPool.reservedThreads` means that the actual value will be heuristically derived from the number of CPU cores and `QueuedThreadPool.maxThreads`.
A value of zero for `QueuedThreadPool.reservedThreads` means that reserved threads are disabled, and therefore the <<execution-strategy-epc,`Execute-Produce-Consume` mode>> is never used -- the <<execution-strategy-pec,`Produce-Execute-Consume` mode>> is always used instead.

`QueuedThreadPool` leases threads to the following Jetty components:

* The xref:server/http.adoc#connector[`Connector`]s; for each connector a thread is leased for each xref:server/http.adoc#connector-acceptors[acceptor] and each xref:server/http.adoc#connector-selectors[selector].
* The `TryExecutor` implementation; for the default implementation `ReservedThreadExecutor`, `QueuedThreadPool.reservedThreads` are leased.

To carefully control the minimum number of threads in `QueuedThreadPool`, you must explicitly specify the number of selectors and the number of acceptors for each `Connector`, and the number of reserved threads.

For example:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ArchitectureDocs.java[tags=threadPoolBudget]
----

In the example above, `6` threads will be leased: `2` reserved threads, `1` acceptor thread and `3` selector threads.

To reduce the number of leased threads, you can configure the acceptors to `0`, the selectors to `1` and the reserved threads to `0`.

[WARNING]
====
Setting the acceptors to `0` results in TCP connections being accepted in non-blocking mode, which may increase the load on the selector, and possibly in increased latency.

Setting the reserved threads to `0` results in the <<execution-strategy-pec,`Produce-Execute-Consume` mode>> to be used instead of the <<execution-strategy-epc,`Execute-Produce-Consume` mode>>.

You have to try and benchmark what is the best configuration for your use case.
====

`QueuedThreadPool` always maintains the number of threads between `QueuedThreadPool.minThreads` and `QueuedThreadPool.maxThreads`; during load spikes the number of thread grows to meet the load demand, and when the load on the system diminishes or the system goes idle, the number of threads shrinks.

The number of leased threads must be taken into account to configure `QueuedThreadPool.maxThreads`.
During `Server` startup, `QueuedThreadPool` leases threads to the Jetty components, and there must be enough threads for these components, and at least `1` more thread to handle requests.
In the example above, `QueuedThreadPool.maxThreads` must be set to `7` or more.

An alternative to explicitly setting the configuration of the Jetty components to which threads are leased, you can leave their configuration at default so that it is calculated by internal Jetty heuristics, and then adjust `QueuedThreadPool.maxThreads` afterwards:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ArchitectureDocs.java[tags=maxThreadsAfterStart]
----

[WARNING]
====
Do not try to reduce the value of `QueuedThreadPool.maxThreads` to the minimum possible.

Jetty may require threads to run internal tasks, and if no threads are available, because they are leased or busy handling requests, the server performance may degrade.

`QueuedThreadPool` can shrink the number of threads when they are not used, as explained below, so it is typically safe to leave `QueuedThreadPool.maxThreads` to its default, or to a value calculated using the max number of leased threads `L`, plus the number of concurrent threads to handle requests `H`, so that `QueuedThreadPool.maxThreads=L+H`.

Consider to choose a generous value for `H`, for example one that takes into account load spikes, since `QueuedThreadPool` shrinking will eventually remove threads that are unused.
====

Shrinking `QueuedThreadPool` is important in particular in containerized environments, where typically you want to return the memory occupied by the threads to the operative system.
The shrinking of the `QueuedThreadPool` is controlled by two parameters: `QueuedThreadPool.idleTimeout` and `QueuedThreadPool.maxEvictCount`.

`QueuedThreadPool.idleTimeout` indicates how long a thread should stay around when it is idle, waiting for tasks to execute.
The longer the threads stay around, the more ready they are in case of new load spikes on the system; however, they consume resources: a Java platform thread typically allocates 1 MiB of native memory.

`QueuedThreadPool.maxEvictCount` controls how many idle threads are evicted for one `QueuedThreadPool.idleTimeout` period.
The larger this value is, the quicker the threads are evicted when the `QueuedThreadPool` is idle or has less load, and their resources returned to the operative system; however, large values may result in too much thread thrashing: the `QueuedThreadPool` shrinks too fast and must re-create a lot of threads in case of a new load spike on the system.

A good balance between `QueuedThreadPool.idleTimeout` and `QueuedThreadPool.maxEvictCount` depends on the load profile of your system, and it is often tuned via trial and error.

[[thread-pool-virtual-threads]]
=== Virtual Threads
Virtual threads have been introduced in Java 19 and Java 20 as a preview feature, and have become an official feature since Java 21.

NOTE: In Java versions where virtual threads are a preview feature, remember to add `+--enable-preview+` to the JVM command line options to use virtual threads.

[[thread-pool-virtual-threads-queued]]
==== Virtual Threads Support with `QueuedThreadPool`

`QueuedThreadPool` can be configured to use virtual threads by specifying the virtual threads `Executor`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ArchitectureDocs.java[tags=queuedVirtual]
----

[CAUTION]
====
Jetty cannot enforce that the `Executor` passed to `setVirtualThreadsExecutor(Executor)` uses virtual threads, so make sure to specify a _virtual_ threads `Executor` and not a normal `Executor` that uses platform threads.
====

`AdaptiveExecutionStrategy` makes use of this setting when it determines that a task should be run with the <<execution-strategy-pec,`Produce-Execute-Consume` mode>>: rather than submitting the task to `QueuedThreadPool` to be run in a platform thread, it submits the task to the virtual threads `Executor`.

[NOTE]
====
Enabling virtual threads in `QueuedThreadPool` will default the number of reserved threads to zero, unless the number of reserved threads is explicitly configured to a positive value.

Defaulting the number of reserved threads to zero ensures that the <<execution-strategy-pec,Produce-Execute-Consume mode>> is always used, which means that virtual threads will always be used for blocking tasks.
====

[[thread-pool-virtual-threads-virtual]]
==== Virtual Threads Support with `VirtualThreadPool`

`VirtualThreadPool`  is an alternative to `QueuedThreadPool` that creates only virtual threads (no platform threads).

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ArchitectureDocs.java[tags=virtualVirtual]
----

Despite the name, `VirtualThreadPool` does not pool virtual threads, but allows you to impose a limit on the maximum number of concurrent tasks run by virtual threads, using a `Semaphore`.

Limiting the number of concurrent tasks run by virtual threads helps to limit resource usage in applications, especially in the case of load spikes.
When an unlimited number of tasks run by virtual threads is allowed, the server might be brought down due to resource (typically memory) exhaustion.

Furthermore, you can configure `VirtualThreadPool` to track virtual threads so that a xref:troubleshooting/component-dump.adoc[Jetty component dump] will show all virtual threads currently in use, including those that are unmounted.

[NOTE]
====
`VirtualThreadPool` does not queue the tasks submitted to it but rather spawns a virtual thread to run the task.
When the virtual thread runs, it attempts to acquire a permit from the `Semaphore`, and only if it succeeds to acquire the permit then the task is run.

This means that there may be more virtual threads than permits, but it is guaranteed that the number of concurrent tasks run by virtual threads is limited by the `Semaphore`.
====

[[thread-pool-virtual-threads-pinning]]
==== Virtual Threads Pinning

Even when using virtual threads, Jetty uses non-blocking I/O, and dedicates a thread to each `java.nio.channels.Selector` to perform the `Selector.select()` operation.

Up to Java 23, calling `Selector.select()` from a virtual thread *pins* the carrier thread.

Since Java 24, calling `Selector.select()` from a virtual thread does *not* pin the carrier thread.

[[thread-pool-virtual-threads-pinning-java23]]
===== Virtual Threads Pinning with Java 19 to 23

If you configure a server-side `Connector`, or Jetty's `HttpClient` with `N` selectors, then `N` carrier threads will be pinned by the virtual threads calling `Selector.select()`.

If you have less than `N` CPU cores in your system, then by default all carriers will be pinned in the `Selector.select()` call, leaving no carrier to execute virtual threads, and therefore completely locking up your system, which will become completely unresponsive.

If you have more than `N` CPU cores in your system, then by default your system may be less efficient, since the carrier threads may be pinned in the `Selector.select()` call, and therefore not available to run virtual threads.

[WARNING]
====
The number of CPU cores of your system determines, by default, the number of carrier threads.
The number of carrier threads may be explicitly configured by setting the system property `-Djdk.virtualThreadScheduler.parallelism=N`, where `N` is your desired number of carrier threads.

Selector threads used by Jetty pin carrier threads.

Choose the number of selectors wisely when using virtual threads: the number of selectors must always be less than the number of carrier threads, to leave some of the carrier threads free to run virtual threads.

As an extreme example, if your system only has `1` CPU core, then `1` selector is enough to pin the only carrier thread, and your system will eventually lock up.
In this case, you must explicitly configure the number of carrier threads by setting the system property `-Djdk.virtualThreadScheduler.parallelism=2` (or to a larger value).
====

---

### Documentation Jetty Modules Programming Guide Pages Client Http (`documentation/jetty/modules/programming-guide/pages/client/http.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= HTTP Client

[[intro]]
== HttpClient Introduction

The Jetty HTTP client module provides easy-to-use APIs and utility classes to perform HTTP (or HTTPS) requests.

Jetty's HTTP client is non-blocking and asynchronous.
It offers an asynchronous API that never blocks for I/O, making it very efficient in thread utilization and well suited for high performance scenarios such as load testing or parallel computation.

However, when all you need to do is to perform a `GET` request to a resource, Jetty's HTTP client offers also a synchronous API; a programming interface where the thread that issued the request blocks until the request/response conversation is complete.

Jetty's HTTP client supports different <<transport,HTTP formats>>: HTTP/1.1, HTTP/2, HTTP/3 and FastCGI.
Each format has a different `HttpClientTransport` implementation, that in turn use a xref:client/io-arch.adoc#transport[low-level transport] to communicate with the server.

This means that the semantic of an HTTP request such as: " ``GET`` the resource ``/index.html`` " can be carried over the low-level transport in different formats.
The most common and default format is HTTP/1.1.
That said, Jetty's HTTP client can carry the same request using the HTTP/2 format, the HTTP/3 format, or the FastCGI format.

Furthermore, every format can be transported over different low-level transport, such as TCP, Unix-Domain sockets, QUIC or memory.
Supports for Unix-Domain sockets requires Java 16 or later, since Unix-Domain sockets support has been introduced in OpenJDK with https://openjdk.java.net/jeps/380[JEP 380].

The <<transport-fcgi,FastCGI format>> is used in Jetty's xref:server/fastcgi.adoc[FastCGI support] that allows Jetty to work as a reverse proxy to PHP (exactly like Apache or Nginx do) and therefore be able to serve, for example, WordPress websites, often in conjunction with Unix-Domain sockets (although it is possible to use FastCGI via network too).

The HTTP/2 format allows Jetty's HTTP client to perform requests using HTTP/2 to HTTP/2 enabled websites, see also Jetty's xref:client/http2.adoc[HTTP/2 support].

The HTTP/3 format allows Jetty's HTTP client to perform requests using HTTP/3 to HTTP/3 enabled websites, see also Jetty's xref:client/http3.adoc[HTTP/3 support].

Out of the box features that you get with the Jetty HTTP client include:

* Redirect support -- redirect codes such as 302 or 303 are automatically followed.
* Cookies support -- cookies sent by servers are stored and sent back to servers in matching requests.
* Authentication support -- HTTP "Basic", "Digest" and "SPNEGO" authentications are supported, others are pluggable.
* Forward proxy support -- HTTP proxying, SOCKS4 and SOCKS5 proxying.
* Response content decoding -- response content compressed by the server is transparently decompressed.

[[start]]
== Starting HttpClient

The Jetty artifact that provides the main HTTP client implementation is `jetty-client`.
The Maven artifact coordinates are the following:

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-client</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

The main class is named `org.eclipse.jetty.client.HttpClient`.

You can think of a `HttpClient` instance as a browser instance.
Like a browser it can make requests to different domains, it manages redirects, cookies and authentication, you can configure it with a forward proxy, and it provides you with the responses to the requests you make.

In order to use `HttpClient`, you must instantiate it, configure it, and then start it:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=start]
----

You may create multiple instances of `HttpClient`, but typically one instance is enough for an application.
There are several reasons for having multiple `HttpClient` instances including, but not limited to:

* You want to specify different configuration parameters (for example, one instance is configured with a forward proxy while another is not).
* You want the two instances to behave like two different browsers and hence have different cookies, different authentication credentials, etc.
* You want to use <<transport,different ``HttpClientTransport``s>>.

Like browsers, HTTPS requests are supported out-of-the-box (see <<configuration-tls,this section>> for the TLS configuration), as long as the server provides a valid certificate.
In case the server does not provide a valid certificate (or in case it is self-signed) you want to customize ``HttpClient``'s TLS configuration as described in <<configuration-tls,this section>>.

[[stop]]
== Stopping HttpClient

It is recommended that when your application stops, you also stop the `HttpClient` instance (or instances) that you are using.

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=stop]
----

Stopping `HttpClient` makes sure that the memory it holds (for example, `ByteBuffer` pools, authentication credentials, cookies, etc.) is released, and that the thread pool and scheduler are properly stopped allowing all threads used by `HttpClient` to exit.

[NOTE]
====
You cannot call `HttpClient.stop()` from one of its own threads, as it would cause a deadlock.
It is recommended that you stop `HttpClient` from an unrelated thread, or from a newly allocated thread, for example:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=stopFromOtherThread]
----
====

[[arch]]
== HttpClient Architecture

A `HttpClient` instance can be thought as a browser instance, and it manages the following components:

* A `CookieStore` (see <<cookie,this section>>).
* A `AuthenticationStore` (see <<authentication,this section>>).
* A `ProxyConfiguration` (see <<proxy,this section>>).
* A set of ``ContentDecoder.Factory``s (see <<content-response,this section>>).
* A set of ``Destination``s, where each `Destination` manages a <<connection-pool,pool of connections>>.

A `Destination` is the client-side component that represents an _origin_ server, and manages a queue of requests for that origin, and a <<connection-pool,pool of connections>> to that origin.

An _origin_ may be simply thought as the tuple `(scheme, host, port)` and it is where the client connects to in order to communicate with the server.
However, this is not enough.

If you use `HttpClient` to write a proxy you may have different clients that want to contact the same server.
In this case, you may not want to use the same proxy-to-server connection to proxy requests for both clients, for example for authentication reasons: the server may associate the connection with authentication credentials, and you do not want to use the same connection for two different users that have different credentials.
Instead, you want to use different connections for different clients and this can be achieved by "tagging" a destination with a tag object that represents the remote client (for example, it could be the remote client IP address).

Two origins with the same `(scheme, host, port)` but different `tag` create two different destinations and therefore two different connection pools.
However, also this is not enough.

It is possible for a server to speak different protocols on the same `port`.
A connection may start by speaking one protocol, for example HTTP/1.1, but then be upgraded to speak a different protocol, for example HTTP/2. After a connection has been upgraded to a second protocol, it cannot speak the first protocol anymore, so it can only be used to communicate using the second protocol.

Two origins with the same `(scheme, host, port, tag)` but different `protocol` create two different destinations and therefore two different connection pools.

Finally, it is possible for a server to speak the same protocol over different xref:client/io-arch.adoc#transport[low-level transports] (represented by `Transport`), for example TCP and Unix-Domain.

Two origins with the same `(scheme, host, port, tag, protocol)` but different low-level transports create two different destinations and therefore two different connection pools.

Therefore, an origin is identified by the tuple `(scheme, host, port, tag, protocol, transport)`.

[[connection-pool]]
== HttpClient Connection Pooling

A `Destination` manages a `org.eclipse.jetty.client.ConnectionPool`, where connections to a particular origin are pooled for performance reasons: opening a connection is a costly operation, and it's better to reuse them for multiple requests.

NOTE: Remember that to select a specific `Destination` you must select a specific origin, and that an origin is identified by the tuple `(scheme, host, port, tag, protocol, transport)`, so you can have multiple ``Destination``s for the same `host` and `port`, and therefore multiple ``ConnectionPool``s

You can access the `ConnectionPool` in this way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=getConnectionPool]
----

Jetty's client library provides the following `ConnectionPool` implementations:

* `DuplexConnectionPool`, historically the first implementation, only used by the HTTP/1.1 transport.
* `MultiplexConnectionPool`, the generic implementation valid for any transport where connections are reused with a most recently used algorithm (that is, the connections most recently returned to the connection pool are the more likely to be used again).
* `RoundRobinConnectionPool`, similar to `MultiplexConnectionPool` but where connections are reused with a round-robin algorithm.
* `RandomConnectionPool`, similar to `MultiplexConnectionPool` but where connections are reused with an algorithm that chooses them randomly.

The `ConnectionPool` implementation can be customized for each destination in by setting a `ConnectionPool.Factory` on the `HttpClientTransport`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=setConnectionPool]
----

[[connection-pool-precreate-connections]]
=== Pre-Creating Connections

`ConnectionPool` offers the ability to pre-create connections by calling `ConnectionPool.preCreateConnections(int)`.

Pre-creating the connections saves the time and processing spent to establish the TCP connection, performing the TLS handshake (if necessary) and, for HTTP/2 and HTTP/3, perform the initial protocol setup.
This is particularly important for HTTP/2 because in the initial protocol setup the server informs the client of the maximum number of concurrent requests per connection (otherwise assumed to be just `1` by the client).

The scenarios where pre-creating connections is useful are, for example:

* Load testing, where you want to prepare the system with connections already created to avoid paying of cost of connection setup.
* Proxying scenarios, often in conjunction with the use of `RoundRobinConnectionPool` or `RandomConnectionPool`, where the proxy creates early the connections to the backend servers.

This is an example of how to pre-create connections:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=preCreateConnections]
----

[NOTE]
====
Pre-creating connections for secure HTTP/1.1 requires you to call `HttpClientTransportOverHTTP.setInitializeConnections(true)`, otherwise only the TCP connection is established, but the TLS handshake is not initiated.

To initialize connections for secure HTTP/1.1, the client sends an initial `OPTIONS * HTTP/1.1` request to the server.
The server must be able to handle this request without closing the connection (in particular it must not add the `Connection: close` header in the response).
====

[[request-processing]]
== HttpClient Request Processing

[plantuml]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false

participant Application
participant Request
participant HttpClient
participant Destination
participant ConnectionPool
participant Connection

Application -> HttpClient : newRequest()
HttpClient -> Request **
Application -> Request : send()
Request -> HttpClient : send()
HttpClient -> Destination ** : get or create
Destination -> ConnectionPool ** : create
HttpClient -> Destination : send(Request)
Destination -> Destination : enqueue(Request)
Destination -> ConnectionPool : acquire()
ConnectionPool -> Connection ** : create
Destination -> Destination : dequeue(Request)
Destination -> Connection : send(Request)
----

When a request is sent, an origin is computed from the request; `HttpClient` uses that origin to find (or create if it does not exist) the correspondent destination.
The request is then queued onto the destination, and this causes the destination to ask its connection pool for a free connection.
If a connection is available, it is returned, otherwise a new connection is created.
Once the destination has obtained the connection, it dequeues the request and sends it over the connection.

The first request to a destination triggers the opening of the first connection.
A second request with the same origin sent _after_ the first request/response cycle is completed may reuse the same connection, depending on the connection pool implementation.
A second request with the same origin sent _concurrently_ with the first request will likely cause the opening of a second connection, depending on the connection pool implementation.
The configuration parameter `HttpClient.maxConnectionsPerDestination` (see also the <<configuration,configuration section>>) controls the max number of connections that can be opened for a destination.

[NOTE]
====
If opening connections to a given origin takes a long time, then requests for that origin will queue up in the corresponding destination until the connections are established.

To save the time spent opening connections, you can xref:connection-pool-precreate-connections[pre-create connections].
====

Each connection can handle a limited number of concurrent requests.
For HTTP/1.1, this number is always `1`: there can only be one outstanding request for each connection.
For HTTP/2 this number is determined by the server `max_concurrent_stream` setting (typically around `100`, i.e. there can be up to `100` outstanding requests for every connection).

When a destination has maxed out its number of connections, and all connections have maxed out their number of outstanding requests, more requests sent to that destination will be queued.
When the request queue is full, the request will be failed.
The configuration parameter `HttpClient.maxRequestsQueuedPerDestination` (see also the <<configuration,configuration section>>) controls the max number of requests that can be queued for a destination.

[[api]]
== HttpClient API Usage

`HttpClient` provides two types of APIs: a blocking API and a non-blocking API.

[[blocking]]
=== HttpClient Blocking APIs

The simpler way to perform a HTTP request is the following:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=simpleBlockingGet]
----

The method `HttpClient.GET(\...)` performs a HTTP `GET` request to the given URI and returns a `ContentResponse` when the request/response conversation completes successfully.

The `ContentResponse` object contains the HTTP response information: status code, headers and possibly content.
The content length is limited by default to 2 MiB; for larger content see <<content-response,the section on response content handling>>.

If you want to customize the request, for example by issuing a `HEAD` request instead of a `GET`, and simulating a browser user agent, you can do it in this way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=headFluent]
----

This is a shorthand for:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=headNonFluent]
----

You first create a request object using `httpClient.newRequest(\...)`, and then you customize it using the fluent API style (that is, a chained invocation of methods on the request object).
When the request object is customized, you call `request.send()` that produces the `ContentResponse` when the request/response conversation is complete.

IMPORTANT: The `Request` object, despite being mutable, cannot be reused for other requests.
This is true also when trying to send two or more identical requests: you have to create two or more `Request` objects.

Simple `POST` requests also have a shortcut method:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=postFluent]
----

The `POST` parameter values added via the `param()` method are automatically URL-encoded.

Jetty's `HttpClient` automatically follows redirects, so it handles the typical web pattern http://en.wikipedia.org/wiki/Post/Redirect/Get[POST/Redirect/GET], and the response object contains the content of the response of the `GET` request.
Following redirects is a feature that you can enable/disable on a per-request basis or globally.

File uploads also require one line, and make use of `java.nio.file` classes:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=fileFluent]
----

It is possible to impose a total timeout for the request/response conversation using the `Request.timeout(\...)` method as follows:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=totalTimeout]
----

In the example above, when the 5 seconds expire, the request/response cycle is aborted and a `java.util.concurrent.TimeoutException` is thrown.

[[non-blocking]]
=== HttpClient Non-Blocking APIs

So far we have shown how to use Jetty HTTP client in a blocking style -- that is, the thread that issues the request blocks until the request/response conversation is complete.

This section will look at Jetty's `HttpClient` non-blocking, asynchronous APIs that are perfectly suited for large content downloads, for parallel processing of requests/responses and in cases where performance and efficient thread and resource utilization is a key factor.

The asynchronous APIs rely heavily on listeners that are invoked at various stages of request and response processing.
These listeners are implemented by applications and may perform any kind of logic.
The implementation invokes these listeners in the same thread that is used to process the request or response.
Therefore, if the application code in these listeners takes a long time to execute, the request or response processing is delayed until the listener returns.

If you need to execute application code that takes long time inside a listener, it is typically better to spawn your own thread to execute the code that takes long time.
In this way you return from the listener as soon as possible and allow the implementation to resume the processing of the request or response (or of other requests/responses).

Request and response processing are executed by two different threads and therefore may happen concurrently.
A typical example of this concurrent processing is an echo server, where a large upload may be concurrent with the large download echoed back.

NOTE: Remember that responses may be processed and completed _before_ requests; a typical example is a large upload that triggers a quick response, for example an error, by the server: the response may arrive and be completed while the request content is still being uploaded.

The application thread that calls `Request.send(Response.CompleteListener)` performs the <<request-processing,processing of the request>> until either the request is fully sent over the network or until it would block on I/O, then it returns (and therefore never blocks).
If it would block on I/O, the thread asks the I/O system to emit an event when the I/O will be ready to continue, then returns.
When such an event is fired, a thread taken from the `HttpClient` thread pool will resume the processing of the request.

Response are processed from the I/O thread taken from the `HttpClient` thread pool that processes the event that bytes are ready to be read.
Response processing continues until either the response is fully processed or until it would block for I/O.
If it would block for I/O, the thread asks the I/O system to emit an event when the I/O will be ready to continue, then returns.
When such an event is fired, a (possibly different) thread taken from the `HttpClient` thread pool will resume the processing of the response.

When the request and the response are both fully processed, the thread that finished the last processing (usually the thread that processes the response, but may also be the thread that processes the request -- if the request takes more time than the response to be processed) is used to dequeue the next request for the same destination and to process it.

A simple non-blocking `GET` request that discards the response content can be written in this way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=simpleNonBlocking]
----

Method `Request.send(Response.CompleteListener)` returns `void` and does not block; the `Response.CompleteListener` lambda provided as a parameter is notified when the request/response conversation is complete, and the `Result` parameter allows you to access the request and response objects as well as failures, if any.

You can impose a total timeout for the request/response conversation in the same way used by the synchronous API:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=nonBlockingTotalTimeout]
----

The example above will impose a total timeout of 3 seconds on the request/response conversation.

[[listeners]]
==== Request and Response Listeners

The HTTP client APIs use listeners extensively to provide hooks for all possible request and response events:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=listeners]
----

This makes Jetty HTTP client suitable for HTTP load testing because, for example, you can accurately time every step of the request/response conversation (thus knowing where the request/response time is really spent).

[IMPORTANT]
====
The code in request and response listeners __should not__ block.

It is allowed to call other blocking APIs, such as the Java file-system APIs.
You should not call blocking APIs that:

* Wait for other request or response events, such as receiving other request or response content chunks.
* Use wait/notify primitives such as those available in `java.lang.Object` or `java.util.concurrent.locks.Condition`.

If the listener code blocks, the implementation also will be blocked and will not be able to advance the processing of the request or response that the listener code is likely waiting for, causing a deadlock.
====

Have a look at the link:{javadoc-url}/org/eclipse/jetty/client/api/Request.Listener.html[`Request.Listener`] class to know about request events, and to the link:{javadoc-url}/org/eclipse/jetty/client/api/Response.Listener.html[`Response.Listener`] class to know about response events.

[[content-request]]
=== Request Content Handling

Jetty's `HttpClient` provides a number of utility classes off the shelf to handle request content.

You can provide request content as `String`, `byte[]`, `ByteBuffer`, `java.nio.file.Path`, `InputStream`, and provide your own implementation of `org.eclipse.jetty.client.Request.Content`.
Here’s an example that provides the request content using `java.nio.file.Paths`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=pathRequestContent]
----

Alternatively, you can use `FileInputStream` via the `InputStreamRequestContent` utility class:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=inputStreamRequestContent]
----

Since `InputStream` is blocking, then also the send of the request will block if the input stream blocks, even in case of usage of the non-blocking `HttpClient` APIs.

If you have already read the content in memory, you can pass it as a `byte[]` (or a `String`) using the `BytesRequestContent` (or `StringRequestContent`) utility class:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=bytesStringRequestContent]
----

If the request content is not immediately available, but your application will be notified of the content to send, you can use `AsyncRequestContent` in this way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=asyncRequestContent]
----

While the request content is awaited and consequently uploaded by the client application, the server may be able to respond (at least with the response headers) completely asynchronously.
In this case, `Response.Listener` callbacks will be invoked before the request is fully sent.
This allows fine-grained control of the request/response conversation: for example the server may reject contents that are too big, send a response to the client, which in turn may stop the content upload.

Another way to provide request content is by using an `OutputStreamRequestContent`, which allows applications to write request content when it is available to the `OutputStream` provided by `OutputStreamRequestContent`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=outputStreamRequestContent]
----

[[content-response]]
=== Response Content Handling

Response content may be _encoded_ by the server, and this is signaled by the presence of the `Content-Encoding` response header.

Typically, encoding the response content means that it is compressed by the server, to save bytes transmitted over the network.
The most common encodings are:

* `gzip`, using the link:https://en.wikipedia.org/wiki/Gzip[gzip] algorithm.
* `br`, using the link:https://github.com/google/brotli[brotli] algorithm.
* `zstd`, using the link:https://github.com/facebook/zstd[zstdandard] algorithm.

Jetty's `HttpClient` supports by default the `gzip` encoding, but the support is pluggable and discovered via Java's `ServiceLoader` mechanism.

In order to add support for gzip, brotli and zstandard, it is enough to add to your classpath the correspondent Jetty artifacts with the implementation, respectively:

* `jetty-compression-gzip`
* `jetty-compression-brotli`
* `jetty-compression-zstandard`

The Maven artifact coordinates are the following:

[,xml,subs=attributes+]
----
<dependencies>
  <dependency>
    <groupId>org.eclipse.jetty.compression</groupId>
    <artifactId>jetty-compression-gzip</artifactId>
    <version>{jetty-version}</version>
  </dependency>
  <dependency>
    <groupId>org.eclipse.jetty.compression</groupId>
    <artifactId>jetty-compression-brotli</artifactId>
    <version>{jetty-version}</version>
  </dependency>
  <dependency>
    <groupId>org.eclipse.jetty.compression</groupId>
    <artifactId>jetty-compression-zstandard</artifactId>
    <version>{jetty-version}</version>
  </dependency>
</dependencies>
----

[NOTE]
====
The Jetty artifacts `jetty-compression-brotli` and `jetty-compression-zstandard` access
respectively the Brotli and Zstandard implementations, that are written in native code.

Since Java 22, the JVM emits a warning message when Java code tries to access code in native libraries.
For example with Java 25:

----
WARNING: A restricted method in java.lang.System has been called
WARNING: java.lang.System::loadLibrary has been called by com.aayushatharva.brotli4j.Brotli4jLoader in an unnamed module (file:/home/simon/.m2/repository/com/aayushatharva/brotli4j/brotli4j/1.17.0/brotli4j-1.17.0.jar)
WARNING: Use --enable-native-access=ALL-UNNAMED to avoid a warning for callers in this module
WARNING: Restricted methods will be blocked in a future release unless native access is enabled
----

To avoid the warning, you can start the JVM as suggested by the warning message above, by adding `--enable-native-access=ALL-UNNAMED` on the command line.
This grants any code in the class-path access to any native library.

If you are running in JPMS mode, you can explicitly grant access just to the JPMS module(s) that require it.
In the case above, you need `--enable-native-access=com.aayushatharva.brotli4j`.
====

If you want to remove support for automatic response content decoding (for example in proxies), use the following code:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=removeDecoders]
----

You can also provide your own implementation by implementing `ContentDecoder.Factory`.

If you want to customize the implementations mentioned previously, you can do it in this way, using the utility class `CompressionContentDecoderFactory` from the `org.eclipse.jetty.compression:jetty-compression-client` Maven artifact:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=customDecoders]
----

Jetty's `HttpClient` allows applications to handle response content in different ways.

You can buffer the response content in memory; this is done when using the <<blocking,blocking APIs>> and the content is buffered within a `ContentResponse` up to 2 MiB.

If you want to control the length of the response content (for example limiting to values smaller than the default of 2 MiB), then you can use a `org.eclipse.jetty.client.CompletableResponseListener` in this way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=completableResponseListener]
----

If the response content length is exceeded, the response will be aborted, and an exception will be thrown by method `get(\...)`.

You can accumulate the response content in memory also using the <<non-blocking,non-blocking APIs>>, via the `RetainingResponseListener` utility class:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=retainingResponseListener]
----

If you want to avoid accumulation, you can wait for the response and then stream the content using the `InputStreamResponseListener` utility class:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=inputStreamResponseListener]
----

If you want to save the response content to a file, you can use the `PathResponseListener` utility class:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=pathResponseListener]
----

Finally, let's look at the advanced usage of the response content handling.

The response content is provided by the `HttpClient` implementation to application listeners following the read/demand model of `org.eclipse.jetty.io.Content.Source`.

The listener that follows this model is `Response.ContentSourceListener`.

After the response headers have been processed by the `HttpClient` implementation, `Response.ContentSourceListener.onContentSource(response, contentSource)` is invoked once and only once.
This allows the application to control precisely the read/demand loop: when to read a chunk, how to process it and when to demand the next one.

You must provide a `ContentSourceListener` whose implementation reads a `Content.Chunk` from the provided `Content.Source`, as explained in xref:arch/io.adoc#content-source[this section].

The invocation of `onContentSource(Request, Content.Source)` and of the demand callback passed to `contentSource.demand(Runnable)` are serialized with respect to asynchronous events such as timeouts or an asynchronous call to `Request.abort(Throwable)`.
This means that these asynchronous events are not processed until the invocation of `onContentSource(Request, Content.Source)` returns, or until the invocation of the demand callback returns.
With this model, applications should not worry too much about concurrent asynchronous events happening during response content handling, because they will eventually see the events as failures while reading the response content.

Demanding for content and consuming the content are orthogonal activities.

An application can read, store aside the `Content.Chunk` objects without releasing them (to consume them later), and demand for more chunks, but it must call `Chunk.retain()` on the stored chunks, and arrange to release them after they have been consumed later.

If not done carefully, this may lead to excessive memory consumption, since the `ByteBuffer` bytes are not consumed.
Releasing the ``Content.Chunk``s will result in the ``ByteBuffer``s to be disposed/recycled and may be performed at any time.

An application can also read one chunk of content, consume it, release it, and then _not_ demand for more content until a later time.

Subclass `Response.AsyncContentListener` overrides the behavior of `Response.ContentSourceListener`; when an application implements `AsyncContentListener.onContent(response, chunk, demander)`, it can control the disposing/recycling of the `ByteBuffer` by releasing the chunk _and_ it can control when to demand one more chunk by calling `demander.run()`.

Subclass `Response.ContentListener` overrides the behavior of `Response.AsyncContentListener`; when an application implementing its `onContent(response, buffer)` returns from the method itself, it will _both_ the effect of disposing/recycling the `buffer` _and_ the effect of demanding one more chunk of content.

An application that implements a forwarder between two servers can be implemented efficiently by handling the response content without copying the `ByteBuffer` bytes as in the following example:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=forwardContent]
----

[[api-transport]]
=== Request `Transport`

The communication between client and server happens over a xref:client/io-arch.adoc#transport[low-level transport], and applications can specify the low-level transport to use for each request.

This gives client applications great flexibility, because they can use the same `HttpClient` instance to communicate, for example, with an external third party web application via TCP, to a different process via Unix-Domain sockets, and efficiently to the same process via memory.

Client application can also choose more esoteric configurations such as using QUIC, typically used to transport HTTP/3, to transport HTTP/1.1 or HTTP/2, because QUIC provides reliable and ordered communication like TCP does.

Provided you have configured a xref:server/http.adoc#connector[`UnixDomainServerConnector`] on the server, this is how you can configure a request to use Unix-Domain sockets:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=unixDomain]
----

In the same way, if you have configured a xref:server/http.adoc#connector[`MemoryConnector`] on the server, this is how you can configure a request to use memory for communication:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=memory]
----

This is a fancy example of how to mix HTTP versions and low-level transports:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=mixedTransports]
----

[[connection-information]]
=== Connection Information

In order to send a request, it is necessary to obtain a connection, as explained in the xref:request-processing[request processing section].

The HTTP/1.1 protocol may send only one request at a time on a single connection, while multiplexed protocols such as HTTP/2 may send many requests at a time on a single connection.

You can access the connection information, for example the local and remote `SocketAddress`, or the `SslSessionData` if the connection is secured, in the following way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=connectionInformation]
----

[NOTE]
====
The connection information is only available when the request is associated with a connection.

This means that the connection is not available in the _request queued_ event, but only starting from the _request begin_ event.
For more information about request events, see xref:non-blocking[this section].
====

[[connection-events]]
=== Connection Events

In order to send HTTP requests, a connection is necessary, as explained in the xref:request-processing[request processing section].

HTTP/1.1 and HTTP/2 use `SocketChannel.connect(SocketAddress)` to establish a connection with the server, either via the TCP transport or via the Unix-Domain transport.

You can listen to these `connect` events using a `ClientConnector.ConnectListener`, for example to record connection establishment times:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=connectListener]
----

This could be particularly useful when you notice that your client application seem "slow" to send requests.
The `connect begin` and `connect success` events, along with the `request queued` and `request begin` event (detailed xref:non-blocking[here]), allow you to understand whether it is the server being slow at accepting connections, or it is the client being slow at processing queued requests.

Once the low-level connection has been established, you can be notified of connection events using a `Connection.Listener`, or more concretely `ConnectionStatistics`, as described in xref:arch/io.adoc#connection-listener[this section].

[[configuration]]
== HttpClient Configuration

`HttpClient` has a quite large number of configuration parameters.
Please refer to the `HttpClient` link:{javadoc-url}/org/eclipse/jetty/client/HttpClient.html[javadocs] for the complete list of configurable parameters.

The most common parameters are:

* `HttpClient.idleTimeout`: same as `ClientConnector.idleTimeout` described in xref:client/io-arch.adoc#transport[this section].
* `HttpClient.connectBlocking`: same as `ClientConnector.connectBlocking` described in xref:client/io-arch.adoc#transport[this section].
* `HttpClient.connectTimeout`: same as `ClientConnector.connectTimeout` described in xref:client/io-arch.adoc#transport[this section].
* `HttpClient.maxConnectionsPerDestination`: the max number of TCP connections that are opened for a particular destination (defaults to 64).
* `HttpClient.maxRequestsQueuedPerDestination`: the max number of requests queued (defaults to 1024).

[[configuration-tls]]
=== HttpClient TLS Configuration

`HttpClient` supports HTTPS requests out-of-the-box like a browser does.

The support for HTTPS request is provided by a `SslContextFactory.Client` instance, typically configured in the `ClientConnector`.
If not explicitly configured, the `ClientConnector` will allocate a default one when started.

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=tlsExplicit]
----

The default `SslContextFactory.Client` verifies the certificate sent by the server by verifying the validity of the certificate with respect to the certificate chain, the expiration date, the server host name, etc.
This means that requests to public websites that have a valid certificate (such as `+https://google.com+`) will work out-of-the-box, without the need to specify a KeyStore or a TrustStore.

However, requests made to sites that return an invalid or a self-signed certificate will fail (like they will in a browser).
An invalid certificate may be expired or have the wrong server host name; a self-signed certificate has a certificate chain that cannot be verified.

The validation of the server host name present in the certificate is important, to guarantee that the client is connected indeed with the intended server.

The validation of the server host name is performed at two levels: at the TLS level (in the JDK) and, optionally, at the application level.

By default, the validation of the server host name at the TLS level is enabled, while it is disabled at the application level.

You can configure the `SslContextFactory.Client` to skip the validation of the server host name at the TLS level:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=tlsNoValidation]
----

When you disable the validation of the server host name at the TLS level, you are strongly recommended to enable it at the application level.
Failing to do so puts you at risk of connecting to a server different from the one you intend to connect to:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=tlsAppValidation]
----

Enabling server host name validation at both the TLS level and application level allow you to further restrict the set of server hosts the client can connect to, among those allowed in the certificate sent by the server.

Entirely disabling server host name validation is not recommended, but may be done in controlled environments.

Even with server host name validation disabled, the validation of the certificate chain, by validating cryptographic signatures and validity dates is still performed.

Please refer to the `SslContextFactory.Client` link:{javadoc-url}/org/eclipse/jetty/util/ssl/SslContextFactory.Client.html[javadocs] for the complete list of configurable parameters.

[[configuration-tls-listener]]
==== HttpClient `SslHandshakeListener`

Applications may register a `org.eclipse.jetty.io.ssl.SslHandshakeListener` to be notified of TLS handshakes success or failure, by adding the `SslHandshakeListener` as a bean to `HttpClient`:

[,java,indent=0,options=nowrap]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=sslHandshakeListener]
----

[[configuration-tls-truststore]]
==== HttpClient TLS TrustStore Configuration
TODO

[[configuration-tls-client-certs]]
==== HttpClient TLS Client Certificates Configuration
TODO

[[cookie]]
== HttpClient Cookie Support

Jetty's `HttpClient` supports cookies out of the box.

The `HttpClient` instance receives cookies from HTTP responses and stores them in a `java.net.CookieStore`, a class that is part of the JDK.
When new requests are made, the cookie store is consulted and if there are matching cookies (that is, cookies that are not expired and that match domain and path of the request) then they are added to the requests.

Applications can programmatically access the cookie store to find the cookies that have been set:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=getCookies]
----

Applications can also programmatically set cookies as if they were returned from a HTTP response:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=setCookie]
----

Cookies may be added explicitly only for a particular request:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=requestCookie]
----

You can remove cookies that you do not want to be sent in future HTTP requests:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=removeCookie]
----

If you want to totally disable cookie handling, you can install a `HttpCookieStore.Empty`.
This must be done when `HttpClient` is used in a proxy application, in this way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=emptyCookieStore]
----

You can enable cookie filtering by installing a cookie store that performs the filtering logic in this way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=filteringCookieStore]
----

The example above will retain only cookies that come from the `google.com` domain or sub-domains.

=== Cookie Name Prefixes

Jetty's `HttpClient` validates cookie name prefixes as defined in https://datatracker.ietf.org/doc/draft-ietf-httpbis-rfc6265bis/[RFC 6265bis].

Cookies with the `+__Secure-+` prefix are only accepted if:

* The cookie has the `Secure` attribute set.
* The cookie was received over a secure (HTTPS) connection.

Cookies with the `+__Host-+` prefix are only accepted if:

* The cookie has the `Secure` attribute set.
* The cookie was received over a secure (HTTPS) connection.
* The cookie does not have a `Domain` attribute (making it a host-only cookie).
* The cookie has `Path=/` explicitly set.

These prefixes allow servers to "lock down" cookies with additional security guarantees.
Cookies that violate these requirements are silently discarded by the `HttpCookieStore`.

// TODO: move this section to server-side
=== Special Characters in Cookies

Jetty is compliant with https://tools.ietf.org/html/rfc6265[RFC6265], and as such care must be taken when setting a cookie value that includes special characters such as `;`.

Previously, `Version=1` cookies defined in https://tools.ietf.org/html/rfc2109[RFC2109] (and continued in https://tools.ietf.org/html/rfc2965[RFC2965]) allowed for special/reserved characters to be enclosed within double quotes when declared in a `Set-Cookie` response header:

[,screen]
----
Set-Cookie: foo="bar;baz";Version=1;Path="/secure"
----

This was added to the HTTP Response as follows:

[,java]
----
protected void service(HttpServletRequest request, HttpServletResponse response)
{
    jakarta.servlet.http.Cookie cookie = new Cookie("foo", "bar;baz");
    cookie.setPath("/secure");
    response.addCookie(cookie);
}
----

The introduction of RFC6265 has rendered this approach no longer possible; users are now required to encode cookie values that use these special characters.
This can be done utilizing `jakarta.servlet.http.Cookie` as follows:

[,java]
----
jakarta.servlet.http.Cookie cookie = new Cookie("foo", URLEncoder.encode("bar;baz", "UTF-8"));
----

Jetty validates all cookie names and values being added to the `HttpServletResponse` via the `addCookie(Cookie)` method.
If an illegal value is discovered Jetty will throw an `IllegalArgumentException` with the details.

[[redirect-cache]]
== Redirect Caching

By default, `HttpClient` follows redirects but does not cache them.
This means that repeated requests to a URL that returns a redirect will each incur the redirect round-trip.

You can enable redirect caching to avoid the redirect round-trip:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=redirectCache]
----

`RedirectCache.Default` caches _permanent_ redirects (status codes 301 and 308).
Subsequent requests with the same HTTP method and the same URI result in requests using the redirect HTTP method and the redirect HTTP URI.

NOTE: `POST` requests replied with `301` will have the redirect method changed to `GET`.
Requests replied with `308` preserve the original HTTP method.

The `RedirectCache.Default` implementation accepts a maximum size parameter to limit memory usage.
When the limit is reached, least recently used entries are evicted.

[[authentication]]
== HttpClient Authentication Support

Jetty's `HttpClient` supports the `BASIC` and `DIGEST` authentication mechanisms defined by https://tools.ietf.org/html/rfc7235[RFC 7235], as well as the SPNEGO authentication mechanism defined in https://tools.ietf.org/html/rfc4559[RFC 4559].

The HTTP _conversation_, the sequence of related HTTP requests, for a request that needs authentication is the following:

[plantuml]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false

participant Application
participant HttpClient
participant Server

Application -> Server : GET /path
Server -> HttpClient : 401 + WWW-Authenticate
HttpClient -> Server : GET + Authentication
Server -> Application : 200 OK
----

Upon receiving a HTTP 401 response code, `HttpClient` looks at the `WWW-Authenticate` response header (the server _challenge_) and then tries to match configured authentication credentials to produce an `Authentication` header that contains the authentication credentials to access the resource.

You can configure authentication credentials in the `HttpClient` instance as follows:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=addAuthentication]
----

``Authentication``s are matched against the server challenge first by mechanism (e.g. `BASIC` or `DIGEST`), then by realm and then by URI.

If an `Authentication` match is found, the application does not receive events related to the HTTP 401 response.
These events are handled internally by `HttpClient` which produces another (internal) request similar to the original request but with an additional `Authorization` header.

If the authentication is successful, the server responds with a HTTP 200 and `HttpClient` caches the `Authentication.Result` so that subsequent requests for a matching URI will not incur in the additional rountrip caused by the HTTP 401 response.

It is possible to clear ``Authentication.Result``s in order to force authentication again:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=clearResults]
----

Authentication results may be preempted to avoid the additional roundtrip due to the server challenge in this way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=preemptedResult]
----

In this way, requests for the given URI are enriched immediately with the `Authorization` header, and the server should respond with HTTP 200 (and the resource content) rather than with the 401 and the challenge.

It is also possible to preempt the authentication for a single request only, in this way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=requestPreemptedResult]
----

See also the <<proxy-authentication,proxy authentication section>> for further information about how authentication works with HTTP proxies.

[[authentication-spnego]]
=== HttpClient SPNEGO Authentication Support
TODO

[[proxy]]
== HttpClient Proxy Support

Jetty's `HttpClient` can be configured to use proxies to connect to destinations.

These types of proxies are available out of the box:

* HTTP proxy (provided by class `org.eclipse.jetty.client.HttpProxy`)
* SOCKS 4 proxy (provided by class `org.eclipse.jetty.client.Socks4Proxy`)
* <<proxy-socks5,SOCKS 5 proxy>> (provided by class `org.eclipse.jetty.client.Socks5Proxy`)

Other implementations may be written by subclassing `ProxyConfiguration.Proxy`.

The following is a typical configuration:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=proxy]
----

You specify the proxy host and proxy port, and optionally also the addresses that you do not want to be proxied, and then add the proxy configuration on the `ProxyConfiguration` instance.

Configured in this way, `HttpClient` makes requests to the HTTP proxy (for plain-text HTTP requests) or establishes a tunnel via HTTP `CONNECT` (for encrypted HTTPS requests).

Proxying is supported for any version of the HTTP protocol.

The communication between the client and the proxy may be encrypted, so that it would not be possible for another party on the same network as the client to know what servers the client connects to.

[[proxy-socks5]]
=== SOCKS5 Proxy Support

SOCKS 5 (defined in https://datatracker.ietf.org/doc/html/rfc1928[RFC 1928]) offers choices for authentication methods and supports IPv6 (things that SOCKS 4 does not support).

A typical SOCKS 5 proxy configuration with the username/password authentication method is the following:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=proxySocks5]
----

[[proxy-authentication]]
=== HTTP Proxy Authentication Support

Jetty's `HttpClient` supports HTTP proxy authentication in the same way it supports <<authentication,server authentication>>.

In the example below, the HTTP proxy requires `BASIC` authentication, but the server requires `DIGEST` authentication, and therefore:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=proxyAuthentication]
----

The HTTP conversation for successful authentications on both the proxy and the server is the following:

[plantuml]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false

participant Application
participant HttpClient
participant Proxy
participant Server

Application -> Proxy : GET /path
Proxy -> HttpClient : 407 + Proxy-Authenticate
HttpClient -> Proxy : GET /path + Proxy-Authorization
Proxy -> Server : GET /path
Server -> Proxy : 401 + WWW-Authenticate
Proxy -> HttpClient : 401 + WWW-Authenticate
HttpClient -> Proxy : GET /path + Proxy-Authorization + Authorization
Proxy -> Server : GET /path + Authorization
Server -> Proxy : 200 OK
Proxy -> HttpClient : 200 OK
HttpClient -> Application : 200 OK
----

The application does not receive events related to the responses with code 407 and 401 since they are handled internally by `HttpClient`.

Similarly to the <<authentication,authentication section>>, the proxy authentication result and the server authentication result can be preempted to avoid, respectively, the 407 and 401 roundtrips.

[[transport]]
== HttpClient Pluggable Transports

Jetty's `HttpClient` can be configured to use different HTTP formats to carry the semantic of HTTP requests and responses, by specifying different `HttpClientTransport` implementations.

This means that the intention of a client to request resource `/index.html` using the `GET` method can be carried over a <<api-transport,low-level transport>> in different formats.

An `HttpClientTransport` is the component that is in charge of converting a high-level, semantic, HTTP requests such as " ``GET`` resource ``/index.html`` " into the specific format understood by the server (for example, HTTP/2 or HTTP/3), and to convert the server response from the specific format (HTTP/2 or HTTP/3) into high-level, semantic objects that can be used by applications.

The most common protocol format is HTTP/1.1, a textual protocol with lines separated by `\r\n`:

[,screen]
----
GET /index.html HTTP/1.1\r\n
Host: domain.com\r\n
...
\r\n
----

However, the same request can be made using FastCGI, a binary protocol:

[,screen]
----
x01 x01 x00 x01 x00 x08 x00 x00
x00 x01 x01 x00 x00 x00 x00 x00
x01 x04 x00 x01 xLL xLL x00 x00
x0C x0B  D   O   C   U   M   E
 N   T   _   U   R   I   /   i
 n   d   e   x   .   h   t   m
 l
...
----

Similarly, HTTP/2 is a binary protocol that transports the same information in a yet different format via TCP, while HTTP/3 is a binary protocol that transports the same information in yet another format via QUIC.

The HTTP protocol version may be _negotiated_ between client and server.
A request for a resource may be sent using one protocol (for example, HTTP/1.1), but the response may arrive in a different protocol (for example, HTTP/2).

`HttpClient` supports these `HttpClientTransport` implementations, each speaking only one protocol:

* `HttpClientTransportOverHTTP`, for <<transport-http11,HTTP/1.1>> (both clear-text and TLS encrypted)
* `HttpClientTransportOverHTTP2`, for <<transport-http2,HTTP/2>> (both clear-text and TLS encrypted)
* `HttpClientTransportOverHTTP3`, for <<transport-http3,HTTP/3>> (only encrypted via QUIC)
* `HttpClientTransportOverFCGI`, for <<transport-fcgi,FastCGI>> (both clear-text and TLS encrypted)

`HttpClient` also supports `HttpClientTransportDynamic`, a <<transport-dynamic,dynamic transport>> that can speak different HTTP formats and can select the right protocol by negotiating it with the server or by explicit indication from applications.

Furthermore, every HTTP format can be sent over different <<api-transport,low-level transports>> such as TCP, Unix-Domain, QUIC or memory.
Supports for Unix-Domain sockets requires Java 16 or later, since Unix-Domain sockets support has been introduced in OpenJDK with https://openjdk.java.net/jeps/380[JEP 380].

Applications are typically not aware of the actual HTTP format or low-level transport being used.
This allows them to write their logic against a high-level API that hides the details of the specific HTTP format and low-level transport being used.

[[transport-http11]]
=== HTTP/1.1 Transport

HTTP/1.1 is the default transport.

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=defaultTransport]
----

If you want to customize the HTTP/1.1 transport, you can explicitly configure it in this way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=http11Transport]
----

[[transport-http2]]
=== HTTP/2 Transport

The HTTP/2 transport can be configured in this way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=http2Transport]
----

`HTTP2Client` is the lower-level client that provides an API based on HTTP/2 concepts such as _sessions_, _streams_ and _frames_ that are specific to HTTP/2. See xref:client/http2.adoc[the HTTP/2 client section] for more information.

`HttpClientTransportOverHTTP2` uses `HTTP2Client` to format high-level semantic HTTP requests into the HTTP/2 specific format.

When the HTTP/2 server closes the connection (see xref:client/http2.adoc#goaway[this section]), ``RetryableStreamException``s are converted into ``RetryableRequestException``s, so that the high-level APIs do not depend on the transport.

See also xref:client/http2.adoc#listeners[this section] to be able to interact with the low-level HTTP/2 APIs while using the high-level APIs.

[[transport-http3]]
=== HTTP/3 Transport

The HTTP/3 transport can be configured in this way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=http3Transport]
----

`HTTP3Client` is the lower-level client that provides an API based on HTTP/3 concepts such as _sessions_, _streams_ and _frames_ that are specific to HTTP/3. See xref:client/http3.adoc[the HTTP/3 client section] for more information.

`HttpClientTransportOverHTTP3` uses `HTTP3Client` to format high-level semantic HTTP requests into the HTTP/3 specific format.

When the HTTP/3 server closes the connection (see xref:client/http3.adoc#goaway[this section]), ``RetryableStreamException``s are converted into ``RetryableRequestException``s, so that the high-level APIs do not depend on the transport.

[[transport-fcgi]]
=== FastCGI Transport

The FastCGI transport can be configured in this way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=fcgiTransport]
----

In order to make requests using the FastCGI transport, you need to have a FastCGI server such as https://en.wikipedia.org/wiki/PHP#PHPFPM[PHP-FPM] (see also link:http://php.net/manual/en/install.fpm.php).

The FastCGI transport is primarily used by Jetty's xref:server/fastcgi.adoc[FastCGI support] to serve PHP pages (WordPress for example).

[[transport-dynamic]]
=== Dynamic Transport

The static `HttpClientTransport` implementations work well if you know in advance the protocol you want to speak with the server, or if the server only supports one protocol (such as FastCGI).

With the advent of HTTP/2 and HTTP/3, however, servers are now able to support multiple protocols.

The HTTP/2 protocol is typically negotiated between client and server.
This negotiation can happen via ALPN, a TLS extension that allows the client to tell the server the list of protocol that the client supports, so that the server can pick one of the client supported protocols that also the server supports; or via HTTP/1.1 upgrade by means of the `Upgrade` header.

Applications can configure the dynamic transport with one or more HTTP versions such as HTTP/1.1, HTTP/2 or HTTP/3.
The implementation will take care of using TLS for HTTPS URIs, using ALPN if necessary, negotiating protocols, upgrading from one protocol to another, etc.

By default, the dynamic transport only speaks HTTP/1.1:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=dynamicDefault]
----

The dynamic transport can be configured with just one protocol, making it equivalent to the corresponding static transport:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=dynamicOneProtocol]
----

The dynamic transport, however, has been implemented to support multiple transports, in particular HTTP/1.1, HTTP/2 and HTTP/3:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=dynamicH1H2H3]
----

The order in which the protocols are specified to `HttpClientTransportDynamic` indicates what is the client preference (first the most preferred).

When clear-text communication is used (i.e. URIs with the `http` scheme) there is no HTTP protocol version negotiation, and therefore the application must know _a priori_ whether the server supports the HTTP version or not.
For example, if the server only supports clear-text HTTP/2, and `HttpClientTransportDynamic` is configured as in the example above, where HTTP/1.1 has precedence over HTTP/2, the client will send, by default, a clear-text HTTP/1.1 request to a clear-text HTTP/2 only server, which will result in a communication failure.

When using TLS (i.e. URIs with the `https` scheme), the HTTP protocol version is _negotiated_ between client and server via ALPN, and it is the server that decides what is the application protocol to use for the communication, regardless of the client preference.

[IMPORTANT]
====
HTTP/1.1 and HTTP/2 are _compatible_ because they both use TCP, while HTTP/3 is incompatible with previous HTTP versions because it uses QUIC.

Only compatible HTTP versions can negotiate the HTTP protocol version to use via ALPN, and only compatible HTTP versions can be upgraded from an older version to a newer version.
====

Provided that the server supports HTTP/1.1, HTTP/2 and HTTP/3, client applications can explicitly hint the version they want to use:

[,java,indent=0,options=nowrap]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=dynamicExplicitVersion]
----

If the client application explicitly specifies the HTTP version, then ALPN is not used by the client.
By specifying the HTTP version explicitly, the client application has prior-knowledge of what HTTP version the server supports, and therefore ALPN is not needed.
If the server does not support the HTTP version chosen by the client, then the communication will fail.

If the client application does not explicitly specify the HTTP version, then ALPN will be used by the client, but only for compatible protocols.
If the server also supports ALPN, then the protocol will be negotiated via ALPN and the server will choose the protocol to use.
If the server does not support ALPN, the client will try to use the first protocol configured in `HttpClientTransportDynamic`, and the communication may succeed or fail depending on whether the server supports the protocol chosen by the client.

For example, HTTP/3 is not compatible with previous HTTP version; if `HttpClientTransportDynamic` is configured to prefer HTTP/3, it will be the only protocol attempted by the client:

[,java,indent=0,options=nowrap]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=dynamicPreferH3]
----

When the client application configures `HttpClientTransportDynamic` to prefer HTTP/2, there could be ALPN negotiation between HTTP/2 and HTTP/1.1 (but not HTTP/3 because it is incompatible); HTTP/3 will only be possible by specifying the HTTP version explicitly:

[,java,indent=0,options=nowrap]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=dynamicPreferH2]
----

---

### Documentation Jetty Modules Programming Guide Pages Client Http2 (`documentation/jetty/modules/programming-guide/pages/client/http2.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= HTTP/2 Client Library

In the vast majority of cases, client applications should use the generic, high-level, xref:client/http.adoc[HTTP client library] that also provides HTTP/2 support via the pluggable xref:client/http.adoc#transport-http2[HTTP/2 transport] or the xref:client/http.adoc#transport-dynamic[dynamic transport].

The high-level HTTP library supports cookies, authentication, redirection, connection pooling and a number of other features that are absent in the low-level HTTP/2 library.

The HTTP/2 client library has been designed for those applications that need low-level access to HTTP/2 features such as _sessions_, _streams_ and _frames_, and this is quite a rare use case.

See also the correspondent xref:server/http2.adoc[HTTP/2 server library].

[[intro]]
== Introducing HTTP2Client

The Maven artifact coordinates for the HTTP/2 client library are the following:

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty.http2</groupId>
  <artifactId>jetty-http2-client</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

The main class is named `org.eclipse.jetty.http2.client.HTTP2Client`, and must be created, configured and started before use:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=start]
----

When your application stops, or otherwise does not need `HTTP2Client` anymore, it should stop the `HTTP2Client` instance (or instances) that were started:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=stop]
----

`HTTP2Client` allows client applications to connect to an HTTP/2 server.
A _session_ represents a single TCP connection to an HTTP/2 server and is defined by class `org.eclipse.jetty.http2.api.Session`.
A _session_ typically has a long life -- once the TCP connection is established, it remains open until it is not used anymore (and therefore it is closed by the idle timeout mechanism), until a fatal error occurs (for example, a network failure), or if one of the peers decides unilaterally to close the TCP connection.

HTTP/2 is a multiplexed protocol: it allows multiple HTTP/2 requests to be sent on the same TCP connection, or _session_.
Each request/response cycle is represented by a _stream_.
Therefore, a single _session_ manages multiple concurrent _streams_.
A _stream_ has typically a very short life compared to the _session_: a _stream_ only exists for the duration of the request/response cycle and then disappears.

[[flow-control]]
== HTTP/2 Flow Control

The HTTP/2 protocol is _flow controlled_ (see https://tools.ietf.org/html/rfc7540#section-5.2[the specification]).
This means that a sender and a receiver maintain a _flow control window_ that tracks the number of data bytes sent and received, respectively.
When a sender sends data bytes, it reduces its flow control window.
When a receiver receives data bytes, it also reduces its flow control window, and then passes the received data bytes to the application.
The application consumes the data bytes and tells back the receiver that it has consumed the data bytes.
The receiver then enlarges the flow control window, and the implementation arranges to send a message to the sender with the number of bytes consumed, so that the sender can enlarge its flow control window.

A sender can send data bytes up to its whole flow control window, then it must stop sending.
The sender may resume sending data bytes when it receives a message from the receiver that the data bytes sent previously have been consumed.
This message enlarges the sender flow control window, which allows the sender to send more data bytes.

HTTP/2 defines _two_ flow control windows: one for each _session_, and one for each _stream_.
Let's see with an example how they interact, assuming that in this example the session flow control window is 120 bytes and the stream flow control window is 100 bytes.

The sender opens a session, and then opens `stream_1` on that session, and sends `80` data bytes.
At this point the session flow control window is `40` bytes (`120 - 80`), and ``stream_1``'s flow control window is `20` bytes (`100 - 80`).
The sender now opens `stream_2` on the same session and sends `40` data bytes.
At this point, the session flow control window is `0` bytes (`40 - 40`), while ``stream_2``'s flow control window is `60` (`100 - 40`).
Since now the session flow control window is `0`, the sender cannot send more data bytes, neither on `stream_1` nor on `stream_2`, nor on other streams, despite all the streams having their stream flow control windows greater than `0`.

The receiver consumes ``stream_2``'s `40` data bytes and sends a message to the sender with this information.
At this point, the session flow control window is `40` (``0 + 40``), ``stream_1``'s flow control window is still `20` and ``stream_2``'s flow control window is `100` (``60 + 40``).
If the sender opens `stream_3` and would like to send `50` data bytes, it would only be able to send `40` because that is the maximum allowed by the session flow control window at this point.

It is therefore very important that applications notify the fact that they have consumed data bytes as soon as possible, so that the implementation (the receiver) can send a message to the sender (in the form of a `WINDOW_UPDATE` frame) with the information to enlarge the flow control window, therefore reducing the possibility that sender stalls due to the flow control windows being reduced to `0`.

How a client application should handle HTTP/2 flow control is discussed in details in <<response,this section>>.

[[connect]]
== Connecting to the Server

The first thing an application should do is to connect to the server and obtain a `Session`.
The following example connects to the server on a clear-text port:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=clearTextConnect]
----

The following example connects to the server on an encrypted port:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=encryptedConnect]
----

IMPORTANT: Applications must know in advance whether they want to connect to a clear-text or encrypted port, and pass the `SslContextFactory` parameter accordingly to the `connect(\...)` method.

[[configure]]
== Configuring the Session

The `connect(\...)` method takes a `Session.Listener` parameter.
This listener's `onPreface(\...)` method is invoked just before establishing the connection to the server to gather the client configuration to send to the server.
Client applications can override this method to change the default configuration:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=configure]
----

The `Session.Listener` is notified of session events originated by the server such as receiving a `SETTINGS` frame from the server, or the server closing the connection, or the client timing out the connection due to idleness.
Please refer to the `Session.Listener` link:{javadoc-url}/org/eclipse/jetty/http2/api/Session.Listener.html[javadocs] for the complete list of events.

Once a `Session` has been established, the communication with the server happens by exchanging _frames_, as specified in the https://tools.ietf.org/html/rfc7540#section-4[HTTP/2 specification].

[[request]]
== Sending a Request

Sending an HTTP request to the server, and receiving a response, creates a _stream_ that encapsulates the exchange of HTTP/2 frames that compose the request and the response.

In order to send an HTTP request to the server, the client must send a `HEADERS` frame.
`HEADERS` frames carry the request method, the request URI and the request headers.
Sending the `HEADERS` frame opens the `Stream`:

[,java,indent=0,subs=attributes+]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=newStream]
----

Note how `Session.newStream(\...)` takes a `Stream.Listener` parameter.
This listener is notified of stream events originated by the server such as receiving `HEADERS` or `DATA` frames that are part of the response, discussed in more details in the <<response,section below>>.
Please refer to the `Stream.Listener` link:{javadoc-url}/org/eclipse/jetty/http2/api/Stream.Listener.html[javadocs] for the complete list of events.

HTTP requests may have content, which is sent using the `Stream` APIs:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=newStreamWithData]
----

IMPORTANT: When sending two `DATA` frames consecutively, the second call to `Stream.data(\...)` must be done only when the first is completed, or a `WritePendingException` will be thrown.
Use the `Callback` APIs or `CompletableFuture` APIs to ensure that the second `Stream.data(\...)` call is performed when the first completed successfully.

[[response]]
== Receiving a Response

Response events are delivered to the `Stream.Listener` passed to `Session.newStream(\...)`.

An HTTP response is typically composed of a `HEADERS` frame containing the HTTP status code and the response headers, and optionally one or more `DATA` frames containing the response content bytes.

The HTTP/2 protocol also supports response trailers (that is, headers that are sent after the response content) that also are sent using a `HEADERS` frame.

A client application can therefore receive the HTTP/2 frames sent by the server by implementing the relevant methods in `Stream.Listener`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=responseListener]
----

[NOTE]
====
When `onDataAvailable(Stream stream)` is invoked, the demand is implicitly cancelled.

Just returning from the `onDataAvailable(Stream stream)` method does _not_ implicitly demand for more `DATA` frames.

Applications must call `Stream.demand()` to explicitly require that `onDataAvailable(Stream stream)` is invoked again when more `DATA` frames are available.
====

Applications that consume the content buffer within `onDataAvailable(Stream stream)` (for example, writing it to a file, or copying the bytes to another storage) should call `Data.release()` as soon as they have consumed the content buffer.
This allows the implementation to reuse the buffer, reducing the memory requirements needed to handle the content buffers.

Alternatively, an application may store away the `Data` object to consume the buffer bytes later, or pass the `Data` object to another asynchronous API (this is typical in proxy applications).

[IMPORTANT]
====
The call to `Stream.readData()` tells the implementation to enlarge the stream and session flow control windows so that the sender will be able to send more `DATA` frames without stalling.
====

Applications can unwrap the `Data` object into some other object that may be used later, provided that the _release_ semantic is maintained:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/HTTP2Docs.java[tags=dataUnwrap]
----

[IMPORTANT]
====
Applications that implement `onDataAvailable(Stream stream)` must remember to call `Stream.demand()` eventually.

If they do not call `Stream.demand()`, the implementation will not invoke `onDataAvailable(Stream stream)` to deliver more `DATA` frames and the application will stall threadlessly until an idle timeout fires to close the stream or the session.
====

[[reset]]
== Resetting a Request or Response

In HTTP/2, clients and servers have the ability to tell to the other peer that they are not interested anymore in either the request or the response, using a `RST_STREAM` frame.

The `HTTP2Client` APIs allow client applications to send and receive this "reset" frame:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=reset]
----

[[push]]
== Receiving HTTP/2 Pushes

HTTP/2 servers have the ability to push resources related to a primary resource.
When an HTTP/2 server pushes a resource, it sends to the client a `PUSH_PROMISE` frame that contains the request URI and headers that a client would use to request explicitly that resource.

Client applications can be configured to tell the server to never push resources, see <<configure,this section>>.

Client applications can listen to the push events, and act accordingly:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=push]
----

If a client application does not want to handle a particular HTTP/2 push, it can just reset the pushed stream to tell the server to stop sending bytes for the pushed stream:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=pushReset]
----

[[goaway]]
== Session Closing

Once a `Session` has been established (see <<connect,here>>), it may be closed in the following way, providing an error code and a short textual reason:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=close]
----

Closing a `Session` will send a `GOAWAY` frame to the server.

When the server closes the `Session`, the `GOAWAY` frame received by the client carries the identifier of the last stream that was processed by the server.
Streams that have an identifier higher than the one specified in the `GOAWAY` frame are not processed by the server.

A client application may be sending requests while the server is closing the `Session`, so it is possible that the client opens a stream that will not be processed by the server, because it has an identifier that is greater than the one specified in the `GOAWAY` frame sent by the server.

In this case the stream is failed on the client with a `RetryableStreamException`, and client applications may decide whether to retry the request (if the request can be retried):

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=retryStream]
----

[[listeners]]
== Listening to HTTP/2 Events

The HTTP/2 low-level APIs allow you to listen and react to semantic events such as HTTP requests and HTTP responses.

You can use `HTTP2Session.LifeCycleListener` to be notified of lifecycle events of a `Session` (when it is opened and when it is closed), and `HTTP2Session.FrameListener` to be notified of what HTTP/2 frames have been sent and received.

These listeners are useful to implement cross-cutting concerns that are not related to the handling of HTTP requests and responses.

For example, you can use a `HTTP2Session.LifeCycleListener` to record and log how long sessions remain open:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=listenerLifeCycle]
----

Another example is to use a `HTTP2Session.FrameListener` to log HTTP/2 frames for HTTP/2 protocol troubleshooting:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=listenerLogging]
----

In limited cases, you can use these listeners to access the low-level HTTP/2 APIs even if your application uses the xref:client/http.adoc[high-level APIs] with the xref:client/http.adoc#transport-http2[HTTP/2 transport].

---

### Documentation Jetty Modules Programming Guide Pages Client Http3 (`documentation/jetty/modules/programming-guide/pages/client/http3.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= HTTP/3 Client Library

In the vast majority of cases, client applications should use the generic, high-level, xref:client/http.adoc[HTTP client library] that also provides HTTP/3 support via the pluggable xref:client/http.adoc#transport-http3[HTTP/3 transport] or the xref:client/http.adoc#transport-dynamic[dynamic transport].

The high-level HTTP library supports cookies, authentication, redirection, connection pooling and a number of other features that are absent in the low-level HTTP/3 library.

The low-level HTTP/3 client library has been designed for those applications that need low-level access to HTTP/3 features such as _sessions_, _streams_ and _frames_, and this is quite a rare use case.

See also the correspondent xref:server/http3.adoc[HTTP/3 server library].

[[intro]]
== Introducing HTTP3Client

The Maven artifact coordinates for the HTTP/3 client library are the following:

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty.http3</groupId>
  <artifactId>jetty-http3-client</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

The HTTP/3 protocol is layered on top of the underlying QUIC protocol.

The QUIC protocol implementation underlying HTTP/3 is provided by Jetty classes that wrap the link:https://github.com/cloudflare/quiche[Quiche] native library, so you need also the following artifact:

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty.quic</groupId>
  <artifactId>jetty-quic-quiche-client</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

Furthermore, the binding to the Quiche native library has two implementations: one based on link:https://github.com/java-native-access/jna[JNA] that works with Java 17 or later, and one based on JDK's https://openjdk.org/jeps/454[Foreign Function & Memory (FFM)] APIs that works with Java 22 or later.

You must choose the binding you want to use (depending on the Java version you are using), with either of the following artifacts:

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty.quic</groupId>
  <artifactId>jetty-quic-quiche-jna</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty.quic</groupId>
  <artifactId>jetty-quic-quiche-foreign</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

[NOTE]
====
Since Java 22, the JVM emits a warning message when Java code tries to access code in native libraries.
For example with Java 25:

----
WARNING: A restricted method in java.lang.foreign.AddressLayout has been called
WARNING: java.lang.foreign.AddressLayout::withTargetLayout has been called by org.eclipse.jetty.quic.quiche.foreign.NativeHelper in an unnamed module (file:/home/simon/opensource/jetty/12.1.x/jetty-core/jetty-quic/jetty-quic-quiche/jetty-quic-quiche-foreign/target/classes/)
WARNING: Use --enable-native-access=ALL-UNNAMED to avoid a warning for callers in this module
WARNING: Restricted methods will be blocked in a future release unless native access is enabled
----

To avoid the warning, you can start the JVM as suggested by the warning message above, by adding `--enable-native-access=ALL-UNNAMED` on the command line.
This grants any code in the class-path access to any native library.

If you are running in JPMS mode, you can explicitly grant access just to the JPMS module(s) that require it.
To grant Jetty access to the Quiche native library, use `--enable-native-access=org.eclipse.jetty.quic.quiche.foreign`.
This grants only to the Jetty JPMS module `org.eclipse.jetty.quic.quiche.foreign` in the module-path access to any native library.
====

The main class is named `org.eclipse.jetty.http3.client.HTTP3Client`, and must be created, configured and started before use:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http3/HTTP3ClientDocs.java[tags=start]
----

When your application stops, or otherwise does not need `HTTP3Client` anymore, it should stop the `HTTP3Client` instance (or instances) that were started:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http3/HTTP3ClientDocs.java[tags=stop]
----

`HTTP3Client` allows client applications to connect to an HTTP/3 server.

When a client wants to establish a connection with a server, the QUIC protocol establishes the connection with the server, represented by a QUIC _session_.

A QUIC _session_ typically has a long life -- once the connection is established, it remains active until it is not used anymore (and therefore it is closed by the idle timeout mechanism), until a fatal error occurs (for example, a network failure), or if one of the peers decides unilaterally to close the connection.

There is a 1-to-1 relationship between a QUIC _session_ and an HTTP/3 _session_, defined by class `org.eclipse.jetty.http3.api.Session.Client`.

The QUIC protocol is multiplexed, that is it allows multiple, independent, QUIC _streams_ to exchange data to and from the server, over a single connection.
Therefore, a single QUIC _session_ manages multiple concurrent QUIC _streams_.

There is a 1-to-1 relationship between a QUIC _stream_ and an HTTP/3 _stream_, defined by class `org.eclipse.jetty.http3.api.Stream.Client`.

Each HTTP/3 request/response cycle is carried by an HTTP/3 _stream_, and has typically a very short life compared to the _session_: a _stream_ only exists for the duration of the request/response cycle and then disappears.

// TODO: flow control?
//[[pg-client-http3-flow-control]]
//==== HTTP/3 Flow Control

//include::../../http3.adoc[tag=flowControl]

//How a client application should handle HTTP/3 flow control is discussed in details in <<response,this section>>.

[[connect]]
== Connecting to the Server

The first thing an application should do is to connect to the server and obtain a `Session`.
The following example connects to the server:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http3/HTTP3ClientDocs.java[tags=connect]
----

[[configure]]
== Configuring the Session

The `connect(\...)` method takes a `Session.Client.Listener` parameter.
This listener's `onPreface(\...)` method is invoked just before establishing the connection to the server to gather the client configuration to send to the server.
Client applications can override this method to change the default configuration:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http3/HTTP3ClientDocs.java[tags=configure]
----

The `Session.Client.Listener` is notified of session events originated by the server such as receiving a `SETTINGS` frame from the server, or the server closing the connection, or the client timing out the connection due to idleness.
Please refer to the `Session.Client.Listener` link:{javadoc-url}/org/eclipse/jetty/http3/api/Session.Client.Listener.html[javadocs] for the complete list of events.

Once a `Session` has been established, the communication with the server happens by exchanging HTTP/3 _frames_ over QUIC _streams_.

[[request]]
== Sending a Request

Sending an HTTP request to the server, and receiving a response, creates a _stream_ that encapsulates the exchange of HTTP/3 frames that compose the request and the response.

In order to send an HTTP request to the server, the client must send a `HEADERS` frame.
`HEADERS` frames carry the request method, the request URI and the request headers.
Sending the `HEADERS` frame opens the `Stream`:

[,java,indent=0,subs=attributes+]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http3/HTTP3ClientDocs.java[tags=newStream]
----

Note how `Session.newRequest(\...)` takes a `Stream.Client.Listener` parameter.
This listener is notified of stream events originated by the server such as receiving `HEADERS` or `DATA` frames that are part of the response, discussed in more details in the <<response,section below>>.
Please refer to the `Stream.Client.Listener` link:{javadoc-url}/org/eclipse/jetty/http3/api/Stream.Client.Listener.html[javadocs] for the complete list of events.

HTTP requests may have content, which is sent using the `Stream` APIs:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http3/HTTP3ClientDocs.java[tags=newStreamWithData]
----

IMPORTANT: When sending two `DATA` frames consecutively, the second call to `Stream.data(\...)` must be done only when the first is completed, or a `WritePendingException` will be thrown.
Use the `Promise` APIs to ensure that the second `Stream.data(\...)` call is performed when the first completed successfully.

[[response]]
== Receiving a Response

Response events are delivered to the `Stream.Client.Listener` passed to `Session.newRequest(\...)`.

An HTTP response is typically composed of a `HEADERS` frame containing the HTTP status code and the response headers, and optionally one or more `DATA` frames containing the response content bytes.

The HTTP/3 protocol also supports response trailers (that is, headers that are sent after the response content) that also are sent using a `HEADERS` frame.

A client application can therefore receive the HTTP/3 frames sent by the server by implementing the relevant methods in `Stream.Client.Listener`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http3/HTTP3ClientDocs.java[tags=responseListener]
----

// TODO: flow control?
//include::../../http3.adoc[tag=apiFlowControl]

[[terminate]]
== Terminating a Request or Response

In HTTP/3, clients and servers have the ability to tell to the other peer that they are not interested anymore in either the request or the response, by terminating the stream.

The `HTTP3Client` APIs allow client applications to send and receive this event:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http3/HTTP3ClientDocs.java[tags=terminate]
----

[[goaway]]
== Session Closing

Once a `Session` has been established (see <<connect,here>>), it may be closed in the following way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http3/HTTP3ClientDocs.java[tags=close]
----

Closing a `Session` will send a `GOAWAY` frame to the server.

When the server closes the `Session`, the `GOAWAY` frame received by the client carries the identifier of the last stream that was processed by the server.
Streams that have an identifier higher than the one specified in the `GOAWAY` frame are not processed by the server.

A client application may be sending requests while the server is closing the `Session`, so it is possible that the client opens a stream that will not be processed by the server, because it has an identifier that is greater than the one specified in the `GOAWAY` frame sent by the server.

In this case the stream is failed on the client with a `RetryableStreamException`, and client applications may decide whether to retry the request (if the request can be retried):

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http3/HTTP3ClientDocs.java[tags=retryStream]
----

---

### Documentation Jetty Modules Programming Guide Pages Client Index (`documentation/jetty/modules/programming-guide/pages/client/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Client Libraries

The Eclipse Jetty Project provides client-side libraries that allow you to embed an HTTP or WebSocket client in your applications.
A typical example is a client application that needs to contact a third party service via HTTP (for example a REST service).
Another example is a proxy application that receives HTTP requests and forwards them as FCGI requests to a PHP application such as WordPress, or receives HTTP/1.1 requests and converts them to HTTP/2 or HTTP/3.
Yet another example is a client application that needs to receive events from a WebSocket server.

The client libraries are designed to be non-blocking and offer both synchronous and asynchronous APIs and come with many configuration options.

These are the available client libraries:

* xref:client/http.adoc[The High-Level HTTP Client Library] for HTTP/1.1, HTTP/2, HTTP/3 and FastCGI
* xref:client/http2.adoc[The Low-Level HTTP/2 Client Library] for low-level HTTP/2
* xref:client/http3.adoc[The Low-Level HTTP/3 Client Library] for low-level HTTP/3
* xref:client/websocket.adoc[The WebSocket client library]

If you are interested in the low-level details of how the Jetty client libraries work, or are interested in writing a custom protocol, look at the xref:client/io-arch.adoc[Client I/O Architecture] and the xref:client/threads.adoc[client threading architecture].

---

### Documentation Jetty Modules Programming Guide Pages Client Io Arch (`documentation/jetty/modules/programming-guide/pages/client/io-arch.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Client I/O Architecture

The Jetty client libraries provide the basic components and APIs to implement a client application.

They build on the common xref:arch/io.adoc[Jetty I/O Architecture] and provide client specific concepts (such as establishing a connection to a server).

There are conceptually two layers that compose the Jetty client libraries:

. <<transport,The transport layer>>, that handles the low-level communication with the server, and deals with buffers, threads, etc.
. <<protocol,The protocol layer>>, that handles the high-level protocol by parsing the bytes read from the transport layer and by generating the bytes to write to the transport layer.

[[transport]]
== Transport Layer

The transport layer is the low-level layer that communicates with the server.

Protocols such as HTTP/1.1 and HTTP/2 are typically transported over TCP, while the newer HTTP/3 is transported over QUIC, which is itself transported over UDP.

However, there are other means of communication supported by the Jetty client libraries, in particular over <<unix-domain,Unix-Domain sockets>> (for inter-process communication), and over <<memory,memory>> (for intra-process communication).

The same high-level protocol can be carried by different low-level transports.
For example, the high-level HTTP/1.1 protocol can be transported over either TCP (the default), or QUIC, or Unix-Domain sockets, or memory, because all these low-level transport provide reliable and ordered communication between client and server.

Similarly, the high-level HTTP/3 protocol can be transported over either QUIC (the default) or memory.
It would be possible to transport HTTP/3 also over Unix-Domain sockets, but the current version of Java only supports Unix-Domain sockets for ``SocketChannel``s and not for ``DatagramChannel``s.

The Jetty client libraries use the common I/O design described in xref:arch/io.adoc[this section].

The common I/O components and concepts are used for all low-level transports.
The only partial exception is the <<memory,memory transport>>, which is not based on network components; as such it does not need a `SelectorManager`, but it exposes `EndPoint` so that high-level protocols have a common interface to interact with the low-level transport.

The client-side abstraction for the low-level transport is `org.eclipse.jetty.io.Transport`.

`Transport` represents how high-level protocols can be transported; there is `Transport.TCP_IP` that represents communication over TCP, but also `Transport.TCPUnix` for Unix-Domain sockets, `QuicheTransport` for QUIC and `MemoryTransport` for memory.

Applications can specify the `Transport` to use for each request as described in xref:client/http.adoc#api-transport[this section].

When the `Transport` implementation uses the network, it delegates to `org.eclipse.jetty.io.ClientConnector`.

`ClientConnector` primarily wraps `org.eclipse.jetty.io.SelectorManager` to provide network functionalities, and aggregates other four components:

* a thread pool (in form of an `java.util.concurrent.Executor`)
* a scheduler (in form of `org.eclipse.jetty.util.thread.Scheduler`)
* a byte buffer pool (in form of `org.eclipse.jetty.io.ByteBufferPool`)
* a TLS factory (in form of `org.eclipse.jetty.util.ssl.SslContextFactory.Client`)

The `ClientConnector` is where you want to set those components after you have configured them.
If you don't explicitly set those components on the `ClientConnector`, then appropriate defaults will be chosen when the `ClientConnector` starts.

`ClientConnector` manages all network-related components, and therefore it is used for TCP, UDP, QUIC and <<unix-domain,Unix-Domain sockets>>.

The simplest example that creates and starts a `ClientConnector` is the following:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/ClientConnectorDocs.java[tags=simplest]
----

A more typical example:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/ClientConnectorDocs.java[tags=typical]
----

A more advanced example that customizes the `ClientConnector` by overriding some of its methods:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/ClientConnectorDocs.java[tags=advanced]
----

Since `ClientConnector` is the component that handles the low-level network transport, it is also the component where you want to configure the low-level network configuration.

The most common parameters are:

* `ClientConnector.selectors`: the number of ``java.nio.Selector``s components (defaults to `1`) that are present to handle the ``SocketChannel``s and ``DatagramChannel``s opened by the `ClientConnector`.
You typically want to increase the number of selectors only for those use cases where each selector should handle more than few hundreds _concurrent_ socket events.
For example, one selector typically runs well for `250` _concurrent_ socket events; as a rule of thumb, you can multiply that number by `10` to obtain the number of opened sockets a selector can handle (`2500`), based on the assumption that not all the `2500` sockets will be active _at the same time_.
* `ClientConnector.idleTimeout`: the duration of time after which `ClientConnector` closes a socket due to inactivity (defaults to `30` seconds).
This is an important parameter to configure, and you typically want the client idle timeout to be shorter than the server idle timeout, to avoid race conditions where the client attempts to use a socket just before the client-side idle timeout expires, but the server-side idle timeout has already expired and the is already closing the socket.
* `ClientConnector.connectBlocking`: whether the operation of connecting a socket to the server (i.e. `SocketChannel.connect(SocketAddress)`) must be a blocking or a non-blocking operation (defaults to `false`).
For `localhost` or same datacenter hosts you want to set this parameter to
`true` because DNS resolution will be immediate (and likely never fail).
For generic Internet hosts (e.g. when you are implementing a web spider) you want to set this parameter to `false`.
* `ClientConnector.connectTimeout`: the duration of time after which `ClientConnector` aborts a connection attempt to the server (defaults to `5` seconds).
This time includes the DNS lookup time _and_ the TCP connect time.

Please refer to the `ClientConnector` link:{javadoc-url}/org/eclipse/jetty/io/ClientConnector.html[javadocs] for the complete list of configurable parameters.

[[unix-domain]]
=== Unix-Domain Support

https://openjdk.java.net/jeps/380[JEP 380] introduced Unix-Domain sockets support in Java 16, on all operative systems, but only for ``SocketChannel``s (not for ``DatagramChannel``s).

`ClientConnector` handles Unix-Domain sockets exactly like it handles regular TCP sockets, so there is no additional configuration necessary -- Unix-Domain sockets are supported out-of-the-box.

Applications can specify the `Transport` to use for each request as described in xref:client/http.adoc#api-transport[this section].

[[memory]]
=== Memory Support

In addition to support communication between client and server via network or Unix-Domain, the Jetty client libraries also support communication between client and server via memory for intra-process communication.
This means that the client and server must be in the same JVM process.

This functionality is provided by `org.eclipse.jetty.server.MemoryTransport`, which does not delegate to `ClientConnector`, but instead delegates to the server-side `MemoryConnector` and its related classes.

Applications can specify the `Transport` to use for each request as described in xref:client/http.adoc#api-transport[this section].

[[protocol]]
== Protocol Layer

The protocol layer builds on top of the transport layer to generate the bytes to be written to the low-level transport and to parse the bytes read from the low-level transport.

Recall from xref:arch/io.adoc#connection[this section] that Jetty uses the `Connection` abstraction to produce and interpret the low-level transport bytes.

On the client side, a `ClientConnectionFactory` implementation is the component that creates `Connection` instances based on the protocol that the client wants to "speak" with the server.

Applications may use `ClientConnector.connect(SocketAddress, Map<String, Object>)` to establish a TCP connection to the server, and must provide `ClientConnector` with the following information in the context map:

* A `Transport` instance that specifies the low-level transport to use.
* A `ClientConnectionFactory` that creates `Connection` instances for the high-level protocol.
* A `Promise` that is notified when the connection creation succeeds or fails.

For example:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/ClientConnectorDocs.java[tags=connect]
----

When a `Connection` is created successfully, its `onOpen()` method is invoked, and then the promise is completed successfully.

It is now possible to write a super-simple `telnet` client that reads and writes string lines:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/ClientConnectorDocs.java[tags=telnet]
----

Note how a very basic "telnet" API that applications could use is implemented in the form of the `onLine(Consumer<String>)` for the non-blocking receiving side and `writeLine(String, Callback)` for the non-blocking sending side.
Note also how the `onFillable()` method implements some basic "parsing" by looking up the `\n` character in the buffer.

NOTE: The "telnet" client above looks like a super-simple HTTP client because HTTP/1.0 can be seen as a line-based protocol.
HTTP/1.0 was used just as an example, but we could have used any other line-based protocol such as https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol[SMTP], provided that the server was able to understand it.

This is very similar to what the Jetty client implementation does for real network protocols.
Real network protocols are of course more complicated and so is the implementation code that handles them, but the general ideas are similar.

The Jetty client implementation provides a number of `ClientConnectionFactory` implementations that can be composed to produce and interpret the network bytes.

For example, it is simple to modify the above example to use the TLS protocol so that you will be able to connect to the server on port `443`, typically reserved for the secure HTTP protocol.

The differences between the clear-text version and the TLS encrypted version are minimal:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/ClientConnectorDocs.java[tags=tlsTelnet]
----

The differences with the clear-text version are only:

* Change the port from `80` to `443`.
* Wrap the `ClientConnectionFactory` with `SslClientConnectionFactory`.
* Unwrap the `SslConnection` to access `TelnetConnection`.

---

### Documentation Jetty Modules Programming Guide Pages Client Threads (`documentation/jetty/modules/programming-guide/pages/client/threads.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Client Threading Architecture

The Jetty HTTP client libraries are xref:client/http.adoc[non-blocking] and xref:client/http.adoc#listeners[event-based].
Client applications provide listeners that are notified of request and response events, and the listeners implement the client application logic.

In general, the thread that sends the `Request` is the thread that emits the request events.
Writing blocking code in a request listener blocks the sending of the `Request`.

The processing of ``Response``s, on the other hand, follows the general xref:arch/io.adoc[Jetty I/O architecture] and xref:arch/threads.adoc[Jetty threading architecture], and it is detailed in <<response-threading,this section>>.

== DNS Lookup Threading

In order to send a request, a connection needs to be established, and this involves a query to the DNS to convert the host name to an IP address (this is necessary only for network transports, see xref:client/io-arch.adoc[here]).

The Java platform only offers blocking APIs to query the DNS, via `InetAddress.getAllByName(String)`.
While DNS queries are typically fast (few milliseconds), in unfortunate cases they may last several seconds, blocking the thread that sends the request.

To avoid blocking the request sender thread, `HttpClient` is configured by default with `SocketAddressResolver.Async`, that executes the DNS query in a different thread.

If you want to avoid this thread hand-off, especially when the request host is `localhost` or resolved locally (for example configured in `/etc/hosts`), you can configure `HttpClient` with `SocketAddressResolver.Sync` that will perform the DNS query in the request sender thread.

[[response-threading]]
== Response Processing Threading

As explained in the general xref:arch/threads.adoc[Jetty threading architecture], client applications can specify the `InvocationType` of the Jetty implementation tasks that invoke client application code in `Response` listeners.

There are two main Jetty implementation tasks that invoke client application code in `Response` listeners:

* The task that invokes the `Response` listeners, detailed <<invocation-type-listeners,here>>.
* The task that invokes the response's `Content.Source` demand callback, detailed <<invocation-type-demand,here>>.

[[invocation-type-listeners]]
=== `Response` Listener `InvocationType`

The `InvocationType` of the task that invokes the `Response` listeners is configured in the `HttpClientTransport` passed to `HttpClient`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/ClientDocs.java[tags=transportInvocationType]
----

The default `InvocationType` for all ``HttpClientTransport``s is `BLOCKING`.

[[invocation-type-demand]]
== `Response` Demand `InvocationType`

The `InvocationType` of the task that invokes the `Response` xref:arch/io.adoc#content-source[`Content.Source`] demand callback is determined from the demand callback object passed to link:{javadoc-url}/org/eclipse/jetty/io/Content.Source.html#demand(java.lang.Runnable)[`Content.Source.demand(Runnable)`], and defines the `InvocationType` with which the demand callback is invoked.

Rather than passing a simple `Runnable` or a lambda expression (for which the `InvocationType` is assumed to be `BLOCKING`), you can pass an `Invocable.Task`, that implements both `Invocable` (to specify the `InvocationType`) and `Runnable` (to specify the demand callback).

For example:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/ClientDocs.java[tags=demandInvocationType]
----
<1> Specifies the `InvocationType` for response listener processing.
<2> Specifies the demand callback to be an `Invocable.Task` with `InvocationType` `NON_BLOCKING`.

---

### Documentation Jetty Modules Programming Guide Pages Client Websocket (`documentation/jetty/modules/programming-guide/pages/client/websocket.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= WebSocket Client

Jetty's `WebSocketClient` is a more powerful alternative to the WebSocket client provided by the standard JSR 356 `javax.websocket` APIs.

Similarly to Jetty's xref:client/http.adoc[`HttpClient`], the `WebSocketClient` is non-blocking and asynchronous, making it very efficient in resource utilization.
A synchronous, blocking, API is also offered for simpler cases.

Since the first step of establishing a WebSocket communication is an HTTP request, `WebSocketClient` makes use of `HttpClient` and therefore depends on it.

The Maven artifact coordinates are the following:

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty.websocket</groupId>
  <artifactId>jetty-websocket-jetty-client</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

[[start]]
== Starting WebSocketClient

The main class is `org.eclipse.jetty.websocket.client.WebSocketClient`; you instantiate it, configure it, and then start it like many other Jetty components.
This is a minimal example:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/websocket/WebSocketClientDocs.java[tags=start]
----

However, it is recommended that you explicitly pass an `HttpClient` instance to `WebSocketClient` so that you can have control over the HTTP configuration as well:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/websocket/WebSocketClientDocs.java[tags=startWithHttpClient]
----

You may create multiple instances of `WebSocketClient`, but typically one instance is enough for most applications.
Creating multiple instances may be necessary for example when you need to specify different configuration parameters for different instances.
For example, you may need different instances when you need to configure the `HttpClient` differently: different transports, different proxies, different cookie stores, different authentications, etc.

The configuration that is not WebSocket specific (such as idle timeout, etc.) should be directly configured on the associated `HttpClient` instance.

The WebSocket specific configuration can be configured directly on the `WebSocketClient` instance.
Configuring the `WebSocketClient` allows to give default values to various parameters, whose values may be overridden more specifically, as described in <<session-configure,this section>>.

Refer to the  `WebSocketClient` link:{javadoc-url}/org/eclipse/jetty/websocket/client/WebSocketClient.html[javadocs] for the setter methods available to customize the WebSocket specific configuration.

[[stop]]
== Stopping WebSocketClient

It is recommended that when your application stops, you also stop the `WebSocketClient` instance (or instances) that you are using.

Similarly to xref:client/http.adoc#stop[stopping `HttpClient`], you want to stop `WebSocketClient` from a thread that is not owned by `WebSocketClient` itself, for example:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/websocket/WebSocketClientDocs.java[tags=stop]
----

[[connect]]
== Connecting to a Remote Host

A WebSocket client may initiate the communication with the server either <<connect-http11,using HTTP/1.1>> or <<connect-http2,using HTTP/2>>.
The two mechanism are quite different and detailed in the following sections.

[[connect-http11]]
=== Using HTTP/1.1

Initiating a WebSocket communication with a server using HTTP/1.1 is detailed in https://tools.ietf.org/html/rfc6455#section-1.8[RFC 6455].

A WebSocket client first establishes a TCP connection to the server, then sends an HTTP/1.1 _upgrade_ request.

If the server supports upgrading to WebSocket, it responds with HTTP status code `101`, and then switches the communication over that connection, either incoming or outgoing, to happen using the WebSocket protocol.

When the client receives the HTTP status code `101`, it switches the communication over that connection, either incoming or outgoing, to happen using the WebSocket protocol.

[plantuml]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false

participant ClientEndPoint
participant WebSocketClient
participant HttpClient
participant Server
participant ServerEndPoint

WebSocketClient -> HttpClient : connect()
HttpClient -> Server : TCP/TLS connect
HttpClient -> Server : GET / HTTP/1.1\nUpgrade: websocket
Server -> ServerEndPoint ** : create
Server -> HttpClient : HTTP/1.1 101\nUpgrade: websocket
HttpClient -> WebSocketClient
WebSocketClient -> ClientEndPoint ** : create
ClientEndPoint -> WebSocketClient : WebSocket Frame A
WebSocketClient -> Server : WebSocket Frame A
Server -> ServerEndPoint : WebSocket Frame A
ServerEndPoint -> Server : WebSocket Frame B
Server -> WebSocketClient : WebSocket Frame B
WebSocketClient -> ClientEndPoint : WebSocket Frame B
----

In code:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/websocket/WebSocketClientDocs.java[tags=connectHTTP11]
----

`WebSocketClient.connect()` links the client-side WebSocket _endpoint_ to a specific server URI, and returns a `CompletableFuture` of an `org.eclipse.jetty.websocket.api.Session`.

The endpoint offers APIs to _receive_ WebSocket data (or errors) from the server, while the session offers APIs to _send_ WebSocket data to the server.

[[connect-http2]]
=== Using HTTP/2

Initiating a WebSocket communication with a server using HTTP/1.1 is detailed in https://tools.ietf.org/html/rfc8441[RFC 8441].

A WebSocket client establishes a TCP connection to the server or reuses an existing one currently used for HTTP/2, then sends an HTTP/2 _CONNECT_ request over an HTTP/2 stream.

If the server supports upgrading to WebSocket, it responds with HTTP status code `200`, then switches the communication over that stream, either incoming or outgoing, to happen using HTTP/2 `DATA` frames wrapping WebSocket frames.

When the client receives the HTTP status code `200`, it switches the communication over that stream, either incoming or outgoing, to happen using HTTP/2 `DATA` frames wrapping WebSocket frames.

From an external point of view, it will look like client is sending chunks of an infinite HTTP/2 request upload, and the server is sending chunks of an infinite HTTP/2 response download, as they will exchange HTTP/2 `DATA` frames; but the HTTP/2 `DATA` frames will contain each one or more WebSocket frames that both client and server know how to deliver to the respective WebSocket endpoints.

When either WebSocket endpoint decides to terminate the communication, the HTTP/2 stream will be closed as well.

[plantuml]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false

participant ClientEndPoint
participant WebSocketClient
participant HttpClient
participant Server
participant ServerEndPoint

WebSocketClient -> HttpClient : connect()
HttpClient --> Server : TCP/TLS connect
HttpClient -> Server : HEADERS\n:method: CONNECT\n:protocol: websocket
Server -> ServerEndPoint ** : create
Server -> HttpClient : HEADERS\n:status: 200\n: websocket
HttpClient -> WebSocketClient
WebSocketClient -> ClientEndPoint ** : create
ClientEndPoint -> HttpClient : WebSocket Frame A
HttpClient -> Server : DATA\nWebSocket Frame A
Server -> ServerEndPoint : WebSocket Frame A
ServerEndPoint -> Server : WebSocket Frame B
Server -> HttpClient : DATA\nWebSocket Frame B
HttpClient -> ClientEndPoint : WebSocket Frame B
----

In code:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/websocket/WebSocketClientDocs.java[tags=connectHTTP2]
----

Alternatively, you can use the xref:client/http.adoc#transport-dynamic[dynamic `HttpClient` transport]:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/websocket/WebSocketClientDocs.java[tags=connectHTTP2Dynamic]
----

[[connect-custom-http-request]]
=== Customizing the Initial HTTP Request

Sometimes you need to add custom cookies, or other HTTP headers, or specify a WebSocket sub-protocol to the HTTP request that initiates the WebSocket communication.

You can do this by using overloaded versions of the `WebSocketClient.connect(...)` method:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/websocket/WebSocketClientDocs.java[tags=customHTTPRequest]
----

[[connect-inspect-http-response]]
=== Inspecting the Initial HTTP Response

If you want to inspect the HTTP response returned by the server as a reply to the  HTTP request that initiates the WebSocket communication, you may provide a `JettyUpgradeListener`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/websocket/WebSocketClientDocs.java[tags=inspectHTTPResponse]
----

[[architecture]]
== Jetty WebSocket Architecture

The Jetty WebSocket architecture is organized around the concept of a logical _connection_ between the client and the server.

The connection may be physical, when connecting to the server using HTTP/1.1, as the WebSocket bytes are carried directly by the TCP connection.

The connection may be virtual, when connecting to the server using HTTP/2, as the WebSocket bytes are wrapped into HTTP/2 `DATA` frames of an HTTP/2 stream.
In this case, a single TCP connection may carry several WebSocket virtual connections, each wrapped in its own HTTP/2 stream.

Each side of a WebSocket connection, either client or server, is made of two entities:

* A <<endpoints,WebSocket _endpoint_>>, the entity that _receives_ WebSocket events.
* A <<session,WebSocket _session_>>, the entity that offers an API to _send_ WebSocket data (and to close the WebSocket connection), as well as to configure WebSocket connection parameters.

[[endpoints]]
== WebSocket Endpoints

A WebSocket endpoint is the entity that receives WebSocket events.

The WebSocket events are the following:

* The _open_ event.
This event is emitted when the WebSocket communication has been successfully established.
Applications interested in the open event receive the WebSocket _session_ so that they can use it to send data to the remote peer.
* The _close_ event.
This event is emitted when the WebSocket communication has been closed.
Applications interested in the close event receive a WebSocket status code and an optional close reason message.
* The _error_ event.
This event is emitted when the WebSocket communication encounters a fatal error, such as an I/O error (for example, the network connection has been broken), or a protocol error (for example, the remote peer sends an invalid WebSocket frame).
Applications interested in the error event receive a `Throwable` that represent the error.
* The _frame_ events.
The frame events are emitted when a WebSocket frame is received, either a control frame such as PING, PONG or CLOSE, or a data frame such as BINARY or TEXT.
One or more data frames of the same type define a _message_.
* The _message_ events.
The message event are emitted when a WebSocket message is received.
The message event can be of two types:
** TEXT.
Applications interested in this type of messages receive a `String` representing the UTF-8 bytes received.
** BINARY.
Applications interested in this type of messages receive a `ByteBuffer` representing the raw bytes received.

<<endpoints-listener,Listener endpoints>> are notified of events by invoking the correspondent method defined by the `org.eclipse.jetty.websocket.api.Session.Listener` interface.

<<endpoints-annotated,Annotated endpoints>> are notified of events by invoking the correspondent method annotated with the correspondent annotation from the `+org.eclipse.jetty.websocket.api.annotations.*+` package.

Jetty uses ``MethodHandle``s to instantiate WebSocket endpoints and invoke WebSocket event methods, so WebSocket endpoint classes and WebSocket event methods must be `public`.

When using JPMS, your classes must be `public` and must be exported using the `exports` directive in your `module-info.java`.
It is not recommended to use the `opens` directive in your `module-info.java` for your classes, as it would expose your classes to deep reflection, which is unnecessary, as the `exports` directive is sufficient.

This guarantees that WebSocket endpoints can be accessed by the Jetty implementation without additional configuration, no matter whether you are using only the class-path, or the module-path.

For both types of WebSocket endpoints, only one thread at a time will be delivering frame or message events to the corresponding methods; the next frame or message event will not be delivered until the previous call to the corresponding method has exited, and if there is <<endpoints-demand,demand>> for it.
Endpoints will always be notified of message events in the same order they were received over the network.

[[endpoints-demand]]
=== WebSocket Events Demand

In order to receive WebSocket events, you must _demand_ for them; the only exception is the _open_ event, because it is the initial event that applications can interact with.

When a WebSocket event is received by an endpoint, the demand for WebSocket events (for that endpoint) is reset, so that no more WebSocket events will be received by the endpoint.
It is responsibility of the endpoint to demand to receive more WebSocket events.

For simple cases, you can just annotate your WebSocket endpoint with `@WebSocket(autoDemand = true)`, or implement `Session.Listener.AutoDemanding`.
In these two cases, when a method that receives a WebSocket event returns, the Jetty implementation automatically demands for another WebSocket event.

For example:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/WebSocketDocs.java[tags=autoDemand]
----

While auto-demand works for simple cases, it may not work in all cases, especially those where the method that receives the WebSocket event performs asynchronous operations.

The following example shows the problem:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/WebSocketDocs.java[tags=autoDemandWrong]
----

Note how, in the example above, auto-demanding has the problem that receiving WebSocket text messages may happen faster than echoing them back, because the call to `sendText(\...)` may return almost immediately but be slow to complete because it is asynchronous.

In the example above, if another WebSocket text message arrives, and the `sendText(\...)` operation is not complete, a `WritePendingException` will be thrown.

In other cases, this may lead to infinite buffering of data, eventually causing ``OutOfMemoryError``s, and in general excessive resource consumption that may be difficult to diagnose and troubleshoot.

For more information, see also the <<session-send,section about sending data>>.

[CAUTION]
====
Always be careful when using auto-demand.

Analyze the operations that your endpoint performs and make sure they complete synchronously within the method.
====

To solve the problem outlined above, you must explicitly demand for the next WebSocket event, only when the processing of the previous events is complete.

For example:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/WebSocketDocs.java[tags=explicitDemand]
----

Note how it is necessary to invoke `Session.demand()` from the _open_ event, in order to receive _message_ events.

Furthermore, note how every time a text message is received, a possibly slow asynchronous operation is initiated (which returns almost immediately, although it may not be completed yet) and then the method returns.

Because there is no demand when the method returns (because the asynchronous operation is not completed yet), the implementation will not notify any other WebSocket event (not even _frame_, _close_ or _error_ events).

When the asynchronous operation completes successfully the callback is notified; this, in turn, invokes `Session.demand()`, and the implementation may notify another WebSocket event (if any) to the WebSocket endpoint.

[[endpoints-listener]]
=== Listener Endpoints

A WebSocket endpoint may implement the `org.eclipse.jetty.websocket.api.Session.Listener` interface to receive WebSocket events:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/WebSocketDocs.java[tags=listenerEndpoint]
----

==== Message Streaming Reads

If you need to deal with large WebSocket messages, you may reduce the memory usage by streaming the message content.
For large WebSocket messages, the memory usage may be large due to the fact that the text or the bytes must be accumulated until the message is complete before delivering the message event.

To stream textual or binary messages, you override either `org.eclipse.jetty.websocket.api.Session.Listener.onWebSocketPartialText(\...)` or `org.eclipse.jetty.websocket.api.Session.Listener.onWebSocketPartialBinary(\...)`.

These methods receive _chunks_ of, respectively, text and bytes that form the whole WebSocket message.

You may accumulate the chunks yourself, or process each chunk as it arrives, or stream the chunks elsewhere, for example:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/WebSocketDocs.java[tags=streamingListenerEndpoint]
----

[[endpoints-annotated]]
=== Annotated Endpoints

A WebSocket endpoint may annotate methods with `+org.eclipse.jetty.websocket.api.annotations.*+` annotations to receive WebSocket events.

Each annotated event method may take an optional `Session` argument as its first parameter:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/WebSocketDocs.java[tags=annotatedEndpoint]
----
<1> Use the `@WebSocket` annotation at the class level to make it a WebSocket endpoint, and disable auto-demand.
<2> Use the `@OnWebSocketOpen` annotation for the _open_ event.
As this is the first event notified to the endpoint, you can configure the `Session` object.
<3> Use the `@OnWebSocketMessage` annotation for the _message_ event, both for textual and binary messages.
<4> Use the `@OnWebSocketError` annotation for the _error_ event.
<5> Use the `@OnWebSocketClose` annotation for the _close_ event.

[[endpoints-annotated-streaming]]
==== Message Streaming Reads

If you need to deal with large WebSocket messages, you may reduce the memory usage by streaming the message content.

To stream textual or binary messages, you still use the `@OnWebSocketMessage` annotation, but you change the signature of the method to take an additional `boolean` parameter:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/WebSocketDocs.java[tags=partialAnnotatedEndpoint]
----

Alternatively, but less efficiently, you can use the `@OnWebSocketMessage` annotation, but you change the signature of the method to take, respectively, a `Reader` and an `InputStream`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/WebSocketDocs.java[tags=streamingAnnotatedEndpoint]
----

[CAUTION]
====
`Reader` or `InputStream` only offer blocking APIs, so if the remote peers are slow in sending the large WebSocket messages, reading threads may be blocked in `Reader.read(char[])` or `InputStream.read(byte[])`, possibly exhausting the thread pool.
====

Note that when you use blocking APIs, the invocations to `Session.demand()` are now performed by the `Reader` or `InputStream` implementations (as well as the `ByteBuffer` lifecycle management).
You indirectly control the demand by deciding when to read from `Reader` or `InputStream`.

[[session]]
== WebSocket Session

A WebSocket session is the entity that offers an API to send data to the remote peer, to close the WebSocket connection, and to configure WebSocket connection parameters.

[[session-configure]]
=== Configuring the Session

You may configure the WebSocket session behavior using the `org.eclipse.jetty.websocket.api.Session` APIs.
You want to do this as soon as you have access to the `Session` object, typically from the <<endpoints,_open_ event>> handler:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/WebSocketDocs.java[tags=sessionConfigure]
----

The settings that can be configured include:

maxBinaryMessageSize::
the maximum size in bytes of a binary message (which may be composed of multiple frames) that can be received.

maxTextMessageSize::
the maximum size in bytes of a text message (which may be composed of multiple frames) that can be received.

maxFrameSize::
the maximum payload size in bytes of any WebSocket frame that can be received.

inputBufferSize::
the input (read from network/transport layer) buffer size in bytes; it has no relationship with the WebSocket frame size or message size.

outputBufferSize::
the output (write to network/transport layer) buffer size in bytes; it has no relationship to the WebSocket frame size or message size.

autoFragment::
whether WebSocket frames are automatically fragmented to respect the maximum frame size.

idleTimeout::
the duration that a WebSocket connection may remain idle (that is, there is no network traffic, neither in read nor in write) before being closed by the implementation.

Please refer to the `Session` link:{javadoc-url}/org/eclipse/jetty/websocket/api/Session.html[javadocs] for the complete list of configuration APIs.

[[session-send]]
=== Sending Data

To send data to the remote peer, you can use the non-blocking APIs offered by `Session`.

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/WebSocketDocs.java[tags=sendNonBlocking]
----
<1> Non-blocking APIs require a `Callback` parameter.
<2> Note how the second send must be performed from inside the callback.
<3> Sequential sends may throw `WritePendingException`.

[IMPORTANT]
====
Non-blocking APIs are more difficult to use since you are required to meet the following condition:

* You cannot initiate another send of any kind until the previous send is completed.

For example, if you have initiated a text send, you cannot initiate another text or binary send, until the previous send has completed.
====

This requirement is necessary to avoid unbounded buffering that could lead to ``OutOfMemoryError``s.

[CAUTION]
====
We strongly recommend that you follow the condition above.

However, there may be cases where you want to explicitly control the number of outgoing buffered messages using `RemoteEndpoint.setMaxOutgoingFrames(int)`.

Remember that trying to control the number of outgoing frames is very difficult and tricky; you may set `maxOutgoingFrames=4` and have a situation where 6 threads try to concurrently send messages: threads 1 to 4 will be able to successfully buffer their messages, thread 5 may fail, but thread 6 may succeed because one of the previous threads completed its send.
At this point you have an out-of-order message delivery that could be unexpected and very difficult to troubleshoot because it will happen non-deterministically.
====

While non-blocking APIs are more difficult to use, they don't block the sender thread and therefore use less resources, which in turn typically allows for greater scalability under load: with respect to blocking APIs, non-blocking APIs need less resources to cope with the same load.

[[session-send-stream]]
==== Streaming Send APIs

If you need to send large WebSocket messages, you may reduce the memory usage by streaming the message content.

The Jetty WebSocket APIs offer `sendPartial*(\...)` methods that allow you to send a chunk of the whole message at a time, therefore reducing the memory usage since it is not necessary to have the whole message `String` or `ByteBuffer` in memory to send it.

The Jetty WebSocket APIs for streaming the message content are non-blocking and therefore you should wait (without blocking!) for the callbacks to complete.

Fortunately, Jetty provides the `IteratingCallback` utility class (described in more details xref:arch/io.adoc#echo[in this section]) which greatly simplify the use of non-blocking APIs:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/WebSocketDocs.java[tags=streamSendNonBlocking]
----
<1> Implementing `Callback` allows to pass `this` to `sendPartialBinary(\...)`.
<2> The `process()` method is called iteratively when each `sendPartialBinary(\...)` is completed.
<3> Sends the message chunks.
<4> When the last chunk as been sent, complete successfully the `IteratingCallback`.
<5> Only when the `IteratingCallback` is completed successfully, demand for more WebSocket events.

[[session-ping]]
=== Sending Ping/Pong

The WebSocket protocol defines two special frame, named `PING` and `PONG` that may be interesting to applications for these use cases:

* Calculate the round-trip time with the remote peer.
* Keep the connection from being closed due to idle timeout -- a heartbeat-like mechanism.

To handle `PING`/`PONG` events, you may implement methods `Session.Listener.onWebSocketPing(ByteBuffer)` and/or `Session.Listener.onWebSocketPong(ByteBuffer)`.

[NOTE]
====
`PING`/`PONG` events are also supported when using annotations via the `OnWebSocketFrame` annotation.
====

`PING` frames may contain opaque application bytes, and the WebSocket implementation replies to them with a `PONG` frame containing the same bytes:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/WebSocketDocs.java[tags=pingPongListener]
----

[[session-close]]
=== Closing the Session

When you want to terminate the communication with the remote peer, you close the `Session`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/WebSocketDocs.java[tags=sessionClose]
----

Closing a WebSocket `Session` carries a status code and a reason message that the remote peer can inspect in the _close_ event handler (see <<endpoints,this section>>).

[NOTE]
====
The reason message is optional, and may be truncated to fit into the WebSocket frame sent to the client.
It is best to use short tokens such as `"shutdown"`, or `"idle_timeout"`, etc. or even application specific codes such as `"0001"` or `"00AF"` that can be converted by the application into more meaningful messages.
====

---

### Documentation Jetty Modules Programming Guide Pages Maven Jetty Jetty Jspc Maven Plugin (`documentation/jetty/modules/programming-guide/pages/maven-jetty/jetty-jspc-maven-plugin.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Jetty Jspc Maven Plugin

This plugin will pre-compile your JSP and works in conjunction with the Maven war plugin to put them inside an assembled war.

[[jspc-config]]
== Configuration

Here's the basic setup required to put the JSPC plugin into your build for the Jakarta EE {ee-current} environment:

[,xml,subs=attributes+]
----
<plugin>
  <groupId>org.eclipse.jetty.{ee-current}</groupId>
   <artifactId>jetty-{ee-current}-jspc-maven-plugin</artifactId>
   <version>{jetty-version}</version>
   <executions>
     <execution>
       <id>jspc</id>
       <goals>
         <goal>jspc</goal>
       </goals>
       <configuration>
       </configuration>
     </execution>
   </executions>
 </plugin>
----

The configurable parameters are as follows:

webXmlFragment::
Default value: `${project.basedir}/target/webfrag.xml`
+
File into which to generate the servlet declarations.
Will be merged with an existing `web.xml`.
webAppSourceDirectory::
Default value: `${project.basedir}/src/main/webapp`
+
Root of resources directory where jsps, tags etc are located.
webXml::
Default value: `${project.basedir}/src/main/webapp/WEB-INF/web.xml`
+
The web.xml file to use to merge with the generated fragments.
includes::
Default value: `+**/*.jsp, **/*.jspx+`
+
The comma separated list of patterns for file extensions to be processed.
excludes::
Default value: `+**/.svn/**+`
+
The comma separated list of patterns for file extensions to be skipped.
classesDirectory::
Default value: `${project.build.outputDirectory}`
+
Location of classes for the webapp.
generatedClasses::
Default value: `${project.build.outputDirectory}`
+
Location to put the generated classes for the jsps.
insertionMarker::
Default value: _none_
+
A marker string in the src `web.xml` file which indicates where to merge in the generated web.xml fragment.
Note that the marker string will NOT be preserved during the insertion.
Can be left blank, in which case the generated fragment is inserted just before the line containing `</web-app>`.
useProvidedScope::
Default value: false
+
If true, jars of dependencies marked with <scope>provided</scope> will be placed on the compilation classpath.
mergeFragment::
Default value: true
+
Whether or not to merge the generated fragment file with the source web.xml.
The merged file will go into the same directory as the webXmlFragment.
keepSources::
Default value: false
+
If true, the generated .java files are not deleted at the end of processing.
scanAllDirectories::
Default value: true
+
Determines if dirs on the classpath should be scanned as well as jars.
If true, this allows scanning for tlds of dependent projects that
are in the reactor as unassembled jars.
scanManifest::
Default value: true
+
Determines if the manifest of JAR files found on the classpath should be scanned.
sourceVersion::
Java version of jsp source files.
The default value depends on the version of the `jetty-{ee-current}-jspc-maven-plugin`.

targetVersion::
Java version of class files generated from jsps.
The default value depends on the version of the `jetty-{ee-current}-jspc-maven-plugin`.

tldJarNamePatterns::
Default value: `+.*taglibs[^/]*\.jar|.*jstl[^/]*\.jar$+`
+
Patterns of jars on the 'system' (ie container) path that contain tlds.
Use | to separate each pattern.
jspc::
Default value: the `org.apache.jasper.JspC` instance being configured.
+
The JspC class actually performs the pre-compilation.
All setters on the JspC class are available.

Taking all the default settings, here's how to configure the war plugin to use the generated `web.xml` that includes all of the jsp servlet declarations:

[,xml]
----
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <configuration>
    <webXml>${project.basedir}/target/web.xml</webXml>
  </configuration>
</plugin>
----

[[jspc-production-precompile]]
== Precompiling only for Production Build

As compiling jsps is usually done during preparation for a production release and not usually done during development, it is more convenient to put the plugin setup inside a <profile> which which can be deliberately invoked during prep for production.

For example, the following profile will only be invoked if the flag `-Dprod` is present on the run line:

[,xml,subs=attributes+]
----
<profiles>
  <profile>
    <id>prod</id>
    <activation>
      <property><name>prod</name></property>
    </activation>
    <build>
    <plugins>
      <plugin>
        <groupId>org.eclipse.jetty.{ee-current}</groupId>
        <artifactId>jetty-{ee-current}-jspc-maven-plugin</artifactId>
        <version>{jetty-version}</version>
        
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        
      </plugin>
    </plugins>
    </build>
  </profile>
</profiles>
----

The following invocation would cause your code to be compiled, the JSPs to be compiled, the `<servlet>` and ``<servlet-mapping>``s inserted in the `web.xml` and your web application assembled into a `+*.war+` file:

----
$ mvn -Dprod package
----

[[jspc-overlay-precompile]]
== Precompiling JSPs with Overlaid Wars

Precompiling jsps with an overlaid war requires a bit more configuration.
This is because you need to separate the steps of unpacking the overlaid war and then repacking the final target war so the `jetty-{ee-current}-jspc-maven-plugin` has the opportunity to access the overlaid resources.

In the following example the overlaid war will provide the `web.xml` file but the jsps will be in `src/main/webapp` (i.e. part of the project that uses the overlay).
The overlaid war file will be unpacked, the jsps compiled and their servlet definitions merged into the extracted `web.xml`, and everything packed into a war.

An example configuration of the war plugin that separates those phases into an unpack phase, and then a packing phase:

[,xml]
----
<plugin>
  <artifactId>maven-war-plugin</artifactId>
  <executions>
    <execution>
      <id>unpack</id>
      <goals><goal>exploded</goal></goals>
      <phase>generate-resources</phase>
      <configuration>
        <webappDirectory>target/foo</webappDirectory>
        <overlays>
          <overlay />
          <overlay>
            <groupId>org.eclipse.jetty.{ee-current}.demos</groupId>
            <artifactId>jetty-{ee-current}-demo-jetty-webapp</artifactId>
          </overlay>
        </overlays>
      </configuration>
    </execution>
    <execution>
      <id>pack</id>
      <goals><goal>war</goal></goals>
      <phase>package</phase>
      <configuration>
        <warSourceDirectory>target/foo</warSourceDirectory>
        <webXml>target/web.xml</webXml>
      </configuration>
    </execution>
  </executions>
</plugin>
----

Now you also need to configure the `jetty-{ee-current}-jspc-maven-plugin` so that it can use the web.xml that was extracted by the war unpacking and merge in the generated definitions of the servlets.
This is in `target/foo/WEB-INF/web.xml`.
Using the default settings, the `web.xml` merged with the jsp servlet definitions will be put into `target/web.xml`.

[,xml,subs=attributes+]
----
<plugin>
  <groupId>org.eclipse.jetty.{ee-current}</groupId>
   <artifactId>jetty-{ee-current}-jspc-maven-plugin</artifactId>
   <version>{jetty-version}</version>
   <executions>
     <execution>
       <id>jspc</id>
       <goals>
         <goal>jspc</goal>
       </goals>
       <configuration>
          <webXml>target/foo/WEB-INF/web.xml</webXml>
          <includes>**/*.foo</includes>
          <excludes>**/*.fff</excludes>
      </configuration>
    </execution>
  </executions>
</plugin>
----

---

### Documentation Jetty Modules Programming Guide Pages Maven Jetty Jetty Maven Plugin (`documentation/jetty/modules/programming-guide/pages/maven-jetty/jetty-maven-plugin.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Using the Jetty Maven Plugin

The Jetty Maven plugin is useful for rapid development and testing.
It can optionally periodically scan a project for changes and automatically redeploys the web applications if any are found.
This makes the development cycle more productive by eliminating the build and deploy steps: use an IDE to make changes to the project, and the running web container automatically picks them up, allowing them to be tested straight away.

There are 4 goals to run a web application in Jetty:

* <<jetty-run-goal,jetty:run>>
* <<jetty-run-war-goal,jetty:run-war>>
* <<jetty-start-goal,jetty:start>>
* <<jetty-start-war-goal,jetty:start-war>>

Plus two utility goals:

* <<jetty-stop-goal,jetty:stop>>
* <<jetty-effective-web-xml-goal,jetty:effective-web-xml>>

`jetty:run` and `jetty:start` are alike in that they both run an _unassembled_ web application in Jetty, however `jetty:run` is designed to be used at the command line, whereas `jetty:start` is specifically designed to be bound to execution phases in the build lifecycle.
`jetty:run` will pause Maven while jetty is running, echoing all output to the console, and then stop Maven when jetty exits.
`jetty:start` will not pause Maven, will write all its output to a file, and will not stop Maven when jetty exits.

`jetty:run-war` and `jetty:start-war` are similar in that they both run an _assembled_ war file in Jetty.
However, `jetty:run-war` is designed to be run at the command line, whereas `jetty:start-war` is specifically designed to be bound to execution phases in the build lifecycle.
`jetty:run-war` will pause Maven while Jetty is running, echoing all output to the console, and then stop Maven when Jetty exits.
`jetty:start-war` will not pause Maven, will write all its output to a file, and will not stop Maven when Jetty exits.

[IMPORTANT]
====
While the Jetty Maven Plugin can be very useful for development we do not recommend its use in a _production capacity_.
In order for the plugin to work it needs to leverage many internal Maven APIs and Maven itself it not a production deployment tool.
We recommend either the traditional xrefr:og-deploy[distribution] deployment approach or using xref:operations-guide:arch/index.adoc[embedded Jetty].
====

[[get-up-and-running]]
== Get Up and Running

Since Jetty 12, Jetty Maven plugin is repackaged for the corresponding Jakarta EE version with an `eeX` classifier in the groupId and artifactId.

First, add `jetty-{ee-all}-maven-plugin` to your `pom.xml` definition. Here's an example of how to do that for Jakarta {ee-current-caps}:

[,xml,subs=attributes+]
----
<plugin>
  <groupId>org.eclipse.jetty.{ee-current}</groupId>
  <artifactId>jetty-{ee-current}-maven-plugin</artifactId>
  <version>{jetty-version}</version>
</plugin>
----

Then, from the same directory as the project's root `pom.xml`, type:

----
mvn jetty:run
----

This starts Jetty and serves up the project on `+http://localhost:8080/+`.

Jetty will continue to run until you stop it.
By default, it will not automatically restart your webapp.
Set a non-zero `<scan>` value to have Jetty scan your web application for changes and automatically redeploy, or set `<scan>` to `0` to cause manual redeployment by hitting the kbd:[Enter] key.

Terminate the plugin with a kbd:[Ctrl+c] in the terminal window where it is running.

[NOTE]
====
The classpath of the running Jetty instance and its deployed web application are managed by Maven, and may not be exactly what you expect.
For example: a web application's dependent jars might be referenced via the local repository, or other projects in the reactor, not the `WEB-INF/lib` directory.
====

[[supported-goals]]
== Supported Goals

The goals prefixed with `"run-"` are designed to be used at the _command line_.
They first run a Maven build on your project to ensure at least the classes are all built.
They then start Jetty and pause the Maven build process until Jetty is manually terminated, at which time the build will also be terminated.
Jetty can scan various files in your project for changes and redeploy the web application as necessary, or you can choose to manually trigger a redeploy if you prefer.
All output from Jetty is echoed to the console.

The goals prefixed with `"start-"` are designed to be used with _build lifecycle bindings in the pom_, and _not_ at the command line.
No part of your project will be rebuilt by invoking these goals - you should ensure that your bind the execution to a build phase where all necessary parts of your project have been built.
Maven will start and terminate Jetty at the appropriate points in the build lifecycle, continuing with the build.
Jetty will _not_ scan any files in your project for changes, and your web application will _not_ be redeployed either automatically or manually.
Output from Jetty is directed to a file in the `target` directory.

To see a list of all goals supported by the Jetty Maven plugin, do:

----
mvn jetty:help
----

To see the detailed list of parameters that can be configured for a particular goal, in addition to its description, do:

----
mvn jetty:help -Ddetail=true -Dgoal=<goalName>
----

[[deployment-modes]]
== Deployment Modes
All of the `"run-"` and `"start-"` goals can deploy your web application either into the running maven process, or forked into a new child process, or forked into a Jetty distribution on disk.

This is controlled by setting the `deployMode` configuration parameter in the pom, but can also be set by defining the Maven property 'jetty.deployMode'.

=== Embedded

`deployMode` of `EMBED`.
This is the "classic" Jetty Maven plugin deployment mode, running in-process with Maven.
This is the _default_ mode.

These extra configuration parameters are available:

httpConnector::
Optional.
Note that to configure a https connector, you will need to use xml configuration files instead, setting the `jettyXmls` parameter.
This parameter can only be used to configure a standard http connector.
If not specified, Jetty will create a link:{javadoc-url}/org/eclipse/jetty/server/ServerConnector.html[ServerConnector] instance listening on port 8080.
You can change this default port number by using the system property `jetty.http.port` on the command line, for example, `mvn -Djetty.http.port=9999 jetty:run`.
Alternatively, you can use this configuration element to set up the information for the ServerConnector.
The following are the valid configuration sub-elements:
port:::
The port number for the connector to listen on.
By default it is 8080.
host:::
The particular interface for the connector to listen on.
By default, all interfaces.
name:::
The name of the connector, which is useful for configuring contexts to respond only on particular connectors.
idleTimeout:::
Maximum idle time for a connection.
You could instead configure the connectors in a standard xref:operations-guide:xml/index.adoc[jetty xml config file] and put its location into the `jettyXml` parameter.
Note that since Jetty 9.0 it is no longer possible to configure a https connector directly in the pom.xml: you need to use jetty xml config files to do it.
loginServices::
Optional.
A list of `org.eclipse.jetty.security.LoginService` implementations. Note that there is no default realm.
If you use a realm in your `web.xml` you can specify a corresponding realm here.
You could instead configure the login services in a jetty xml file and add its location to the `jettyXml` parameter.
See <<configuring-security-settings,Configuring Security>>.
requestLog::
Optional.
An implementation of the `org.eclipse.jetty.server.RequestLog` request log interface.
There are three other ways to configure the `RequestLog`:
+
  * In a Jetty xml config file, as specified in the `jettyXml` parameter.
  * In a context xml config file, as specified in the `contextXml` parameter.
  * In the `webApp` element.
+
See xref:server/http.adoc#request-logging[Configuring Request Logs] for more information.
server::
Optional as of Jetty 9.3.1.
This would configure an instance of `org.eclipse.jetty.server.Server` for the plugin to use, however it is usually _not_ necessary to configure this, as the plugin will automatically configure one for you.
In particular, if you use the `jettyXmls` element, then you generally _don't_ want to define this element, as you are probably using the `jettyXmls` file/s to configure up a Server with a special constructor argument, such as a custom threadpool.
If you define both a `server` element and use a `jettyXmls` element which points to a config file that has a line like `<Configure id="Server" class="org.eclipse.jetty.server.Server">` then the the xml configuration will override what you configure for the `server` in the `pom.xml`.
useProvidedScope::
Default value is `false`.
If true, the dependencies with `<scope>provided</scope>` are placed onto the __container classpath__.
Be aware that this is _not_ the web application class-path, as `provided` indicates that these dependencies would normally be expected to be provided by the container.
You should very rarely ever need to use this.
See <<container-classpath,Container Classpath vs Web Application Classpath>>.

=== Forked

`deployMode` of `FORK`.
This is similar to the old "jetty:run-forked" goal - a separate process is forked to run your web application embedded into jetty.
These extra configuration parameters are available:

env::
Optional.
Map of key/value pairs to pass as environment to the forked JVM.
jvmArgs::
Optional.
A space separated string representing arbitrary arguments to pass to the forked JVM.
forkWebXml::
Optional.
Defaults to `target/fork-web.xml`.
This is the location of a quickstart web xml file that will be _generated_ during the forking of the jetty process.
You should not need to set this parameter, but it is available if you wish to control the name and location of that file.
useProvidedScope::
Default value is `false`.
If true, the dependencies with `<scope>provided</scope>` are placed onto the __container classpath__.
Be aware that this is NOT the web application class-path, as "provided" indicates that these dependencies would normally be expected to be provided by the container.
You should very rarely ever need to use this.
See <<container-classpath,Container Classpath vs Web Application Classpath>>.

=== In a jetty distribution

`deployMode` of `EXTERNAL`.
This is similar to the old "jetty:run-distro" goal - your web application is deployed into a dynamically downloaded, unpacked and configured Jetty distribution.
A separate process is forked to run it.
These extra configuration parameters are available:

jettyBase::
Optional.
The location of an existing Jetty base directory to use to deploy the web application.
The existing base will be copied to the `target/` directory before the web application is deployed.
If there is no existing jetty base, a fresh one will be made in `target/jetty-base`.
jettyHome::
Optional.
The location of an existing unpacked Jetty distribution.
If one does not exist, a fresh Jetty distribution will be downloaded from Maven and installed to the `target` directory.
jettyOptions::
Optional.
A space separated string representing extra arguments to the synthesized Jetty command line.
Values for these arguments can be found in the section titled "Options" in the output of `java -jar $jetty.home/start.jar --help`.
jvmArgs::
Optional.
A space separated string representing arguments that should be passed to the jvm of the child process running the distro.
modules::
Optional.
An array of names of additional Jetty modules that the Jetty child process will activate.
Use this to change the <<container-classpath,container classpath>> instead of `useProvidedScope`.
These modules are enabled by default: `http,{ee-current}-webapp,{ee-current}-deploy`.

[[common-configuration]]
== Common Configuration

The following configuration parameters are common to all of the `"run-"` and `"start-"` goals:

deployMode::
One of `EMBED`, `FORK` or `EXTERNAL`.
Default `EMBED`.
Can also be configured by setting the Maven property `jetty.deployMode`.
This parameter determines whether the web application will run in Jetty in-process with Maven, forked into a new process, or deployed into a Jetty distribution.
See <<deployment-modes,Deployment Modes>>.
jettyXmls::
Optional.
A comma separated list of locations of Jetty xml files to apply in addition to any plugin configuration parameters.
You might use it if you have other web applications, handlers, specific types of connectors etc., to deploy, or if you have other Jetty objects that you cannot configure from the plugin.
skip::
Default is false.
If true, the execution of the plugin exits.
Same as setting the SystemProperty `-Djetty.skip` on the command line.
This is most useful when configuring Jetty for execution during integration testing and you want to skip the tests.
excludedGoals::
Optional.
A list of Jetty plugin goal names that will cause the plugin to print an informative message and exit.
Useful if you want to prevent users from executing goals that you know cannot work with your project.
supportedPackagings::
Optional.
Defaults to `war`.
This is a list of maven &lt;packaging&gt; types that can work with the jetty plugin.
Usually, only `war` projects are suitable, however, you may configure other types.
The plugin will refuse to start if the &lt;packaging&gt; type in the pom is not in list of `supportedPackagings`.
systemProperties::
Optional.
Allows you to configure System properties for the execution of the plugin.
For more information, see <<setting-system-properties,Setting System Properties>>.
systemPropertiesFile::
Optional.
A file containing System properties to set for the execution of the plugin.
By default, settings that you make here *do not* override any system properties already set on the command line, by the JVM, or in the POM via `systemProperties`.
Read <<setting-system-properties,Setting System Properties>> for how to force overrides.
jettyProperties::
Optional.
A map of property name, value pairs.
Allows you to configure standard jetty properties.

[[container-classpath]]
== Container Classpath vs Web Application Classpath

The Servlet Specification makes a strong distinction between the class-path for a _web application_, and the classpath of the _container_.
When running in Maven, the plugin's class-path is equivalent to the _container_ class-path.
It will make a class-path for the _web application_ to be deployed comprised of `<dependencies>` specified in the pom.

If your production environment places specific jars onto the container's classpath, the equivalent way to do this with Maven is to define these as &lt;dependencies&gt; for the _plugin_ itself, not the _project_. See http://maven.apache.org/pom.html#Plugins[configuring maven plugins].
This is suitable if you are using either `EMBED` or `FORK` mode.
If you are using `EXTERNAL` mode, then you should configure the `modules` parameter with the names of the Jetty modules that place these jars onto the container classpath.

Note that in `EMBED` or `FORK` mode, you could also influence the container classpath by setting the `useProvidedScope` parameter to `true`: this will place any dependencies with &lt;scope&gt;provided&lt;scope&gt; onto the plugin's classpath.
Use this very cautiously: as the plugin already automatically places most Jetty jars onto the classpath, you could wind up with duplicate jars.


[[jetty-run-goal]]
== jetty:run

The `run` goal deploys a web application that is _not_ first built into a WAR.
A virtual web application is constructed from the project's sources and its dependencies.
It looks for the constituent parts of a web application in the Maven default project locations, although you can override these in the plugin configuration.
For example, by default it looks for:

* resources in `${project.basedir}/src/main/webapp`
* classes in `${project.build.outputDirectory}`
* `web.xml` in `${project.basedir}/src/main/webapp/WEB-INF/`

The plugin first runs a Maven parallel build to ensure that the classes are built and up-to-date before deployment.
If you change the source of a class and your IDE automatically compiles it in the background, the plugin picks up the changed class (note you need to configure a non-zero `scan` interval for automatic redeployment).

If the plugin is invoked in a multi-module build, any dependencies that are also in the Maven reactor are used from their compiled classes.

Once invoked, you can configure the plugin to run continuously, scanning for changes in the project and automatically performing a hot redeploy when necessary.
Any changes you make are immediately reflected in the running instance of Jetty, letting you quickly jump from coding to testing, rather than going through the cycle of: code, compile, reassemble, redeploy, test.

The Maven build will be paused until Jetty exits, at which time Maven will also exit.

Stopping Jetty is accomplished by typing `cntrl-c` at the command line.

Output from Jetty will be logged to the console.

Here is an example, which turns on scanning for changes every ten seconds, and sets the web application context path to `/test`:

[,xml,subs=attributes+]
----
<plugin>
  <groupId>org.eclipse.jetty.{ee-current}</groupId>
  <artifactId>jetty-{ee-current}-maven-plugin</artifactId>
  <version>{jetty-version}</version>
  <configuration>
    <scan>10</scan>
    <webApp>
      <contextPath>/test</contextPath>
    </webApp>
  </configuration>
</plugin>
----

=== Configuration

webApp::
This is an instance of link:{javadoc-url}/org/eclipse/jetty/{ee-current}}/maven/plugin/MavenWebAppContext.html[org.eclipse.jetty.{ee-current}.maven.plugin.MavenWebAppContext], which is an extension to the class  link:{javadoc-url}/org/eclipse/jetty/{ee-current}/webapp/WebAppContext.hml[`org.eclipse.jetty.{ee-current}.webapp.WebAppContext`].
You can use any of the setter methods on this object to configure your web application.
Here are a few of the most useful ones:
+
contextPath::
The context path for your web application.
By default, this is set to `/`.
If using a custom value for this parameter, you should include the leading `/`, example `/mycontext`.
descriptor::
The path to the `web.xml` file for your web application.
By default, the plugin will look in `src/main/webapp/WEB-INF/web.xml`.
defaultsDescriptor::
The path to a `webdefault.xml` file that will be applied to your web application before the `web.xml`.
If you don't supply one, Jetty uses a default file baked into the `jetty-{ee-current}-webapp-{jetty-version}.jar`.
overrideDescriptor::
The path to a `web.xml` file that Jetty applies after reading your `web.xml`.
You can use this to replace or add configuration.
jettyEnvXml::
Optional.
Location of a `jetty-{ee-current}-env.xml` file, which allows you to make JNDI bindings that satisfy `env-entry`, `resource-env-ref`, and `resource-ref` linkages in the `web.xml` that are scoped  only to the web application and not shared with other web applications that you might be deploying at the same time (for example, by using a `jettyXml` file).
tempDirectory::
The path to a dir that Jetty can use to expand or copy jars and jsp compiles when your web application is running.
The default is `${project.build.outputDirectory}/tmp`.
baseResource::
The path from which Jetty serves static resources.
Defaults to `src/main/webapp`.
If this location does not exist (because, for example, your project does not use static content), then the plugin will synthesize a virtual static resource location of `target/webapp-synth`.
resourceBases::
Use instead of `baseResource` if you have multiple directories from which you want to serve static content.
This is an array of directory locations, either as urls or file paths.
baseAppFirst::
Defaults to "true".
Controls whether any overlaid wars are added before or after the original base resource(s) of the web application.
See the section on <<using-overlaid-wars,overlaid wars>> for more information.
containerIncludeJarPattern::
Defaults to `.*/jetty-jakarta-servlet-api-[^/]*\.jar$|.*jakarta.servlet.jsp.jstl-[^/]*\.jar|.*taglibs-standard-impl-.*\.jar`.
This is a pattern that is applied to the names of the jars on the container's classpath (ie the classpath of the plugin, not that of the web application) that should be scanned for fragments, tlds, annotations etc.
This is analogous to the context attribute xref:operations-guide:annotations/index.adoc#og-container-include-jar-pattern[org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern] that is documented xref:operations-guide:annotations/index.adoc#og-container-include-jar-pattern[here].
You can define extra patterns of jars that will be included in the scan.
webInfIncludeJarPattern::
Defaults to matching _all_ the dependency jars for the web application (ie the equivalent of WEB-INF/lib).
You can make this pattern more restrictive to only match certain jars by using this setter.
This is analogous to the context attribute xref:operations-guide:annotations/index.adoc#og-web-inf-include-jar-pattern[org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern] that is documented xref:operations-guide:annotations/index.adoc#og-web-inf-include-jar-pattern[here].
contextXml::
The path to a context xml file that is applied to your web application AFTER the `webApp` element.
classesDirectory::
Location of your compiled classes for the web application.
You should rarely need to set this parameter.
Instead, you should set `<build><outputDirectory>` in your `pom.xml`.
testClassesDirectory::
Location of the compiled test classes for your web application.
By default this is `${project.build.testOutputDirectory}`.
useTestScope::
If true, the classes from `testClassesDirectory` and dependencies of scope "test" are placed first on the classpath.
By default this is false.
scan::
The pause in seconds between sweeps of the web application to check for changes and automatically hot redeploy if any are detected.
*By default this is `-1`, which disables hot redeployment scanning.*
A value of `0` means no hot redeployment is done, and that you must use the kbd:[Enter] key to manually force a redeploy.
Any positive integer will enable hot redeployment, using the number as the sweep interval in seconds.
scanTargetPatterns::
Optional.
List of extra directories with glob-style include/excludes patterns (see http://docs.oracle.com/javase/8/docs/api/java/nio/file/FileSystem.html#getPathMatcher-java.lang.String-[javadoc] for http://docs.oracle.com/javase/8/docs/api/java/nio/file/FileSystem.html#getPathMatcher-java.lang.String-[FileSystem.getPathMatcher]) to specify other files to periodically scan for changes.
scanClassesPattern::
Optional.
Include and exclude patterns that can be applied to the classesDirectory for the purposes of scanning, it does *not* affect the classpath.
If a file or directory is excluded by the patterns then a change in that file (or subtree in the case of a directory) is ignored and will not cause the web application to redeploy.
Patterns are specified as a relative path using a glob-like syntax as described in the http://docs.oracle.com/javase/8/docs/api/java/nio/file/FileSystem.html#getPathMatcher-java.lang.String-[javadoc] for http://docs.oracle.com/javase/8/docs/api/java/nio/file/FileSystem.html#getPathMatcher-java.lang.String-[FileSystem.getPathMatcher].
scanTestClassesPattern::
Optional.
Include and exclude patterns that can be applied to the testClassesDirectory for the purposes of scanning, it does *not* affect the classpath.
If a file or directory is excluded by the patterns then a change in that file (or subtree in the case of a directory) is ignored and will not cause the web application to redeploy.
Patterns are specified as a relative path using a glob-like syntax as described in the http://docs.oracle.com/javase/8/docs/api/java/nio/file/FileSystem.html#getPathMatcher-java.lang.String-[javadoc] for http://docs.oracle.com/javase/8/docs/api/java/nio/file/FileSystem.html#getPathMatcher-java.lang.String-[FileSystem.getPathMatcher].

See <<deployment-modes,Deployment Modes>> for other configuration parameters available when using the `run` goal in EMBED, FORK or EXTERNAL modes.

Here is an example of a pom configuration for the plugin with the `run` goal:

[,xml,subs=attributes+]
----
<project>
  ...
  <plugins>
    ...
    <plugin>
      <groupId>org.eclipse.jetty.{ee-current}</groupId>
      <artifactId>jetty-{ee-current}-maven-plugin</artifactId>
      <version>{jetty-version}</version>
      <configuration>
        <webApp>
          <contextPath>/</contextPath>
          <descriptor>${project.basedir}/src/over/here/web.xml</descriptor>
          <jettyEnvXml>${project.basedir}/src/over/here/jetty-env.xml</jettyEnvXml>
          <baseResource>${project.basedir}/src/staticfiles</baseResource>
        </webApp>
        <classesDirectory>${project.basedir}/somewhere/else</classesDirectory>
        <scanClassesPattern>
          <excludes>
             <exclude>**/Foo.class</exclude>
          </excludes>
        </scanClassesPattern>
        <scanTargetPatterns>
          <scanTargetPattern>
            <directory>src/other-resources</directory>
            <includes>
              <include>**/*.xml</include>
              <include>**/*.properties</include>
            </includes>
            <excludes>
              <exclude>**/myspecial.xml</exclude>
              <exclude>**/myspecial.properties</exclude>
            </excludes>
          </scanTargetPattern>
        </scanTargetPatterns>
      </configuration>
    </plugin>
  </plugins>
  ...
</project>
----

If, for whatever reason, you cannot run on an unassembled web application, the goal `run-war` works on assembled web applications.

[[jetty-run-war-goal]]
== jetty:run-war

When invoked at the command line this goal first executes a maven build of your project to the package phase.

By default it then deploys the resultant war to Jetty, but you can use this goal instead to deploy _any_ war file by simply setting the `<webApp><war>` configuration parameter to its location.

If you set a non-zero `scan`, Jetty watches your `pom.xml` and the WAR file; if either changes, it redeploys the war.

The maven build is held up until Jetty exits, which is achieved by typing `cntrl-c` at the command line.

All Jetty output is directed to the console.

=== Configuration

Configuration parameters are:

webApp::
war:::
The location of the built WAR file. This defaults to `${project.build.directory}/${project.build.finalName}.war`.
You can set it to the location of any pre-built war file.
contextPath:::
The context path for your web application. By default, this is set to `/`.
If using a custom value for this parameter, you should include the leading `/`, example `/mycontext`.
defaultsDescriptor:::
The path to a `webdefault.xml` file that will be applied to your web application before the `web.xml`.
If you don't supply one, Jetty uses a default file baked into the `jetty-{ee-current}-webapp-{jetty-version}.jar`.
overrideDescriptor:::
The path to a `web.xml` file that Jetty applies after reading your `web.xml`.
You can use this to replace or add configuration.
containerIncludeJarPattern:::
Defaults to `.*/jetty-jakarta-servlet-api-[^/]*\.jar$|.*jakarta.servlet.jsp.jstl-[^/]*\.jar|.*taglibs-standard-impl-.*\.jar`.
This is a pattern that is applied to the names of the jars on the container's classpath (ie the classpath of the plugin, not that of the web application) that should be scanned for fragments, tlds, annotations etc.
This is analogous to the context attribute xref:operations-guide:annotations/index.adoc#og-container-include-jar-pattern[org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern] that is documented xref:operations-guide:annotations/index.adoc#og-container-include-jar-pattern[here].
You can define extra patterns of jars that will be included in the scan.
webInfIncludeJarPattern:::
Defaults to matching _all_ of the dependency jars for the web application (ie the equivalent of WEB-INF/lib).
You can make this pattern more restrictive to only match certain jars by using this setter.
This is analogous to the context attribute xref:operations-guide:annotations/index.adoc#og-web-inf-include-jar-pattern[org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern] that is documented xref:operations-guide:annotations/index.adoc#og-web-inf-include-jar-pattern[here].
tempDirectory:::
The path to a dir that Jetty can use to expand or copy jars and jsp compiles when your web application is running.
The default is `${project.build.outputDirectory}/tmp`.
contextXml:::
The path to a context xml file that is applied to your web application AFTER the `webApp` element.
scan::
The pause in seconds between sweeps of the web application to check for changes and automatically hot redeploy if any are detected.
*By default this is `-1`, which disables hot redeployment scanning.*
A value of `0` means no hot redeployment is done, and that you must use the kbd:[Enter] key to manually force a redeploy.
Any positive integer will enable hot redeployment, using the number as the sweep interval in seconds.
scanTargetPatterns::
Optional.
List of directories with ant-style include/excludes patterns to specify other files to periodically scan for changes.

See <<deployment-modes,Deployment Modes>> for other configuration parameters available when using the `run-war` goal in EMBED, FORK or EXTERNAL modes.

[[jetty-start-goal]]
== jetty:start

This is similar to the `jetty:run` goal, however it is _not_ designed to be run from the command line and does _not_ first execute the build up until the `test-compile` phase to ensure that all necessary classes and files of the web application have been generated.
It will _not_ scan your project for changes and restart your web application.
It does _not_ pause maven until Jetty is stopped.

Instead, it is designed to be used with build phase bindings in your pom.
For example to you can have Maven start your web application at the beginning of your tests and stop at the end.

If the plugin is invoked as part of a multi-module build, any dependencies that are also in the maven reactor are used from their compiled classes.

Here's an example of using the `pre-integration-test` and `post-integration-test` Maven build phases to trigger the execution and termination of Jetty:

[,xml,subs=attributes+]
----
<plugin>
  <groupId>org.eclipse.jetty.{ee-current}</groupId>
  <artifactId>jetty-{ee-current}-maven-plugin</artifactId>
  <version>{jetty-version}</version>
  <configuration>
    <stopKey>foo</stopKey>
    <stopPort>9999</stopPort>
  </configuration>
  <executions>
    <execution>
      <id>start-jetty</id>
      <phase>pre-integration-test</phase>
      <goals>
        <goal>start</goal>
      </goals>
    </execution>
    <execution>
      <id>stop-jetty</id>
      <phase>post-integration-test</phase>
       <goals>
         <goal>stop</goal>
       </goals>
     </execution>
  </executions>
</plugin>
----

This goal will generate output from jetty into the `target/jetty-start.out` file.

=== Configuration

These configuration parameters are available:

webApp::
This is an instance of link:{javadoc-url}/org/eclipse/jetty/{ee-current}/maven/plugin/MavenWebAppContext.html[org.eclipse.jetty.{ee-current}.maven.plugin.MavenWebAppContext], which is an extension to the class  link:{javadoc-url}/org/eclipse/jetty/webapp/WebAppContext.hml[`org.eclipse.jetty.ee9.webapp.WebAppContext`].
You can use any of the setter methods on this object to configure your web application.
Here are a few of the most useful ones:
+
contextPath::
The context path for your web application.
By default, this is set to `/`.
If using a custom value for this parameter, you should include the leading `/`, example `/mycontext`.
descriptor::
The path to the `web.xml` file for your web application.
The default is `src/main/webapp/WEB-INF/web.xml`.
defaultsDescriptor::
The path to a `webdefault.xml` file that will be applied to your web application before the `web.xml`.
If you don't supply one, Jetty uses a default file baked into the `jetty-{ee-current}-webapp-{jetty-version}.jar`.
overrideDescriptor::
The path to a `web.xml` file that Jetty applies after reading your `web.xml`.
You can use this to replace or add configuration.
jettyEnvXml::
Optional.
Location of a `jetty-env.xml` file, which allows you to make JNDI bindings that satisfy `env-entry`, `resource-env-ref`, and `resource-ref` linkages in the `web.xml` that are scoped  only to the web application and not shared with other web applications that you might be deploying at the same time (for example, by using a `jettyXml` file).
tempDirectory::
The path to a dir that Jetty can use to expand or copy jars and jsp compiles when your web application is running.
The default is `${project.build.outputDirectory}/tmp`.
baseResource::
The path from which Jetty serves static resources.
Defaults to `src/main/webapp`.
resourceBases::
Use instead of `baseResource` if you have multiple directories from which you want to serve static content.
This is an array of directory names.
baseAppFirst::
Defaults to "true".
Controls whether any overlaid wars are added before or after the original base resource(s) of the web application.
See the section on <<using-overlaid-wars,overlaid wars>> for more information.
containerIncludeJarPattern::
Defaults to `.*/jetty-jakarta-servlet-api-[^/]*\.jar$|.*jakarta.servlet.jsp.jstl-[^/]*\.jar|.*taglibs-standard-impl-.*\.jar`.
This is a pattern that is applied to the names of the jars on the container's classpath (ie the classpath of the plugin, not that of the web application) that should be scanned for fragments, tlds, annotations etc.
This is analogous to the context attribute xref:operations-guide:annotations/index.adoc#og-container-include-jar-pattern[org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern] that is documented xref:operations-guide:annotations/index.adoc#og-container-include-jar-pattern[here].
You can define extra patterns of jars that will be included in the scan.
webInfIncludeJarPattern::
Defaults to matching _all_ of the dependency jars for the web application (ie the equivalent of WEB-INF/lib).
You can make this pattern more restrictive to only match certain jars by using this setter.
This is analogous to the context attribute xref:operations-guide:annotations/index.adoc#og-web-inf-include-jar-pattern[org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern] that is documented xref:operations-guide:annotations/index.adoc#og-web-inf-include-jar-pattern[here].
contextXml::
The path to a context xml file that is applied to your web application AFTER the `webApp` element.
classesDirectory::
Location of your compiled classes for the web application.
You should rarely need to set this parameter.
Instead, you should set `build outputDirectory` in your `pom.xml`.
testClassesDirectory::
Location of the compiled test classes for your web application.
By default this is `${project.build.testOutputDirectory}`.
useTestScope::
If true, the classes from `testClassesDirectory` and dependencies of scope "test" are placed first on the classpath.
By default this is false.
stopPort::
Optional.
Port to listen on for stop commands.
Useful to use in conjunction with the <<jetty-stop-goal,stop>> and <<jetty-start-goal,start>> goals.
stopKey::
Optional.
Used in conjunction with stopPort for stopping jetty.
Useful to use in conjunction with the <<jetty-stop-goal,stop>> and <<jetty-start-goal,start>> goals.

These additional configuration parameters are available when running in `FORK` or `EXTERNAL` mode:

maxChildStartChecks::
Default is `10`.
This is maximum number of times the parent process checks to see if the forked jetty process has started correctly
maxChildStartCheckMs::
Default is `200`.
This is the time in milliseconds between checks on the startup of the forked jetty process.


[[jetty-start-war-goal]]
== jetty:start-war

Similarly to the `jetty:start` goal, `jetty:start-war` is designed to be bound to build lifecycle phases in your pom.

It will _not_ scan your project for changes and restart your web application.
It does _not_ pause maven until Jetty is stopped.

By default, if your pom is for a web application project, it will deploy the war file for the project to jetty.
However, like the `jetty:run-war` project, you can nominate any war file to deploy by defining its location in the `<webApp><war>` parameter.

If the plugin is invoked as part of a multi-module build, any dependencies that are also in the Maven reactor are used from their compiled classes.

This goal will generate output from jetty into the `target/jetty-start-war.out` file.

=== Configuration

These configuration parameters are available:

webApp::
war:::
The location of the built WAR file. This defaults to `${project.build.directory}/${project.build.finalName}.war`.
You can set it to the location of any pre-built war file.
contextPath:::
The context path for your web application. By default, this is set to `/`.
If using a custom value for this parameter, you should include the leading `/`, example `/mycontext`.
defaultsDescriptor:::
The path to a `webdefault.xml` file that will be applied to your web application before the `web.xml`.
If you don't supply one, Jetty uses a default file baked into the `jetty-{ee-current}-webapp-{jetty-version}.jar`.
overrideDescriptor:::
The path to a `web.xml` file that Jetty applies after reading your `web.xml`.
You can use this to replace or add configuration.
containerIncludeJarPattern:::
Defaults to `.*/jetty-jakarta-servlet-api-[^/]*\.jar$|.*jakarta.servlet.jsp.jstl-[^/]*\.jar|.*taglibs-standard-impl-.*\.jar`.
This is a pattern that is applied to the names of the jars on the container's classpath (ie the classpath of the plugin, not that of the web application) that should be scanned for fragments, tlds, annotations etc.
This is analogous to the context attribute xref:operations-guide:annotations/index.adoc#og-container-include-jar-pattern[org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern] that is documented xref:operations-guide:annotations/index.adoc#og-container-include-jar-pattern[here].
You can define extra patterns of jars that will be included in the scan.
webInfIncludeJarPattern:::
Defaults to matching _all_ of the dependency jars for the web application (ie the equivalent of WEB-INF/lib).
You can make this pattern more restrictive to only match certain jars by using this setter.
This is analogous to the context attribute xref:operations-guide:annotations/index.adoc#og-web-inf-include-jar-pattern[org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern] that is documented xref:operations-guide:annotations/index.adoc#og-web-inf-include-jar-pattern[here].
tempDirectory:::
The path to a dir that Jetty can use to expand or copy jars and jsp compiles when your web application is running.
The default is `${project.build.outputDirectory}/tmp`.
contextXml:::
The path to a context xml file that is applied to your web application AFTER the `webApp` element.
stopPort::
Optional.
Port to listen on for stop commands.
Useful to use in conjunction with the <<jetty-stop-goal,stop>>.
stopKey::
Optional.
Used in conjunction with stopPort for stopping jetty.
Useful to use in conjunction with the <<jetty-stop-goal,stop>>.

These additional configuration parameters are available when running in FORK or EXTERNAL mode:

maxChildStartChecks::
Default is `10`.
This is maximum number of times the parent process checks to see if the forked Jetty process has started correctly
maxChildStartCheckMs::
Default is `200`.
This is the time in milliseconds between checks on the startup of the forked Jetty process.


[[jetty-stop-goal]]
== jetty:stop

The stop goal stops a FORK or EXTERNAL mode running instance of Jetty.
To use it, you need to configure the plugin with a special port number and key.
That same port number and key will also be used by the other goals that start Jetty.

=== Configuration

stopPort::
A port number for Jetty to listen on to receive a stop command to cause it to shutdown.
stopKey::
A string value sent to the `stopPort` to validate the stop command.
stopWait::
The maximum time in seconds that the plugin will wait for confirmation that Jetty has stopped.
If false or not specified, the plugin does not wait for confirmation but exits after issuing the stop command.

Here's a configuration example:

[,xml,subs=attributes+]
----
<plugin>
  <groupId>org.eclipse.jetty.{ee-current}</groupId>
  <artifactId>jetty-{ee-current}-maven-plugin</artifactId>
  <version>{jetty-version}</version>
  <configuration>
    <stopPort>9966</stopPort>
    <stopKey>foo</stopKey>
    <stopWait>10</stopWait>
  </configuration>
</plugin>
----

Then, while Jetty is running (in another window), type:

----
mvn jetty:stop
----

The `stopPort` must be free on the machine you are running on.
If this is not the case, you will get an "Address already in use" error message after the "Started ServerConnector ..." message.

[[jetty-effective-web-xml-goal]]
== jetty:effective-web-xml

This goal calculates a synthetic `web.xml` (the "effective web.xml") according to the rules of the Servlet Specification taking into account all sources of discoverable configuration of web components in your application: descriptors (`webdefault.xml`, `web.xml`, `web-fragment.xml`s, `web-override.xml`) and discovered annotations (`@WebServlet`, `@WebFilter`, `@WebListener`).
No programmatic declarations of servlets, filters and listeners can be taken into account.

You can calculate the effective web.xml for any pre-built war file by setting the `<webApp><war>` parameter, or you can calculate it for the unassembled web application by setting all the usual `<webApp>` parameters as for `jetty:run`.

Other useful information about your web application that is produced as part of the analysis is also stored as context parameters in the effective-web.xml.
The effective-web.xml can be used in conjunction with the xref:operations-guide:quickstart/index.adoc[Quickstart] feature to quickly start your web application (note that Quickstart is not appropriate for the mvn jetty goals).

The effective web.xml from these combined sources is generated into a file, which by default is `target/effective-web.xml`, but can be changed by setting the `effectiveWebXml` configuration parameter.

=== Configuration

effectiveWebXml::
The full path name of a file into which you would like the effective web xml generated.
webApp::
war:::
The location of the built WAR file. This defaults to `${project.build.directory}/${project.build.finalName}.war`.
You can set it to the location of any pre-built war file.
Or you can leave it blank and set up the other `webApp` parameters as per <<jetty-run-goal,jetty:run>>, as well as the `webAppSourceDirectory`, `classes` and `testClasses` parameters.
contextPath:::
The context path for your web application.
By default, this is set to `/`.
If using a custom value for this parameter, you should include the leading `/`, example `/mycontext`.
defaultsDescriptor:::
The path to a `webdefault.xml` file that will be applied to your web application before the `web.xml`.
If you don't supply one, Jetty uses a default file baked into the `jetty-{ee-current}-webapp-{jetty-version}.jar`.
overrideDescriptor:::
The path to a `web.xml` file that Jetty applies after reading your `web.xml`.
You can use this to replace or add configuration.
containerIncludeJarPattern:::
Defaults to `.*/jetty-jakarta-servlet-api-[^/]*\.jar$|.*jakarta.servlet.jsp.jstl-[^/]*\.jar|.*taglibs-standard-impl-.*\.jar`.
This is a pattern that is applied to the names of the jars on the container's classpath (ie the classpath of the plugin, not that of the web application) that should be scanned for fragments, tlds, annotations etc.
This is analogous to the context attribute xref:operations-guide:annotations/index.adoc#og-container-include-jar-pattern[org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern] that is documented xref:operations-guide:annotations/index.adoc#og-container-include-jar-pattern[here].
You can define extra patterns of jars that will be included in the scan.
webInfIncludeJarPattern:::
Defaults to matching _all_ the dependency jars for the web application (ie the equivalent of WEB-INF/lib).
You can make this pattern more restrictive to only match certain jars by using this setter.
This is analogous to the context attribute xref:operations-guide:annotations/index.adoc#og-web-inf-include-jar-pattern[org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern] that is documented xref:operations-guide:annotations/index.adoc#og-web-inf-include-jar-pattern[here].
tempDirectory:::
The path to a dir that Jetty can use to expand or copy jars and jsp compiles when your web application is running.
The default is `${project.build.outputDirectory}/tmp`.
contextXml:::
The path to a context xml file that is applied to your web application AFTER the `webApp` element.


You can also generate the origin of each element into the effective web.xml file.
The origin is either a descriptor eg web.xml,web-fragment.xml,override-web.xml file, or an annotation eg @WebServlet.
Some examples of elements with origin attribute information are:

[,xml]
----
<listener origin="DefaultsDescriptor(file:///path/to/distro/etc/webdefault.xml):21">
<listener origin="WebDescriptor(file:///path/to/base/webapps/test-spec/WEB-INF/web.xml):22">
<servlet-class origin="FragmentDescriptor(jar:file:///path/to/base/webapps/test-spec/WEB-INF/lib/test-web-fragment.jar!/META-INF/web-fragment.xml):23">
<servlet-class origin="@WebServlet(com.acme.test.TestServlet):24">
----

To generate origin information, use the following configuration parameters on the  `webApp` element:

originAttribute::
The name of the attribute that will contain the origin.
By default it is `origin`.
generateOrigin::
False by default. If true, will force the generation of the `originAttribute` onto each element.


[[using-overlaid-wars]]
== Using Overlaid wars

If your web application depends on other war files, the <<jetty-run-goal,jetty:run>> and <<jetty-start-goal,jetty:start>> goals are able to merge resources from all of them.
It can do so based on the settings of the http://maven.apache.org/plugins/maven-war-plugin/[maven-war-plugin], or if your project does not use the http://maven.apache.org/plugins/maven-war-plugin/[maven-war-plugin] to handle the overlays, it can fall back to a simple algorithm to determine the ordering of resources.

=== With maven-war-plugin

The `maven-war-plugin` has a rich set of capabilities for merging resources.
The `jetty:run` and `jetty:start` goals are able to interpret most of them and apply them during execution of your unassembled web application.
This is probably best seen by looking at a concrete example.

Suppose your web application depends on the following wars:

[,xml]
----
<dependency>
  <groupId>com.acme</groupId>
  <artifactId>X</artifactId>
  <type>war</type>
</dependency>
<dependency>
  <groupId>com.acme</groupId>
  <artifactId>Y</artifactId>
  <type>war</type>
</dependency>
----

Containing:

----
WebAppX:

 /foo.jsp
 /bar.jsp
 /WEB-INF/web.xml

WebAppY:

 /bar.jsp
 /baz.jsp
 /WEB-INF/web.xml
 /WEB-INF/special.xml
----

They are configured for the http://maven.apache.org/plugins/maven-war-plugin/overlays.html[maven-war-plugin]:

[,xml,subs=attributes+]
----
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <version>{jetty-version}</version>
  <configuration>
    <overlays>
      <overlay>
        <groupId>com.acme</groupId>
        <artifactId>X</artifactId>
        <excludes>
          <exclude>bar.jsp</exclude>
        </excludes>
      </overlay>
      <overlay>
        <groupId>com.acme</groupId>
        <artifactId>Y</artifactId>
        <excludes>
          <exclude>baz.jsp</exclude>
        </excludes>
      </overlay>
      <overlay>
      </overlay>
    </overlays>
  </configuration>
</plugin>
----

Then executing jetty:run would yield the following ordering of resources: `com.acme.X.war : com.acme.Y.war: ${project.basedir}/src/main/webapp`.
Note that the current project's resources are placed last in the ordering due to the empty <overlay/> element in the `maven-war-plugin`.
You can either use that, or specify the `<baseAppFirst>false</baseAppFirst>` parameter to the `jetty-{ee-current}-maven-plugin`.

Moreover, due to the `exclusions` specified above, a request for the resource ` bar.jsp` would only be satisfied from `com.acme.Y.war.`
Similarly as `baz.jsp` is excluded, a request for it would result in a 404 error.

=== Without maven-war-plugin

The algorithm is fairly simple, is based on the ordering of declaration of the dependent wars, and does not support exclusions.
The configuration parameter `<baseAppFirst>` (see for example <<jetty-run-goal,jetty:run>> for more information) can be used to control whether your web application's resources are placed first or last on the resource path at runtime.

For example, suppose our web application depends on these two wars:

[,xml]
----
<dependency>
  <groupId>com.acme</groupId>
  <artifactId>X</artifactId>
  <type>war</type>
</dependency>
<dependency>
  <groupId>com.acme</groupId>
  <artifactId>Y</artifactId>
  <type>war</type>
</dependency>
----

Suppose the web applications contain:

----
WebAppX:

 /foo.jsp
 /bar.jsp
 /WEB-INF/web.xml

WebAppY:

 /bar.jsp
 /baz.jsp
 /WEB-INF/web.xml
 /WEB-INF/special.xml

----

Then our web application has available these additional resources:

----
/foo.jsp (X)
/bar.jsp (X)
/baz.jsp (Y)
/WEB-INF/web.xml (X)
/WEB-INF/special.xml (Y)
----

[[configuring-security-settings]]
== Configuring Security Settings

You can configure LoginServices in the plugin.
Here's an example of setting up the `HashLoginService` for a web application:

[,xml,subs=attributes+]
----
<plugin>
  <groupId>org.eclipse.jetty.{ee-current}</groupId>
  <artifactId>jetty-{ee-current}-maven-plugin</artifactId>
  <version>{jetty-version}</version>
  <configuration>
    <scan>10</scan>
    <webApp>
      <contextPath>/test</contextPath>
    </webApp>
    <loginServices>
      <loginService implementation="org.eclipse.jetty.security.HashLoginService">
        <name>Test Realm</name>
        <config implementation="org.eclipse.jetty.maven.MavenResource">
          <resourceAsString>${project.basedir}/src/etc/realm.properties</resourceAsString>
        </config>
      </loginService>
    </loginServices>
  </configuration>
</plugin>
----

[[using-multiple-webapp-root-directories]]
== Using Multiple Web Application Root Directories

If you have external resources that you want to incorporate in the execution of a web application, but which are not assembled into war files, you can't use the overlaid wars method described above, but you can tell Jetty the directories in which these external resources are located.
At runtime, when Jetty receives a request for a resource, it searches all the locations to retrieve the resource.
It's a lot like the overlaid war situation, but without the war.

Here is a configuration example:

[,xml]
----
<configuration>
  <webApp>
    <contextPath>/${build.finalName}</contextPath>
    <resourceBases>
      <resourceBase>src/main/webapp</resourceBase>
      <resourceBase>/home/johndoe/path/to/my/other/source</resourceBase>
      <resourceBase>/yet/another/folder</resourceBase>
    </resourceBases>
  </webApp>
</configuration>
----

[[running-more-than-one-webapp]]
== Running More than One Web Application

=== With jetty:run

You can use either a `jetty.xml` file to configure extra (pre-compiled) web applications that you want to deploy, or you can use the `<contextHandlers>` configuration element to do so.
If you want to deploy web application A, and web applications B and C in the same Jetty instance:

Putting the configuration in web application A's `pom.xml`:

[,xml,subs=attributes+]
----
<plugin>
  <groupId>org.eclipse.jetty.{ee-current}</groupId>
  <artifactId>jetty-{ee-current}-maven-plugin</artifactId>
  <version>{jetty-version}</version>
  <configuration>
    <scan>10</scan>
    <webApp>
      <contextPath>/test</contextPath>
    </webApp>
    <contextHandlers>
      <contextHandler implementation="org.eclipse.jetty.{ee-current}.maven.plugin.MavenWebAppContext">
        <war>${project.basedir}../../B.war</war>
        <contextPath>/B</contextPath>
      </contextHandler>
      <contextHandler implementation="org.eclipse.jetty.{ee-current}.maven.plugin.MavenWebAppContext">
        <war>${project.basedir}../../C.war</war>
        <contextPath>/C</contextPath>
      </contextHandler>
    </contextHandlers>
  </configuration>
</plugin>
----

[IMPORTANT]
====
If the `ContextHandler` you are deploying is a Jakarta EE web application, it is *essential* that you use an `org.eclipse.jetty.{ee-current}.maven.plugin.MavenWebAppContext` instance rather than a standard `org.eclipse.jetty.{ee-current}.webapp.WebAppContext` instance.
Only the former will allow the web application to function correctly in the Maven environment.
====

Alternatively, add a `jetty.xml` file to web application A.
Copy the `jetty.xml` file from the Jetty distribution, and then add ``WebAppContext``s for the other 2 web applications:

[,xml]
----
<Ref refid="Contexts">
  <Call name="addHandler">
    <Arg>
      <New class="org.eclipse.jetty.{ee-current}.maven.plugin.MavenWebAppContext">
        <Set name="contextPath">/B</Set>
        <Set name="war">../../B.war</Set>
      </New>
    </Arg>
  </Call>
  <Call>
    <Arg>
      <New class="org.eclipse.jetty.{ee-current}.maven.plugin.MavenWebAppContext">
        <Set name="contextPath">/C</Set>
        <Set name="war">../../C.war</Set>
      </New>
    </Arg>
  </Call>
</Ref>
----

Then configure the location of this `jetty.xml` file into web application A's Jetty plugin:

[,xml,subs=attributes+]
----
<plugin>
  <groupId>org.eclipse.jetty.{ee-current}</groupId>
  <artifactId>jetty-{ee-current}-maven-plugin</artifactId>
  <version>{jetty-version}</version>
  <configuration>
    <scan>10</scan>
    <webApp>
      <contextPath>/test</contextPath>
    </webApp>
    <jettyXml>src/main/etc/jetty.xml</jettyXml>
  </configuration>
</plugin>
----

For either of these solutions, the other web applications must already have been built, and they are not automatically monitored for changes.
You can refer either to the packed WAR file of the pre-built web applications or to their expanded equivalents.

[[setting-system-properties]]
== Setting System Properties

You can specify property name/value pairs that Jetty sets as System properties for the execution of the plugin.
This feature is useful to tidy up the command line and save a lot of typing.

However, *sometimes it is not possible to use this feature to set System properties* - sometimes the software component using the System property is already initialized by the time that maven runs (in which case you will need to provide the System property on the command line), or by the time that Jetty runs.
In the latter case, you can use the http://www.mojohaus.org/[maven properties plugin] to define the system properties instead. Here's an example that configures the logback logging system as the Jetty logger:

[,xml]
----
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>properties-maven-plugin</artifactId>
  <executions>
    <execution>
      <goals>
        <goal>set-system-properties</goal>
      </goals>
      <configuration>
        <properties>
          <property>
            <name>logback.configurationFile</name>
            <value>${project.baseUri}/resources/logback.xml</value>
          </property>
        </properties>
      </configuration>
    </execution>
  </executions>
</plugin>
----

[NOTE]
====
If a System property is already set (for example, from the command line or by the JVM itself), then by default these configured properties *DO NOT* override them.
However, they can override system properties set from a file instead, see <<specifying-properties-in-file,specifying system properties in a file>>.
====

[[specifying-properties-in-pom]]
=== Specifying System Properties in the POM

Here's an example of how to specify System properties in the POM:

[,xml]
----
<plugin>
  <groupId>org.eclipse.jetty.{ee-current}</groupId>
  <artifactId>jetty-{ee-current}-maven-plugin</artifactId>
  <configuration>
    <systemProperties>
        <fooprop>222</fooprop>
    </systemProperties>
    <webApp>
      <contextPath>/test</contextPath>
    </webApp>
  </configuration>
</plugin>
----

[[specifying-properties-in-file]]
=== Specifying System Properties in a File

You can also specify your System properties in a file.
System properties you specify in this way *do not* override System properties that set on the command line, by the JVM, or directly in the POM via `systemProperties`.

Suppose we have a file called `mysys.props` which contains the following:

----
fooprop=222
----

This can be configured on the plugin like so:

[,xml]
----
<plugin>
  <groupId>org.eclipse.jetty.{ee-current}</groupId>
  <artifactId>jetty-{ee-current}-maven-plugin</artifactId>
  <configuration>
    <systemPropertiesFile>${project.basedir}/mysys.props</systemPropertiesFile>
    <webApp>
      <contextPath>/test</contextPath>
    </webApp>
  </configuration>
</plugin>
----

You can instead specify the file by setting the System property `jetty.systemPropertiesFile` on the command line.

---

### Documentation Jetty Modules Programming Guide Pages Migration 11 To 12 (`documentation/jetty/modules/programming-guide/pages/migration/11-to-12.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Migrating from Jetty 11.0.x to Jetty 12.0.x

[[java-version]]
== Required Java Version Changes

[cols="1,1", options="header"]
|===
| Jetty 11.0.x | Jetty 12.0.x
| Java 11 | Java 17
|===

[[maven-artifacts]]
== Maven Artifacts Changes

[cols="1a,1a", options="header"]
|===
| Jetty 11.0.x | Jetty 12.0.x
| org.eclipse.jetty.fcgi:**fcgi-client** | org.eclipse.jetty.fcgi:**jetty-fcgi-client**
| org.eclipse.jetty.fcgi:**fcgi-server** | org.eclipse.jetty.fcgi:**jetty-fcgi-server**
| org.eclipse.jetty.http2:**http2-client** | org.eclipse.jetty.http2:**jetty-http2-client**
| org.eclipse.jetty.http2:**http2-common** | org.eclipse.jetty.http2:**jetty-http2-common**
| org.eclipse.jetty.http2:**http2-hpack** | org.eclipse.jetty.http2:**jetty-http2-hpack**
| org.eclipse.jetty.http2:**http2-http-client-transport** | org.eclipse.jetty.http2:**jetty-http2-client-transport**
| org.eclipse.jetty.http2:**http2-server** | org.eclipse.jetty.http2:**jetty-http2-server**
| org.eclipse.jetty.http3:**http3-client** | org.eclipse.jetty.http3:**jetty-http3-client**
| org.eclipse.jetty.http3:**http3-common** | org.eclipse.jetty.http3:**jetty-http3-common**
| org.eclipse.jetty.http3:**http3-http-client-transport** | org.eclipse.jetty.http3:**jetty-http3-client-transport**
| org.eclipse.jetty.http3:**http3-qpack** | org.eclipse.jetty.http3:**jetty-http3-qpack**
| org.eclipse.jetty.http3:**http3-server** | org.eclipse.jetty.http3:**jetty-http3-server**
| org.eclipse.jetty:**jetty-osgi.*** | * org.eclipse.jetty:**jetty-osgi**
* org.eclipse.jetty.{ee-all}:**jetty-{ee-all}-osgi-***
| org.eclipse.jetty:**jetty-proxy** | * org.eclipse.jetty:**jetty-proxy**
* org.eclipse.jetty.{ee-all}:**jetty-{ee-all}-proxy**
| org.eclipse.jetty.quic:**quic-client** | org.eclipse.jetty.quic:**jetty-quic-client**
| org.eclipse.jetty.quic:**quic-common** | org.eclipse.jetty.quic:**jetty-quic-common**
| org.eclipse.jetty.quic:**quic-quiche** | org.eclipse.jetty.quic:**jetty-quic-quiche**
| org.eclipse.jetty.quic:**quic-server** | org.eclipse.jetty.quic:**jetty-quic-server**
| org.eclipse.jetty:**jetty-unixsocket.*** | Removed -- Use org.eclipse.jetty:jetty-unixdomain-server
| org.eclipse.jetty.websocket:**websocket-core-client** | org.eclipse.jetty.websocket:**jetty-websocket-core-client**
| org.eclipse.jetty.websocket:**websocket-core-common** | org.eclipse.jetty.websocket:**jetty-websocket-core-common**
| org.eclipse.jetty.websocket:**websocket-core-server** | org.eclipse.jetty.websocket:**jetty-websocket-core-server**
| org.eclipse.jetty.websocket:**websocket-jetty-api** | org.eclipse.jetty.websocket:**jetty-websocket-jetty-api**
| org.eclipse.jetty.websocket:**websocket-jetty-client** | * org.eclipse.jetty.websocket:**jetty-websocket-jetty-client**
* org.eclipse.jetty.**{ee-all}.websocket**:jetty-**{ee-all}-websocket-jetty-client**
| org.eclipse.jetty.websocket:**websocket-jetty-common** | * org.eclipse.jetty.websocket:**jetty-websocket-jetty-common**
* org.eclipse.jetty.**{ee-all}.websocket**:jetty-**{ee-all}-websocket-jetty-common**
| org.eclipse.jetty.websocket:**websocket-jetty-server** | * org.eclipse.jetty.websocket:**jetty-websocket-jetty-server**
* org.eclipse.jetty.**{ee-all}.websocket**:jetty-**{ee-all}-websocket-jetty-server**
| org.eclipse.jetty.**websocket**:**websocket-jakarta-client** | org.eclipse.jetty.**{ee-all}.websocket**:**jetty-{ee-all}-websocket-jakarta-client**
| org.eclipse.jetty.**websocket**:**websocket-jakarta-common** | org.eclipse.jetty.**{ee-all}.websocket**:**jetty-{ee-all}-websocket-jakarta-common**
| org.eclipse.jetty.**websocket**:**websocket-jakarta-server** | org.eclipse.jetty.**{ee-all}.websocket**:**jetty-{ee-all}-websocket-jakarta-server**
| org.eclipse.jetty.**websocket**:**websocket-servlet** | org.eclipse.jetty.**{ee-all}.websocket**:**jetty-{ee-all}-websocket-servlet**
| org.eclipse.jetty:**apache-jsp** | org.eclipse.jetty.**{ee-all}**:**jetty-{ee-all}-apache-jsp**
| org.eclipse.jetty:**jetty-annotations** | org.eclipse.jetty.**{ee-all}**:**jetty-{ee-all}-annotations**
| org.eclipse.jetty:**jetty-ant** | Removed -- No Replacement
| org.eclipse.jetty:**jetty-cdi** | org.eclipse.jetty.**{ee-all}**:**jetty-{ee-all}-cdi**
| org.eclipse.jetty:**glassfish-jstl** | org.eclipse.jetty.**{ee-all}**:**jetty-{ee-all}-glassfish-jstl**
| org.eclipse.jetty:**jetty-jaspi** | org.eclipse.jetty.**{ee-all}**:**jetty-{ee-all}-jaspi**
| org.eclipse.jetty:**jetty-jndi** | org.eclipse.jetty.**{ee-all}**:**jetty-{ee-all}-jndi**
| org.eclipse.jetty:**jetty-jspc-maven-plugin** | org.eclipse.jetty.**{ee-all}**:**jetty-{ee-all}-jspc-maven-plugin**
| org.eclipse.jetty:**jetty-maven-plugin** | org.eclipse.jetty.**{ee-all}**:**jetty-{ee-all}-maven-plugin**
| org.eclipse.jetty:**jetty-plus** | org.eclipse.jetty.**{ee-all}**:**jetty-{ee-all}-plus**
| org.eclipse.jetty:**jetty-quickstart** | org.eclipse.jetty.**{ee-all}**:**jetty-{ee-all}-quickstart**
| org.eclipse.jetty:**jetty-servlet** | org.eclipse.jetty.**{ee-all}**:**jetty-{ee-all}-servlet**
| org.eclipse.jetty:**jetty-servlets** | org.eclipse.jetty.**{ee-all}**:**jetty-{ee-all}-servlets**
| org.eclipse.jetty:**jetty-webapp** | org.eclipse.jetty.**{ee-all}**:**jetty-{ee-all}-webapp**
|===

[[class-packages-names]]
== Class Packages/Names Changes

[cols="1a,1a", options="header"]
|===
| Jetty 11.0.x | Jetty 12.0.x
| `org.eclipse.jetty.client.**api**.*` | `org.eclipse.jetty.client.*`
| `org.eclipse.jetty.client.**util**.*` | `org.eclipse.jetty.client.*`
| `org.eclipse.jetty.client.**util**.*` | `org.eclipse.jetty.client.*`
| `org.eclipse.jetty.client.**http**.*` | `org.eclipse.jetty.client.**transport**.*`
| `org.eclipse.jetty.http2.client.**http**.*` | `org.eclipse.jetty.http2.client.**transport**.*`

| `org.eclipse.jetty.websocket.api.annotation.**OnWebSocketConnect**` | `org.eclipse.jetty.websocket.api.annotation.**OnWebSocketOpen**`
| `org.eclipse.jetty.websocket.api.**WriteCallback**` | `org.eclipse.jetty.websocket.api.**Callback**`
| `org.eclipse.jetty.websocket.api.**WebSocket*Listener**` | `org.eclipse.jetty.websocket.api.**Session.Listener.AutoDemanding**`
| `org.eclipse.jetty.websocket.api.**RemoteEndpoint**` | `org.eclipse.jetty.websocket.api.**Session**`
| `org.eclipse.jetty.websocket.api.**WebSocketPolicy**` | `org.eclipse.jetty.websocket.api.**Configurable**`
|===

[[jetty-module-names]]
== Jetty Module Changes
[cols="1a,1a", options="header"]
|===
| Jetty 11.0.x | Jetty 12.0.x
| `annotations.mod` | `{ee-all}-annotations.mod` +
| `apache-jsp.mod` | `{ee-all}-apache-jsp.mod` +
| `bytebufferpool-logarithmic.mod` | `bytebufferpool-quadratic.mod`
| `cdi.mod` +
`cdi-*.mod` | `{ee-all}-cdi.mod` +
`{ee-all}-cdi-*.mod`
| `deploy.mod` | `core-deploy.mod` +
`{ee-all}-deploy.mod`
| `fcgi.mod` | `{ee-all}-fcgi-proxy.mod`
| `glassfish-jstl.mod` | `{ee-all}-glassfish-jstl.mod`
| `jaspi.mod` +
`jaspi-*.mod` | `{ee-all}-jaspi.mod` +
`{ee-all}-jaspi-*.mod`
| `jsp.mod` | `{ee-all}-jsp.mod`
| `jstl.mod` | `{ee-all}-jstl.mod`
| `openid.mod` | `openid.mod` +
`{ee-all}-openid.mod`
| `plus.mod` | `plus.mod` +
`{ee-all}-plus.mod`
| `proxy.mod` | `proxy.mod` +
`{ee-all}-proxy.mod`
| `security.mod` | `security.mod` +
`{ee-all}-security.mod`
| `servlet.mod` | `{ee-all}-servlet.mod`
| `servlets.mod` | `{ee-all}-servlets.mod`
| `stats.mod` | `statistics.mod`
| `webapp.mod` | `{ee-all}-webapp.mod`
| `websocket.mod` | N/A
| `websocket-jakarta.mod` | `{ee-all}-websocket-jakarta.mod`
| `websocket-jetty.mod` | `websocket-jetty.mod` +
`{ee-all}-websocket-jetty.mod`
|===

With the introduction of the <<jetty-core-apis,Jetty Core APIs>>, modules that in Jetty 11 were implicitly based on the Servlet APIs, in Jetty 12 have been renamed to `{ee-all}-<name>.mod`.

Modules that retain the same name, such as `openid.mod`, `security.mod`, etc., in Jetty 12 enable the same functionality using the Jetty Core APIs, not the Servlet APIs.
For the correspondent module that uses the Servlet API, use `{ee-all}-openid.mod`, `{ee-all}-security.mod`, etc.

This is particularly important for the Jetty 11 `deploy.mod`, that in Jetty 12 must be explicitly changed to one of the `{ee-all}-deploy.mod`, otherwise web applications will not be deployed.
Refer to xref:operations-guide:deploy/index.adoc[web application deployment] section for more information.

[[jetty-core-apis]]
== Server-Side Web Application APIs Changes

Jetty 12 introduced redesigned server-side APIs for web applications.
In Jetty 11, these APIs were based on a mix of Jakarta Servlet APIs and Jetty Handler APIs, while in Jetty 12 they are solely based on Jetty Handler APIs.

In Jetty 12 you can now write web applications independently of the Servlet APIs, so you can migrate Jakarta Servlets to Jetty Handlers as explained in xref:servlet-to-handler[this section].

If you were already using the Jetty 11 Handler APIs, you can migrate them to the Jetty 12 Handler APIs as explained in xref:api-changes-handler[this section].

[[servlet-to-handler]]
== Migrate Servlets to Jetty Handlers

Web applications written using the Servlet APIs may be re-written using the Jetty `Handler` APIs.
The sections below outline the Jetty `Handler` APIs that correspond to the Servlet APIs.
For more information about why using the Jetty `Handler` APIs instead of the Servlet APIs, refer to xref:server/http.adoc[this section].

For more information about replacing ``HttpServlet``s or Servlet ``Filter``s with Jetty ``Handler``s, refer to xref:server/http.adoc#handler[this section].

=== Handler Request APIs
[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/migration/ServletToHandlerDocs.java[tags=request]
----

=== Handler Request Content APIs
[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/migration/ServletToHandlerDocs.java[tags=requestContent-string]

include::code:example$src/main/java/org/eclipse/jetty/docs/programming/migration/ServletToHandlerDocs.java[tags=requestContent-buffer]

include::code:example$src/main/java/org/eclipse/jetty/docs/programming/migration/ServletToHandlerDocs.java[tags=requestContent-stream]

include::code:example$src/main/java/org/eclipse/jetty/docs/programming/migration/ServletToHandlerDocs.java[tags=requestContent-source]

include::code:example$src/main/java/org/eclipse/jetty/docs/programming/migration/ServletToHandlerDocs.java[tags=requestContent-formFields]

include::code:example$src/main/java/org/eclipse/jetty/docs/programming/migration/ServletToHandlerDocs.java[tags=requestContent-multiPart]
----

Refer also to the `Content.Source` APIs detailed in xref:arch/io.adoc#content-source[this section].

=== Handler Response APIs
[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/migration/ServletToHandlerDocs.java[tags=response]
----

=== Handler Response Content APIs
[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/migration/ServletToHandlerDocs.java[tags=responseContent-implicit]

include::code:example$src/main/java/org/eclipse/jetty/docs/programming/migration/ServletToHandlerDocs.java[tags=responseContent-implicit-status]

include::code:example$src/main/java/org/eclipse/jetty/docs/programming/migration/ServletToHandlerDocs.java[tags=responseContent-explicit]

include::code:example$src/main/java/org/eclipse/jetty/docs/programming/migration/ServletToHandlerDocs.java[tags=responseContent-content]

include::code:example$src/main/java/org/eclipse/jetty/docs/programming/migration/ServletToHandlerDocs.java[tags=responseContent-string]

include::code:example$src/main/java/org/eclipse/jetty/docs/programming/migration/ServletToHandlerDocs.java[tags=responseContent-echo]

include::code:example$src/main/java/org/eclipse/jetty/docs/programming/migration/ServletToHandlerDocs.java[tags=responseContent-trailers]
----

Refer also to the `Content.Sink` APIs detailed in xref:arch/io.adoc#content-sink[this section].

[[api-changes]]
== APIs Changes

[[api-changes-handler]]
=== `Handler`

The server-side `Handler` class, and the APIs to use for request/response processing, have been redesigned in Jetty 12.

The Jetty 11 `Handler` method:

`Handler.handle(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response)`

has been changed in Jetty 12 to:

`Handler.handle(Request request, Response response, Callback callback)`

The Jetty 11 `target` parameter has been removed, and in Jetty 12 it has been replaced by the information present in `Request.getHttpURI()`.

In Jetty 11, ``Handler``s would mark the fact that they handled the request, and therefore are producing a response, by calling `Request.setHandled(true)`.
In Jetty 12, this is performed by returning `true` from the `Handler.handle(\...)` method, which also requires that the `Callback` parameter must be completed, either by succeeding it or failing it.

In Jetty 11, the `Handler.handle(\...)` method has a blocking semantic, while in Jetty 12 the `Handler.handle(\...)` method has an asynchronous semantic, thanks to the `Callback` parameter.
This means that you can return from the `Handler.handle(\...)` method _before_ the response has been sent, similarly to what you can do with the Servlet APIs when you call `HttpServletRequest.startAsync()`.
Similarly, in Jetty 11 after a call to `startAsync()` you must call `AsyncContext.complete()`, while in Jetty 12 you must complete the `Callback` parameter, either by succeeding it or failing it.

In Jetty 11, `AbstractHandler` provides a utility class to implement `Handler`.
In Jetty 12, use `Handler.Abstract`.

In Jetty 11, the APIs to deal with request or response HTTP headers are based on either Jetty's `HttpFields`, or the Servlet APIs.
In Jetty 12, the HTTP headers API are only based on `HttpFields`.
Please refer to the `HttpFields` link:{javadoc-url}/org/eclipse/jetty/http/HttpFields.html[javadocs] for details.

In Jetty 11, the request content is accessed via `Request.getInputStream()` or `HttpServletRequest.getInputStream()`.
In Jetty 12, the `Request` object itself _is-a_ `Content.Source` that can be read as explained in xref:arch/io.adoc#content-source[this section].
In Jetty 12, you can use `Content.Source.asInputStream(request)` to obtain an `InputStream` and minimize the changes to your code, but remember that `InputStream` only provides blocking APIs, while `Content.Source` provides non-blocking APIs.

In Jetty 11, the response content is accessed via `Response.getOutputStream()` or `HttpServletResponse.getOutputStream()`.
In Jetty 12, the `Response` object itself _is-a_ `Content.Sink` that can be written as explained in xref:arch/io.adoc#content-sink[this section].
In Jetty 12, you can use `Content.Sink.asOutputStream(response)` to obtain an `OutputStream` and minimize the changes to your code, but remember that `OutputStream` only provides blocking APIs, while `Content.Sink` provides non-blocking APIs.

[[api-changes-handler-sequence]]
=== `HandlerCollection` and `HandlerList`

Jetty 11's `org.eclipse.jetty.server.handler.HandlerCollection` and `org.eclipse.jetty.server.handler.HandlerList` have been replaced by Jetty 12's `org.eclipse.jetty.server.Handler.Sequence`.

Note that when used in configuration properties, XML files, or via reflection, you must use the fully qualified class name `org.eclipse.jetty.server.Handler$Sequence` (note the `$` symbol).

Note that class `org.eclipse.jetty.server.handler.ContextHandlerCollection` has been retained (with the updates introduced by `Handler` as described <<api-changes-handler,here>>), and it is typically a better choice: where `Handler.Sequence` just iterates all its children ``Handler``s until one return `true` from the `handle(Request, Response, Callback)` method, `ContextHandlerCollection` more efficiently selects the child `ContextHandler` based on context path and virtual hosts.

[[api-changes-handler-requestlog]]
=== `RequestLogHandler`

Jetty 11's `org.eclipse.jetty.server.handler.RequestLogHandler` has been removed with no replacement.

In Jetty 12, request logging is achieved by calling `Server.setRequestLog(RequestLog requestLog)`, as described in xref:server/http.adoc#request-logging[this section].

=== `HttpClient`

The Jetty 11 `Request.onResponseContentDemanded(Response.DemandedContentListener)` API has been replaced by `Request.onResponseContentSource(Response.ContentSourceListener)` in Jetty 12.

However, also look at `Request.onResponseContentAsync(Response.AsyncContentListener)` and `Request.onResponseContent(Response.ContentListener)` for simpler usages.

The Jetty 11 model was a "demand+push" model: the application was demanding content; when the content was available, the implementation was pushing content to the application by calling `DemandedContentListener.onContent(Response, LongConsumer, ByteBuffer, Callback)` for every content chunk.

The Jetty 12 model is a "demand+pull" model: when the content is available, the implementation calls once `Response.ContentSourceListener.onContentSource(Content.Source)`; the application can then pull the content chunks from the `Content.Source`.

For more information about the new model, see xref:arch/io.adoc#content-source[this section].

Jetty 12 introduced the concept of low-level transport for high-level protocols, described in xref:client/io-arch.adoc#transport[this section].

=== WebSocket

The Jetty WebSocket APIs have been vastly simplified, and brought in line with the style of other APIs.

The Jetty 12 WebSocket APIs are now fully asynchronous, so the Jetty 11 `SuspendToken` class has been removed in favor of an explicit (or automatic) demand mechanism in Jetty 12 (for more information, refer to xref:client/websocket.adoc#endpoints-demand[this section]).

The various Jetty 11 `WebSocket*Listener` interfaces have been replaced by a single interface in Jetty 12, `Session.Listener.AutoDemanding` (for more information, refer to xref:client/websocket.adoc#endpoints-listener[this section]).

The Jetty 11 `RemoteEndpoint` APIs have been merged into the `Session` APIs in Jetty 12.

The Jetty 11 `WriteCallback` class has been renamed to just `Callback` in Jetty 12, because it is now also used when receiving binary data.
Note that this `Callback` interface is a different interface from the `org.eclipse.jetty.util.Callback` interface, which cannot be used in the Jetty WebSocket APIs due to class loader visibility issues.

On the server-side, the Jetty WebSocket APIs have been made independent of the Servlet APIs.

Jetty 11 `JettyWebSocketServerContainer` has been replaced by `ServerWebSocketContainer` in Jetty 12, with similar APIs (for more information, refer to xref:server/websocket.adoc#jetty[this section]).

On the client-side the `WebSocketClient` APIs are practically unchanged, as most of the changes come from the `HttpClient` changes described above.

---

### Documentation Jetty Modules Programming Guide Pages Migration 12.0 To 12.1 (`documentation/jetty/modules/programming-guide/pages/migration/12.0-to-12.1.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Migrating from Jetty 12.0.x to Jetty 12.1.x

The main theme of Jetty 12.1.x is the implementation of the Jakarta EE 11 specifications, so that now Jetty 12.1.x supports Java EE 8, Jakarta EE 9, Jakarta EE 10 and Jakarta EE 11.

Jetty 12.1.x now implements the following Jakarta EE 11 specifications:

* link:https://jakarta.ee/specifications/servlet/6.1/[Jakarta Servlet 6.1]
* link:https://jakarta.ee/specifications/pages/4.0/[Jakarta Pages 4.0]
* link:https://jakarta.ee/specifications/websocket/2.2/[Jakarta WebSocket 2.2]

With the implementation of Jakarta EE 11, all the Jetty modules that before were available for Jakarta EE 10, such as `ee10-servlet`, `ee10-jsp`, etc. are now available for Jakarta EE 11 as `ee11-servlet`, `ee11-jsp`, etc.

[[component-changes]]
== Component Changes

[[component-changes-deployer]]
=== Deployer

The xref:operations-guide:deploy/index.adoc[deployer] is the Jetty component that deploys web applications from the `$JETTY_BASE/webapps` directory, and it has been completely rewritten in Jetty 12.1.x.

The rewrite allowed to use only one scanner (Jetty 12.0.x had one for each Jakarta EE environment), and allowed to define the deployment of xref:operations-guide:deploy/index.adoc#web-application-format-core[Jetty core web applications] and xref:operations-guide:deploy/index.adoc#web-application-format-static[static web applications].

[[component-changes-authentication]]
=== Authentication

The server-side authentication mechanisms now have these additional implementations:

* `EthereumAuthenticator`, that implements link:https://eips.ethereum.org/EIPS/eip-4361[ERC-4361: Sign-In with Ethereum], to allow users to authenticate with their blockchain wallets (see xref:security/siwe-support.adoc[this section] for further details).
* `MultiAuthenticator` to allow multiple authentications for the same web application.

[[component-changes-compression]]
=== Compression

Jetty 12.0.x only supported GZIP compression, for both the client (`GZIPContentDecoder`) and the server (`GzipHandler`).

Jetty 12.1.x now supports pluggable compression mechanisms and provides implementations for GZIP, link:https://github.com/google/brotli[Brotli] and link:https://github.com/facebook/zstd[Zstandard], in both the client (see xref:client/http.adoc#content-response[here]) and the server (see xref:server/http.adoc#handler-use-compression[here]).
Brotli and Zstandard use native libraries for the compression implementation.

On the client, `ContentDecoder` has been changed to accommodate the new compression mechanisms, see <<api-changes-httpclient,this section>> for further details.

On the server, `GzipHandler` has been deprecated, in favor of `CompressionHandler`, see  xref:server/http.adoc#handler-use-compression[this section] for further details.

Also on the server, the Jetty module `gzip.mod` has been deprecated, replaced by the Jetty module xref:operations-guide:modules/standard.adoc#compression-gzip[`compression-gzip`].

New `+compression-*+` Jetty modules have been added for all compression mechanisms, see xref:operations-guide:modules/standard.adoc#compression[this section] for further details.

[[component-changes-invocation-type]]
=== `InvocationType`

Components that implement `Invocable` return an `InvocationType` that defines the blocking or non-blocking behavior of the component, for example whether a server-side `Handler` or a demand task passed to `Content.Source.demand(Runnable)` is blocking or non-blocking.
The `InvocationType` is then used by Jetty's xref:arch/threads.adoc#execution-strategy-adaptive[adaptive execution strategy] to efficiently process these components.

Jetty 12.1.x strengthens the processing of `Invocable` components for both the client (refer to xref:client/threads.adoc[this section]) and the server (refer to xref:server/threads.adoc[this section]).

Client-side and server-side applications can now explicitly set the `InvocationType` of specific components, and Jetty will process them accordingly.

[[component-changes-quic]]
=== QUIC

The support for the QUIC protocol underlying HTTP/3 has been completely rewritten.

The rewrite introduced a QUIC API that abstracts away the QUIC implementation.

In Jetty 12.1.x the QUIC implementation is still based on the xref:server/http3.adoc[Quiche native library], but thanks to the QUIC API abstraction in the future it will be possible to plug in a different QUIC implementation without changing the HTTP/3 layer.

[[component-changes-json]]
=== JSON

The Jetty JSON library now supports Java 16 records.

[[api-changes]]
== APIs Changes

[[api-changes-httpclient]]
=== `HttpClient`

In Jetty 12.0.x, applications could configure response content decoding through `HttpClient.getContentDecoderFactories()`, and implement their own by implementing `org.eclipse.jetty.client.ContentDecoder`.

In Jetty 12.1.x, applications can still configure response content decoding through `HttpClient.getContentDecoderFactories()`.

However, the decoding is based on the `org.eclipse.jetty.compression.Compression` classes.
Applications can implement their own response content decoding by implementing a `Compression` subclass and the corresponding `DecoderSource`, based on the xref:arch/io.adoc#content-source[`Content.Source`] and xref:arch/io.adoc#content-source-chunk[`Content.Chunk`] APIs.

[[api-changes-iterating-callback]]
=== `IteratingCallback`

Class `IteratingCallback` underwent refinements that changed the behavior of the `onCompleteFailure(Throwable)` method.

In Jetty 12.0.x, `IteratingCallback.onCompleteFailure(Throwable)` was called as soon as a failure was reported, without waiting the completion of the asynchronous operation (despite its name containing the word "complete").

For example, if a write operation performed with `IteratingCallback` was pending due to TCP congestion, and a timeout happened, `onCompleteFailure(Throwable)` was called as soon as the timeout happened, without waiting for the TCP congestion to resolve.

In Jetty 12.1.x, the same behavior is achieved by `IteratingCallback.onFailure(Throwable)`, so applications should review their usage of `IteratingCallback` and change the overrides of `onCompleteFailure(Throwable)` to override `onFailure(Throwable)` instead.

In Jetty 12.1.x, `onCompleteFailure(Throwable)` will be called when the asynchronous operation is complete; in the example above, it will be called when the TCP congestion is resolved.

[[api-changes-retainable-bytebuffer]]
=== `RetainableByteBuffer`

In Jetty 12.0.x, `RetainableByteBuffer` was primarily used internally, and exposed to users only through `ByteBufferPool.acquire(\...)`.

In Jetty 12.1.x, `RetainableByteBuffer` has broader usage within the xref:migration/11-to-12.adoc#jetty-core-apis[Jetty Core APIs]:

* `RetainableByteBuffer` is now a base interface of `Content.Chunk`, the key class to read content (see xref:arch/io.adoc#content-source-chunk[this section]).
* `RetainableByteBuffer.FixedCapacity` provides an implementation for fixed capacity buffers, such as those returned by `ByteBufferPool`.
* `RetainableByteBuffer.DynamicCapacity` provides an implementation for dynamic capacity buffers.
This class is useful to retain or copy other buffers such as ``Content.Chunk``s.
Dynamic capacity buffers are particularly effective to read and accumulate data efficiently (for example for later processing), when processing server-side request content, client-side response content, or WebSocket binary messages.

[[api-changes-httpconfig-maxresponseheadersize]]
=== `HttpConfiguration.maxResponseHeaderSize`

In Jetty 12.0.x, `HttpConfiguration.maxResponseHeaderSize` defaulted to -1, which means that `HttpConfiguration.responseHeaderSize` (defaulting to `8192`) is used instead to limit the length of the server response headers.

In Jetty 12.1.x, `HttpConfiguration.maxResponseHeaderSize` defaults to `16384`; if it is explicitly configured to `-1` Jetty 12.1.x will behave exactly like Jetty 12.0.x.

In Jetty 12.1.x and HTTP/1.1, this means that server responses are first generated using the smaller `HttpConfiguration.responseHeaderSize`, to use less resources.
If the generation of the server response overflows `HttpConfiguration.responseHeaderSize`, then `HttpConfiguration.maxResponseHeaderSize` is used.
If the generation overflows also `HttpConfiguration.maxResponseHeaderSize`, a `500` error response is generated.

In Jetty 12.1.x and HTTP/2 `HttpConfiguration.maxResponseHeaderSize` is used for the link:https://datatracker.ietf.org/doc/html/rfc9113#SETTINGS_MAX_HEADER_LIST_SIZE[`MAX_HEADER_LIST_SIZE`] setting, while in HTTP/3 is used for the link:https://datatracker.ietf.org/doc/html/rfc9114#SETTINGS_MAX_FIELD_SECTION_SIZE[`MAX_FIELD_SECTION_SIZE`] setting.

[[api-changes-websocket]]
=== `WebSocket`

In Jetty 12.0.x, it was possible to use the Jetty WebSocket annotations to have, in the same WebSocket EndPoint, a method annotated with `@OnWebSocketFrame` and one annotated with `@OnWebSocketMessage`, `@OnWebSocketPing` or `@OnWebSocketPong`.
However, this was causing issues, discussed in link:https://github.com/jetty/jetty.project/pull/12342[#12342].

In Jetty 12.1.x if the annotation `@OnWebSocketFrame` is present, then there cannot be methods annotated with `@OnWebSocketMessage`, `@OnWebSocketPing` or `@OnWebSocketPong`.

Furthermore, in Jetty 12.1.x the signature of methods annotated with `@OnWebSocketFrame` now requires a `Callback` parameter.

---

### Documentation Jetty Modules Programming Guide Pages Migration 94 To 10 (`documentation/jetty/modules/programming-guide/pages/migration/94-to-10.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Migrating from Jetty 9.4.x to Jetty 10.0.x

[[java-version]]
== Required Java Version Changes

[cols="1,1", options="header"]
|===
| Jetty 9.4.x | Jetty 10.0.x
| Java 8 | Java 11
|===

[[websocket]]
== WebSocket Migration Guide

Migrating from Jetty 9.4.x to Jetty 10.0.x requires changes in the coordinates of the Maven artifact dependencies for WebSocket. Some of these classes have also changed name and package. This is not a comprehensive list of changes but should cover the most common changes encountered during migration.

[[websocket-maven-artifact-changes]]
=== Maven Artifacts Changes

[cols="1a,1a", options="header"]
|===
| Jetty 9.4.x | Jetty 10.0.x

| `org.eclipse.jetty.websocket:**websocket-api**`
| `org.eclipse.jetty.websocket:**websocket-jetty-api**`

| `org.eclipse.jetty.websocket:**websocket-server**`
| `org.eclipse.jetty.websocket:**websocket-jetty-server**`

| `org.eclipse.jetty.websocket:**websocket-client**`
| `org.eclipse.jetty.websocket:**websocket-jetty-client**`

| `org.eclipse.jetty.websocket:**javax-websocket-server-impl**`
| `org.eclipse.jetty.websocket:**websocket-javax-server**`

| `org.eclipse.jetty.websocket:**javax-websocket-client-impl**`
| `org.eclipse.jetty.websocket:**websocket-javax-client**`

|===

[[websocket-class-name-changes]]
=== Class Names Changes

[cols="1a,1a", options="header"]
|===
| Jetty 9.4.x | Jetty 10.0.x

| `org.eclipse.jetty.websocket.**server.NativeWebSocketServletContainerInitializer**`
| `org.eclipse.jetty.websocket.**server.config.JettyWebSocketServletContainerInitializer**`

| `org.eclipse.jetty.websocket.**jsr356.server.deploy.WebSocketServerContainerInitializer**`
| `org.eclipse.jetty.websocket.**javax.server.config.JavaxWebSocketServletContainerInitializer**`

| `org.eclipse.jetty.websocket.**servlet.WebSocketCreator**`
| `org.eclipse.jetty.websocket.**server.JettyWebSocketCreator**`

| `org.eclipse.jetty.websocket.**servlet.ServletUpgradeRequest**`
| `org.eclipse.jetty.websocket.**server.JettyServerUpgradeRequest**`

| `org.eclipse.jetty.websocket.**servlet.ServletUpgradeResponse**`
| `org.eclipse.jetty.websocket.**server.JettyServerUpgradeResponse**`

| `org.eclipse.jetty.websocket.**servlet.WebSocketServlet**`
| `org.eclipse.jetty.websocket.**server.JettyWebSocketServlet**`

| `org.eclipse.jetty.websocket.**servlet.WebSocketServletFactory**`
| `org.eclipse.jetty.websocket.**server.JettyWebSocketServletFactory**`
|===

[[websocket-example-code]]
=== Example Code

[cols="1a,1a", options="header"]
|===
| Jetty 9.4.x
| Jetty 10.0.x

|
[,java]
----
public class ExampleWebSocketServlet extends WebSocketServlet
{
    @Override
    public void configure(WebSocketServletFactory factory)
    {
        factory.setCreator(new WebSocketCreator()
        {
            @Override
            public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp)
            {
                return new ExampleEndpoint();
            }
        });
    }
}
----

|
[,java]
----
public class ExampleWebSocketServlet extends JettyWebSocketServlet
{
    @Override
    public void configure(JettyWebSocketServletFactory factory)
    {
        factory.setCreator(new JettyWebSocketCreator()
        {
            @Override
            public Object createWebSocket(JettyServerUpgradeRequest req, JettyServerUpgradeResponse resp)
            {
                return new ExampleEndpoint();
            }
        });
    }
}
----
|===

---

### Documentation Jetty Modules Programming Guide Pages Security Configuring Form Size (`documentation/jetty/modules/programming-guide/pages/security/configuring-form-size.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

[[limit-form-content]]
= Limiting Form Content

Form content sent to the server is processed by Jetty into a map of parameters to be used by the web application.
Forms can be a vector for denial-of-service attacks, since significant memory and CPU can be consumed if a malicious client sends very large form content or a large number of form keys.
Thus, Jetty limits the amount of data and keys that can be in a form posted to Jetty.

The default maximum size Jetty permits is 200000 bytes and 1000 keys.
You can change this default for a particular web application or for all web applications on a particular `Server` instance.

== Configuring Form Limits for a Web Application

To configure the form limits for a single web application, the `ServletContextHandler` (or `WebAppContext`) instance can be configured using the following methods:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/security/FormDocs.java[tags=limitFormContent]
----

These settings can also be set via the following `ServletContext` attributes.

- `org.eclipse.jetty.server.Request.maxFormKeys`
- `org.eclipse.jetty.server.Request.maxFormContentSize`

== Configuring Default Form Limits for the Server

The following system properties can be used to configure form limits for the entire server, including all contexts without explicit configuration:

- `org.eclipse.jetty.server.Request.maxFormKeys`
- `org.eclipse.jetty.server.Request.maxFormContentSize`.

If not configured for either the server or a specific context, then the default  `maxFormKeys` is 1000 and the default `maxFormContentSize` is 200000.

== Limiting Form Content with Jetty Core API

The class `FormFields` is used to parse forms with the Jetty Core API, which provides `onFields` and `getFields` static methods to provide both async & blocking ways to parse a form.

These methods can take parameters for `maxFields` and `maxLength` which can be used to limit the form content.

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/security/FormDocs.java[tags=jettyCoreAPI]
----

---

### Documentation Jetty Modules Programming Guide Pages Security Index (`documentation/jetty/modules/programming-guide/pages/security/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Jetty Security

Web application security restricts access to resources within the web application, or imposes security requirements on the transport with which these resources are delivered to clients.

The processing of the request may be:

* _forbidden_; the request is immediately responded with a `403` status code.
* _allowed_; the request processing is allowed to continue to the application, which produces a response.
* _challenged_; the request may be allowed, but the credentials are missing (or wrong), so the request is responded either with a `401` status code, or with a response indicating that credentials are required (for example, a login page or a redirect to a login page.

Jetty implements web application security with 3 components:

* A subclass of `org.eclipse.jetty.security.SecurityHandler`, see <<security-handler,this section>> for the implementations available out-of-the-box.
* An implementation of `org.eclipse.jetty.security.Authenticator`, see <<security-authenticator,this section>> for the implementations available out-of-the-box.
* An implementation of `org.eclipse.jetty.security.LoginService`, see <<security-login-service,this section>> for the implementations available out-of-the-box.

These components interact in this way:

[plantuml]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false

participant SecurityHandler
participant Authenticator
participant LoginService

SecurityHandler -> SecurityHandler : getConstraint()
SecurityHandler -> Authenticator : validateRequest()
Authenticator -> LoginService : login()
LoginService -> Authenticator : roles
Authenticator -> SecurityHandler : AuthenticationState
----

1. The `SecurityHandler` subclass returns a `Constraint` based on the subclass-specific logic, for example based on the request path, the request method, etc.
2. The `Constraint` is used to check if the request is trivially allowed, or trivially forbidden, or whether it requires a secure transport, and if so immediately handled by `SecurityHandler` with no further actions.
Otherwise, the `Constraint` declares what requirements about authorization, transport and roles are necessary for the request to be allowed.
3. The `SecurityHandler` calls `Authenticator.validateRequest(\...)` that performs implementation-specific logic to retrieve the authentication credentials, for example from HTTP request headers such as `Authorization`.
4. The `Authenticator` calls `LoginService.login(\...)` to verify the credentials and, if the verification is successful, obtain information about the roles associated with these credentials.
5. The `Authenticator` builds an `AuthenticationState` with the results of the call to `LoginService.login(\...)`, and returns it to the `SecurityHandler`.
6. The `SecurityHandler`, based on the received `AuthenticationState`, either allows the request to be processed by its child `Handler`, or sends an appropriate response to the client, for example a `401` challenge response or a `403` forbidden response.

[[security-core-configuration]]
== Configuring Jetty Core Security

This section is about configuring security for Jetty Core web applications.
To configure security for Jakarta EE web applications, see <<security-jakarta-configuration,this section>>.

`SecurityHandler` is typically configured as a child of the `ContextHandler` that represents the web application.

A simple example uses `SecurityHandler.PathMapped` along with `BasicAuthenticator` and `HashLoginService`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/security/SecurityDocs.java[tags=pathMapped]
----

The `Handler` tree structure looks like the following:

[,screen]
----
Server
└── ContextHandler /app
    └── SecurityHandler
        └── AppHandler
----

The `realm.properties` file used by `HashLoginService` is the following:

.realm.properties
----
# Format: <user>:<password>,<roles>
carol:password
david:password,admin
----

The behavior of the example above is the following:

* For any request, _all_ the request path matches are collected and processed in order from the least significant to the most significant, combining their constraint.
* A non-secure request using the `http` scheme is replied with a `403` response, since secure transport is required.
* A request for `/app/foo` matches the constraint mapped to `+/*+`, and is replied with a `200` response; `AppHandler` would see a `null` request principal.
* A request for `/app/admin/bar` without `Authorization` header matches both the constraints mapped to `+/*+` and `+/admin/*+`, and is replied with a `401` response, since authentication is required.
* A request for `/app/admin/bar` with an `Authorization` header containing invalid or unknown credentials is replied with a `401` response, since authentication is required.
* A request for `/app/admin/bar` with an `Authorization` header containing valid credentials with a role that is not `admin` is replied with a `401` response, since authentication is required.
* A request for `/app/admin/bar` with an `Authorization` header containing valid credentials for user `david`, that has `admin` role, is replied with a `200` response, and `AppHandler` would see a request principal for user `david`.

[TIP]
====
It is good practice to have a default constraint for all paths (that is, for path `+/*+`), even if it is `Constraint.ALLOWED`.
In this way it is clear what is the intended behavior for paths that do not match more specific constraint configurations.
====

The more complex example below uses `SecurityHandler.PathMethodMapped` to allow users with the `read` role to read resources, and users with the `write` role to write resources:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/security/SecurityDocs.java[tags=pathMethodMapped]
----

.realm.properties
----
# Format: <user>:<password>,<roles>
bob:password,read
alice:password,read,write
----

In the example above, access to any resource is by default forbidden, and requires secure transport.

However, access to `/data/*` resources using the HTTP method `GET` is granted but only to users with `read` role.
Both `bob` and `alice` are granted access to these resources, but only if they use the `GET` method.

Similarly, access to `/data/*` resources using the HTTP method `PUT` is granted but only to users with `write` role.
Only `alice` is granted access to these resources, but only if she uses the `PUT` method.

[TIP]
====
It is good practice to have a default constraint for all paths (that is, for path `+/*+`), and for all HTTP methods (that is, for method `+*+`), even if it is `Constraint.ALLOWED`.
In this way it is clear what is the intended behavior for paths and HTTP methods that do not match more specific constraint configurations.
====

[IMPORTANT]
====
The behavior of `SecurityHandler` implementations for the Jetty Core API is to combine all the constraints that match.
This is different from the behavior of the `SecurityHandler` implementations for the Jakarta EE APIs, that only use the constraint of the most specific match.
====

[[security-jakarta-configuration]]
== Configuring Jakarta EE Security

This section is about configuring security for Jakarta EE web applications.
To configure security for Jetty Core web applications, see <<security-core-configuration,this section>>.

To configure Jakarta EE web application security you must use the `SecurityHandler` subclass `org.eclipse.jetty.{ee-current}.servlet.security.ConstraintSecurityHandler`.

Below you can find a simple example that sets up a `ConstraintSecurityHandler` to secure your Jakarta EE web application:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/security/SecurityDocs.java[tags=jakartaPathMapped]
----

The `Handler` tree structure looks like the following:

[,screen]
----
Server
└── WebAppContext /app
    └── ConstraintSecurityHandler
        └── ServletHandler
            └── AppServlet (defined in app.war)
----

The `realm.properties` file used by `HashLoginService` is the following:

.realm.properties
----
# Format: <user>:<password>,<roles>
carol:password
david:password,admin
----

Note how the constraints are configured using `org.eclipse.jetty.ee11.servlet.security.ConstraintMapping`, that allows to configure the path, the HTTP method and the `Constraint`.

The more complex example below uses `ConstraintSecurityHandler` to allow users with the `read` role to read resources, and users with the `write` role to write resources:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/security/SecurityDocs.java[tags=jakartaPathMethodMapped]
----

.realm.properties
----
# Format: <user>:<password>,<roles>
bob:password,read
alice:password,read,write
----

In the example above, access to any resource is by default forbidden, and requires secure transport.

However, access to `/data/*` resources using the HTTP method `GET` is granted but only to users with `read` role.
Both `bob` and `alice` are granted access to these resources, but only if they use the `GET` method.

Similarly, access to `/data/*` resources using the HTTP method `PUT` is granted but only to users with `write` role.
Only `alice` is granted access to these resources, but only if she uses the `PUT` method.

Note also that `ConstraintSecurityHandler.denyUncoveredHttpMethods=true` to make sure that requests that match `+/data/*+` but are neither `GET` nor `PUT` are denied.

[IMPORTANT]
====
The behavior of `SecurityHandler` implementations for the Jakarta EE API is to only use the constraint of the most specific match.
This is different from the behavior of the `SecurityHandler` implementations for the Jetty Core APIs, that combine all the constraints that match.
====

[[security-handler]]
== `SecurityHandler` Implementations

Jetty provides the following `SecurityHandler` implementations:

* `SecurityHandler.PathMapped`, that allows you to configure constraints based on the request path.
* `SecurityHandler.PathMethodMapped`, that allows you to configure constraints based on the request path and the request HTTP method.
* `ConstraintSecurityHandler`, that implements the Jakarta EE web application security constraints.

[[security-authenticator]]
== `Authenticator` Implementations

Jetty provides the following `Authenticator` implementations:

* `BasicAuthenticator`, that implements link:https://datatracker.ietf.org/doc/html/rfc7617[Basic authentication].
* `DigestAuthentication`, that implements link:https://datatracker.ietf.org/doc/html/rfc7616[Digest authentication].
* `FormAuthenticator`, that implements HTML form authentication using the link:https://jakarta.ee/specifications/servlet/6.1/jakarta-servlet-spec-6.1#form-based-authentication[`j_security_check` mechanism].
* `SslClientCertAuthenticator`, that implements authentication based on client certificates.
* `SPNEGOAuthenticator`, that implements link:https://datatracker.ietf.org/doc/html/rfc4178[SPNEGO authentication].
* `EthereumAuthenticator`, that implements link:https://eips.ethereum.org/EIPS/eip-4361[Sign-In with Ethereum authentication] (see also xref:security/siwe-support.adoc[this dedicated section]).
* `OpenIdAuthenticator`, that implements link:https://openid.net/specs/openid-connect-core-1_0-final.html[OpenID Connect Core 1.0 authentication] (see also xref:security/openid-support.adoc[this dedicated section]).
* `MultiAuthenticator`, that supports multiple authentication mechanisms for the same web application.
* `JaspiAuthenticator`, for the link:https://jakarta.ee/specifications/authentication/[Jakarta Authentication API].

[[security-login-service]]
== `LoginService` Implementations

Jetty provides the following `LoginService` implementations:

* `HashLoginService`, that verifies credentials retrieved from a `+*.properties+` file.
* `JDBCLoginService`, that verifies credentials retrieved from a RDBMS using the JDBC APIs.
* `DataSourceLoginService`, that verifies credentials retrieved from a RDBMS using the `DataSource` APIs.
* `JAASLoginService`, that verifies credentials retrieved using the JAAS APIs.
* `AnyUserLoginService`, that does not verify credentials, but can delegate to another `LoginService` to provide roles for authenticated users.

Some `Authenticator` implementation require a specific `LoginService`, so the following implementation are also available:

* `SPNEGOLoginService`, used in conjunction with `SPNEGOAuthenticator`.
* `OpenIdLoginService`, used in conjunction with `OpenIdAuthenticator`.

---

### Documentation Jetty Modules Programming Guide Pages Security Openid Support (`documentation/jetty/modules/programming-guide/pages/security/openid-support.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

[[openid-support]]
= OpenID Support

Jetty supports authentication using the OpenID Connect protocol (see link:https://openid.net/specs/openid-connect-core-1_0-final.html[OpenID Connect Core 1.0]).

This allows users to authenticate with third party OpenID Providers, making possible to integrate features like "Sign in with Google" or "Sign in with Microsoft", among others.

With OpenID Connect, Jetty applications can offload the responsibility of managing user credentials while enabling features like single sign-on (SSO).

== External Configuration

To use OpenID Connect authentication with Jetty you are required to set up an external OpenID Provider for your web application; some examples of OpenID Providers are link:https://developers.google.com/identity/protocols/OpenIDConnect[Google] and link:https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_create_oidc.html[Amazon].

Once you have set up your OpenID Provider you will have access to a Client ID and Client Secret which you can use to configure Jetty.

You must also configure your OpenID Provider to recognize the redirect URIs that your web application will use to handle authentication responses.
By default, the redirect path is `/j_security_check`, but this can be customized through the `OpenIdAuthenticator` configuration if needed.

For example, you may wish to register the following URIs:

 * For a deployed application, `+https://example.com/j_security_check+`.
 * For local development, `+http://localhost:8080/j_security_check+`.

Ensure that all relevant environments where your web application is deployed have their corresponding URIs registered with the OpenID Provider to avoid authentication errors.

== Jetty Configuration

=== Code Example

This is an example of how you can configure OpenID Connect authentication with an embedded Jetty environment with the Jetty Core API.

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/security/OpenIdDocs.java[tags=openIdConfigExample]
----

=== Application Usage

Here is an example of a Jetty Core `Handler` which handles authenticated requests by accessing the OpenID claims, and also handles authentication errors at `/error`.

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/security/OpenIdDocs.java[tags=openIdUsageExample]
----

==== Claims and Access Token
Claims about the user can be found using attributes in the HTTP session attribute `org.eclipse.jetty.security.openid.claims`, and the full response containing the OAuth 2.0 Access Token can be found in the HTTP session attribute `org.eclipse.jetty.security.openid.response`.

=== Authorization with Security Roles

If security roles are required, they can be configured through a wrapped `LoginService` which is deferred to for role information by the `OpenIdLoginService`.

The wrapped `LoginService` be configured through the constructor arguments of `OpenIdLoginService`, or left `null` if no user roles are required.

When using authorization roles, the property `authenticateNewUsers`, which can be configured through the `OpenIdConfiguration` or directly on the `OpenIdLoginService`, becomes significant.

When `authenticateNewUsers` is set to `true`, users not found by the wrapped `LoginService` will still be authenticated but will have no roles.

When `authenticateNewUsers` is set to `false`, users not found by the wrapped `LoginService` will be not be allowed to authenticate and are redirected to the error page.

---

### Documentation Jetty Modules Programming Guide Pages Security Siwe Support (`documentation/jetty/modules/programming-guide/pages/security/siwe-support.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

[[siwe-support]]
= SIWE Support

== Introduction

Sign-In with Ethereum (SIWE) is a decentralized authentication protocol that allows users to authenticate using their Ethereum account.

This enables users to retain more control over their identity and provides an alternative to protocols such as OpenID Connect, which rely on a centralized identity provider.

Sign-In with Ethereum works by using off-chain services to sign a standard message format defined by EIP-4361 (https://eips.ethereum.org/EIPS/eip-4361). The user signs the SIWE message to prove ownership of the Ethereum address. This is verified by the server by extracting the Ethereum address from the signature and comparing it to the address supplied in the SIWE message.

Typically, you would rely on a browser extension such as MetaMask to provide a user-friendly way for users to sign the message with their Ethereum account.

=== Support

Currently Jetty only provides support SIWE in Jetty 12.1+ and only for `jetty-core`, and `ee11`+ environments. It is enabled by adding the `EtheremAuthenticator` to the `SecurityHandler` of your web application.

== Usage

=== Enabling SIWE
The Sign-In with Ethereum module can be enabled when using Standalone Jetty with.
[source,subs=attributes+]
----
$ java -jar $JETTY_HOME/start.jar --add-modules=siwe
----

If using embedded Jetty you must add the `EthereumAuthenticator` to your `SecurityHandler`.

=== Configuration

Configuration of the `EthereumAuthenticator` is done through init params on the `ServletContext` or `SecurityHandler`. The `loginPath` is the only mandatory configuration and the others have defaults that you may wish to configure.

Login Path::
* Init param: `org.eclipse.jetty.security.siwe.login_path`
* Description: Unauthenticated requests are redirected to a login page where they must sign a SIWE message and send it to the server. This path represents a page in the application that contains the SIWE login page.

Nonce Path::
* Init param: `org.eclipse.jetty.security.siwe.nonce_path`
* Description: Requests to this path will generate a random nonce string which is associated with the session. The nonce is used in the SIWE Message to avoid replay attacks. The path at which this nonce is served can be configured through the init parameter. The application does not need to implement their own nonce endpoint, they just configure this path and the Authenticator handles it. The default value for this is `/auth/nonce` if left un-configured.

Authentication Path::
* Init param: `org.eclipse.jetty.security.siwe.authentication_path`
* Description: The authentication path is where requests containing a signed SIWE message are sent in order to authenticate the user. The default value for this is `/auth/login`.

Max Message Size::
* Init Param: `org.eclipse.jetty.security.siwe.max_message_size`
* Description: This is the max size of the authentication message which can be read by the implementation. This limit defaults to `4 * 1024`. This is necessary because the complete request content is read into a string and then parsed.

Logout Redirect Path::
* Init Param: `org.eclipse.jetty.security.siwe.logout_redirect_path`
* Description: Where the request is redirected to after logout. If left un-configured no redirect will be done upon logout.

Error Path::
* Init Param: `org.eclipse.jetty.security.siwe.error_path`
* Description: Path where Authentication errors are sent, this may contain an optional query string. An error description is available on the error page through the request parameter `error_description_jetty`. If this configuration is not set Jetty will send a 403 Forbidden response upon authentication errors.

Dispatch::
* Init Param: `org.eclipse.jetty.security.siwe.dispatch`
* Description: If set to true a dispatch will be done instead of a redirect to the login page in the case of an unauthenticated request. This defaults to false.

Authenticate New Users::
* Init Param: `org.eclipse.jetty.security.siwe.authenticate_new_users`
* Description: This can be set to false if you have a nested `LoginService` and only want to authenticate users known by the `LoginService`. This defaults to `true` meaning that any user will be authenticated regardless if they are known by the nested `LoginService`.

Domains::
* Init Param: org.eclipse.jetty.security.siwe.domains
* Description: The list of allowed domains to be declared in the `domain` field of the SIWE Message. If left unconfigured, the `domain` field must match the authority (`host[:port]`) of the request being authenticated.

Chain IDs::
* Init Param: org.eclipse.jetty.security.siwe.chainIds
* Description: This list of allowed Chain IDs to be declared in the `chain-id` field of the SIWE Message. If left blank this will allow all Chain IDs.

=== Nested LoginService

A nested `LoginService` may be used to assign roles to users of a known Ethereum Address. Or the nested `LoginService` may be combined with the setting `authenticateNewUsers == false` to only allow authentication of known users.

For example a `HashLoginService` may be configured through the `jetty-ee11-web.xml` file:
[, xml, indent=0]
----
<Configure id="wac" class="org.eclipse.jetty.ee11.webapp.WebAppContext">
  <Call id="ResourceFactory" class="org.eclipse.jetty.util.resource.ResourceFactory" name="of">
    <Arg><Ref refid="Server"/></Arg>
    <Call id="realmResource" name="newResource">
      <Arg><SystemProperty name="jetty.base" default="."/>/etc/realm.properties</Arg>
    </Call>
  </Call>

  <Call name="getSecurityHandler">
    <Set name="loginService">
      <New class="org.eclipse.jetty.security.HashLoginService">
        <Set name="name">myRealm</Set>
        <Set name="config"><Ref refid="realmResource"/></Set>
      </New>
    </Set>
  </Call>
</Configure>
----

=== Application Implementation
EIP-4361 specifies the format of a SIWE Message, the overview of the Sign-In with Ethereum process, and message validation. However, it does not specify certain things like how the SIWE Message and signature are sent to the server for validation, and it does not specify the process the client acquires the nonce from the server. For this reason the `EthereumAuthenticator` has been made extensible to allow different implementations.

Currently Jetty supports authentication requests of type `application/x-www-form-urlencoded` or `multipart/form-data`, which contains the fields `message` and `signature`. Where `message` contains the full SIWE message, and `signature` is the ERC-1271 signature of the SIWE message.

The nonce endpoint provided by the `EthereumAuthenticator` returns a response with `application/json` format, with a single key of `nonce`.

=== Configuring Security Handler
[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/security/siwe/SignInWithEthereumEmbeddedExample.java[tags=configureSecurityHandler]
----

=== Login Page Example

Include the `Web3.js` library to interact with the users Ethereum wallet.
[,html,indent=0]
----
<script src="https://cdn.jsdelivr.net/npm/web3@1.6.1/dist/web3.min.js"></script>
----

HTML form to submit the sign in request.
[,html,indent=0]
----
<button id="siwe">Sign-In with Ethereum</button>
<form id="loginForm" action="/auth/login" method="POST" style="display: none;">
    <input type="hidden" id="signatureField" name="signature">
    <input type="hidden" id="messageField" name="message">
</form>
<p class="alert" style="display: none;">Result: <span id="siweResult"></span></p>
----

Add script to generate and sign the SIWE message when the sign-in button is pressed.
[,html,indent=0]
----
<script>
    let provider = window.ethereum;
    let accounts;

    if (!provider) {
        document.getElementById('siweResult').innerText = 'MetaMask is not installed. Please install MetaMask to use this feature.';
    } else {
        document.getElementById('siwe').addEventListener('click', async () => {
            try {
                accounts = await provider.request({ method: 'eth_requestAccounts' });
                const domain = window.location.host;
                const from = accounts[0];

                // Fetch nonce from the server.
                const nonceResponse = await fetch('/auth/nonce');
                const nonceData = await nonceResponse.json();
                const nonce = nonceData.nonce;

                const siweMessage = `${domain} wants you to sign in with your Ethereum account:\n${from}\n\nI accept the MetaMask Terms of Service: https://community.metamask.io/tos\n\nURI: https://${domain}\nVersion: 1\nChain ID: 1\nNonce: ${nonce}\nIssued At: ${new Date().toISOString()}`;
                document.getElementById('signatureField').value = await provider.request({
                    method: 'personal_sign',
                    params: [siweMessage, from]
                });
                document.getElementById('messageField').value = siweMessage;
                document.getElementById('loginForm').submit();
            } catch (error) {
                console.error('Error during login:', error);
                document.getElementById('siweResult').innerText = `Error: ${error.message}`;
                document.getElementById('siweResult').parentElement.style.display = 'block';
            }
        });
    }
</script>
----

---

### Documentation Jetty Modules Programming Guide Pages Server Compliance (`documentation/jetty/modules/programming-guide/pages/server/compliance.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Server Compliance Modes

The Jetty server strives to keep up with the latest https://en.wikipedia.org/wiki/Request_for_Comments[IETF RFC]s for compliance with internet specifications, which are periodically updated.

When possible, Jetty will support backwards compatibility by providing compliance modes that can be configured to allow violations of the current specifications that may have been allowed in obsoleted specifications.

There are compliance modes provided for:

* <<http,HTTP Compliance>>
* <<uri,URI Compliance>>
* <<cookie,Cookie Compliance>>
* <<multiPart,MultiPart Compliance>>

Compliance modes can be configured to allow violations from the RFC requirements, or in some cases to allow additional behaviors that Jetty has implemented in excess of the RFC (for example, to allow <<uri,ambiguous URIs>>).

For example, the HTTP RFCs require that request HTTP methods are https://datatracker.ietf.org/doc/html/rfc9110#section-9.1[case sensitive], however Jetty can allow case-insensitive HTTP methods by including the link:{javadoc-url}/org/eclipse/jetty/http/HttpCompliance.Violation.html#CASE_INSENSITIVE_METHOD[`HttpCompliance.Violation.CASE_INSENSITIVE_METHOD`] in the link:{javadoc-url}/org/eclipse/jetty/http/HttpCompliance.html[`HttpCompliance`] set of allowed violations.

[[http]]
== HTTP Compliance Modes

In 1995, when Jetty was first implemented, there were no RFC specification of HTTP, only a W3C specification for https://www.w3.org/Protocols/HTTP/AsImplemented.html[HTTP/0.9], which has since been obsoleted or augmented by:

 * https://datatracker.ietf.org/doc/html/rfc1945[RFC 1945] for HTTP/1.0 in 1996
 * https://datatracker.ietf.org/doc/html/rfc2068[RFC 2068] for HTTP/1.1  in 1997
 * https://datatracker.ietf.org/doc/html/rfc2616[RFC 2616] for HTTP/1.1 bis in 1999
 * https://datatracker.ietf.org/doc/html/rfc7230[RFC 7230], https://datatracker.ietf.org/doc/html/rfc7231[RFC 7231], https://datatracker.ietf.org/doc/html/rfc7232[RFC 7232], https://datatracker.ietf.org/doc/html/rfc7233[RFC 7233], https://datatracker.ietf.org/doc/html/rfc7234[RFC 7234], https://datatracker.ietf.org/doc/html/rfc7235[RFC 7235] again for HTTP/1.1 in 2014
 * https://datatracker.ietf.org/doc/html/rfc7540[RFC 7540] for HTTP/2.0 in 2015
* https://datatracker.ietf.org/doc/html/rfc9110[RFC 9110] for common HTTP semantics and https://datatracker.ietf.org/doc/html/rfc9112[RFC 9112] for HTTP/1.1

In addition to these evolving requirements, some earlier version of Jetty did not completely or strictly implement the RFC at the time (for example, case-insensitive HTTP methods).
Therefore, upgrading to a newer Jetty version may cause runtime behavior differences that may break your applications.

The link:{javadoc-url}/org/eclipse/jetty/http/HttpCompliance.Violation.html[`HttpCompliance.Violation`] enumeration defines the RFC requirements that may be optionally enforced by Jetty, to support legacy deployments. These possible violations are grouped into modes by the link:{javadoc-url}/org/eclipse/jetty/http/HttpCompliance.html[`HttpCompliance`] class, which also defines several named modes that support common deployed sets of violations (with the default being link:{javadoc-url}/org/eclipse/jetty/http/HttpCompliance.html#RFC9110[`HttpCompliance.RFC9110`]).

For example:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=httpCompliance]
----

If you want to customize the violations that you want to allow, you can create your own mode using the link:{javadoc-url}/org/eclipse/jetty/http/HttpCompliance.html#from(java.lang.String)[`HttpCompliance.from(String)`] method:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=httpComplianceCustom]
----

[[uri]]
== URI Compliance Modes

Universal Resource Locators (URLs) where initially formalized in 1994 in https://datatracker.ietf.org/doc/html/rfc1738[RFC 1738] and then refined in 1995 with relative URLs by https://datatracker.ietf.org/doc/html/rfc1808[RFC 1808].

In 1998, URLs were generalized to Universal Resource Identifiers (URIs) by https://datatracker.ietf.org/doc/html/rfc2396[RFC 2396], which also introduced features such a https://datatracker.ietf.org/doc/html/rfc2396#section-3.3[path parameter]s.

This was then obsoleted in 2005 by https://datatracker.ietf.org/doc/html/rfc3986[RFC 3986] which removed the definition for path parameters.

Unfortunately by this stage the existence and use of such parameters had already been codified in the Servlet specification.
For example, the relative URI `/foo/bar;JSESSIONID=a8b38cd02b1c` would define the path parameter `JSESSIONID` for the path segment `bar`, but the most recent RFC does not specify a formal definition of what this relative URI actually means.

The current situation is that there may be URIs that are entirely valid for https://datatracker.ietf.org/doc/html/rfc3986[RFC 3986], but are ambiguous when handled by the Servlet APIs:

* A URI with `..` _and_ path parameters such as `/some/..;/path` is not https://datatracker.ietf.org/doc/html/rfc3986#section-5.2[_resolved_] by RFC 3986, since the resolution process only applies to the exact segment `..`, not to `..;`.
However, once the path parameters are removed by the Servlet APIs, the resulting `/some/../path` can easily be resolved to `/path`, rather than be treated as a path that has `..;` as a segment.
* A URI such as `/some/%2e%2e/path` is not resolved by RFC 3986, yet when URL-decoded by the Servlet APIs will result in `/some/../path` which can easily be resolved to `/path`, rather than be treated as a path that has `..` as a segment.
* A URI with empty segments like `/some//../path` may be correctly resolved to `/some/path` (the `..` removes the previous empty segment) by the Servlet APIs.
However, if the URI raw path is passed to some other APIs (for example, file system APIs) it can be interpreted as `/path` because the empty segment `//` is discarded and treated as `/`, and the `..` thus removes the `/some` segment.

In order to avoid ambiguous URIs, Jetty imposes additional URI requirements in excess of what is required by https://datatracker.ietf.org/doc/html/rfc3986[RFC 3986] compliance.

These additional requirements may optionally be violated and are defined by the link:{javadoc-url}/org/eclipse/jetty/http/UriCompliance.Violation.html[`UriCompliance.Violation`] enumeration.

These violations are then grouped into modes by the link:{javadoc-url}/org/eclipse/jetty/http/UriCompliance.html[`UriCompliance`] class, which also defines several named modes that support common deployed sets of violations, with the default being link:{javadoc-url}/org/eclipse/jetty/http/UriCompliance.html#DEFAULT[`UriCompliance.DEFAULT`].

For example:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=uriCompliance]
----

If you want to customize the violations that you want to allow, you can create your own mode using the link:{javadoc-url}/org/eclipse/jetty/http/UriCompliance.html#from(java.lang.String)[`UriCompliance.from(String)`] method:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=uriComplianceCustom]
----

[[servleturi]]
=== Servlet URI Compliance Modes

Even if the server has been configured (as above) to allow ambiguous URIs to be received, individual Servlet contexts may not allow such ambiguous URIs to be returned via some specific methods.

Specifically the `HttpServletRequest` methods: link:https://jakarta.ee/specifications/servlet/6.0/apidocs/jakarta.servlet/jakarta/servlet/http/httpservletrequest#getServletPath()[getServletPath()] and link:https://jakarta.ee/specifications/servlet/6.0/apidocs/jakarta.servlet/jakarta/servlet/http/httpservletrequest#getPathInfo()[getPathInfo()], may throw `IllegalArgumentException` for such URIs.

The intention is for safer methods, such as link:https://jakarta.ee/specifications/servlet/6.0/apidocs/jakarta.servlet/jakarta/servlet/http/httpservletrequest#getRequestURI()[getRequestURI] to be used instead.

If necessary, the `ServletHandler` can be configured to allow ambiguous URIs from all methods with link:{javadoc-url}/org/eclipse/jetty/ee10/servlet/ServletHandler.html#setDecodeAmbiguousURIs(boolean)[setDecodeAmbiguousURIs(boolean)].

[[cookie]]
== Cookie Compliance Modes

The standards for Cookies have varied greatly over time from a non-specified but de-facto standard (implemented by the first browsers), through https://tools.ietf.org/html/rfc2965[RFC 2965] and currently to https://tools.ietf.org/html/rfc6265[RFC 6265].

The link:{javadoc-url}/org/eclipse/jetty/http/CookieCompliance.Violation.html[CookieCompliance.Violation] enumeration defines the RFC requirements that may be optionally enforced by Jetty when parsing the `Cookie` HTTP header in requests and when generating the `Set-Cookie` HTTP header in responses.

These violations are then grouped into modes by the link:{javadoc-url}/org/eclipse/jetty/http/CookieCompliance.html[`CookieCompliance`] class, which also defines several named modes that support common deployed sets of violations, with the default being link:{javadoc-url}/org/eclipse/jetty/http/CookieCompliance.html#RFC6265[`CookieCompliance.RFC6265`].

For example:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=cookieCompliance]
----

If you want to customize the violations that you want to allow, you can create your own mode using the link:{javadoc-url}/org/eclipse/jetty/http/CookieCompliance.html#from(java.lang.String)[`CookieCompliance.from(String)`] method:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=cookieComplianceCustom]
----

[[multiPart]]
== MultiPart Compliance Modes

The standards for multipart uploads (also known as `multipart/form-data`) varied over time, but were eventually standardized by link:https://tools.ietf.org/html/rfc7578[RFC 7578].

The link:{javadoc-url}/org/eclipse/jetty/http/MultiPartCompliance.Violation.html[MultiPartCompliance.Violation] enumeration defines the RFC requirements that may be optionally enforced by Jetty when parsing `multipart/form-data` content in requests.

These violations are then grouped into modes by the link:{javadoc-url}/org/eclipse/jetty/http/MultiPartCompliance.html[`MultiPartCompliance`] class, which also defines several named modes that support common deployed sets of violations, with the default being link:{javadoc-url}/org/eclipse/jetty/http/MultiPartCompliance.html#RFC7578[`MultiPartCompliance.RFC7578`].

If you want to customize the violations that you want to allow, you can create your own mode using the link:{javadoc-url}/org/eclipse/jetty/http/MultiPartCompliance.html#from(java.lang.String)[`MultiPartCompliance.from(String)`] method:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=multiPartCompliance]
----

---

### Documentation Jetty Modules Programming Guide Pages Server Deploy (`documentation/jetty/modules/programming-guide/pages/server/deploy.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Web Application Deployment

When using the xref:server/index.adoc[Jetty server libraries] in your code, web applications are typically assembled by composing the required ``Handler``s, as explained in xref:server/http.adoc#handler[this section].

When the `Server` instance is started, web applications are also started; there typically is no deployer component that can deploy web applications after the `Server` instance is started.

The deployer component is the key component to deploy web applications when using Jetty as a standalone server, and it is detailed xref:operations-guide:deploy/index.adoc[here].

However, it is possible to use the Jetty server libraries in your code with the deployer component, so that you can deploy web applications after the `Server` instance is started.

The Maven artifact coordinates are:

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-deploy</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

The main components to set up are:

* A `Deployer`, by default `org.eclipse.jetty.deploy.StandardDeployer`.
The deployer component is responsible for adding and removing a `ContextHandler` (or subclass) instance, that represents a web application, from the `Handler` tree, and is responsible for the lifecycle (starting and stopping) the `ContextHandler`.
* A `DeploymentScanner`, that scans directories for files or directories that represent web applications.
Typical web application files are `+*.war+`, `+*.properties+` and `+*.xml+` files.
When a web application file or directory is added/updated/removed, `DeploymentScanner` creates or retrieves (depending on the event) the corresponding `ContextHandler` and calls the `Deployer` to deploy/redeploy/undeploy the web application.

[plantuml]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false

participant WebDir as "Web Application Directory"
participant DeploymentScanner
participant Deployer
participant ContextHandlerCollection

DeploymentScanner -> WebDir : monitors for changes
-> WebDir : create webapp.war
DeploymentScanner -> Deployer : deploy ContextHandler
Deployer -> ContextHandlerCollection : deployHandler() + start
-> WebDir : delete webapp.war
DeploymentScanner -> Deployer : undeploy ContextHandler
Deployer -> ContextHandlerCollection : stop + removeHandler()
----

Each web application is deployed to a specific _environment_.

The environment allows you to specify environmental properties that will be available to all web applications, as well as a `ClassLoader` that loads classes that are specific to the environment.

Furthermore, the environment determines how the web application file should be interpreted: if it is a directory, whether it should be interpreted as a xref:operations-guide:deploy/index.adoc#web-application-format-static[Jetty Static web application], or as a xref:operations-guide:deploy/index.adoc#web-application-format-core[Jetty Core web application], or as a xref:operations-guide:deploy/index.adoc#web-application-format-jakarta[Jakarta EE exploded `+*.war+`].

By default, there are no environments; you must always create at least one, depending on the type of deployment you want for your web applications.

[[simple]]
== Simple Deployer Setup

The simplest deployer setup is when you deploy a web application via xref:operations-guide:deploy/index.adoc#context-xml[Jetty context XML file]:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/deploy/DeployDocs.java[tags=simple]
----

With this simple setup, you can only deploy Jetty context XML files.
In the Jetty context XML files, you can only reference classes that you have in the class-path or module-path, for example:

.acme-webapp.xml
[,xml]
----
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">
<Configure class="org.eclipse.jetty.server.handler.ContextHandler">
  <Set name="contextPath">/acme</Set>
  <Set name="handler">
    <New class="com.acme.WebAppHandler" /> 
  </Set>
</Configure>
----
<1> This class must be in the class-path or module-path.

WARNING: Do not be tempted to use this simple setup to deploy Jetty Core web applications, Jetty Static web applications, or Jakarta web applications; their setup is described their respective sections below.

[[static]]
== Jetty Static Deployer Setup

For xref:operations-guide:deploy/index.adoc#web-application-format-static[Jetty Static web applications], the setup is the following:

[,java,indent=0,subs=+attributes]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/deploy/DeployDocs.java[tags=static]
----
<1> The environment is configured with its own class-path.
<2> Jetty Static web applications use `StaticAppContext` as their `ContextHandler`, so web application directories are interpreted as having the xref:operations-guide:deploy/index.adoc#web-application-format-static[Jetty Static web application format].

Your Jetty Static web application is a directory, for example `acme-asset/`, under `/path/to/webapps/`:

[,bash]
----
/path/to/webapps/
└── acme-assets/
    ├── css/
    │   └── styles.css
    └── index.html
----

[[core]]
== Jetty Core Deployer Setup

For xref:operations-guide:deploy/index.adoc#web-application-format-core[Jetty Core web applications], the setup is the following:

[,java,indent=0,subs=+attributes]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/deploy/DeployDocs.java[tags=core]
----
<1> The environment is configured with its own class-path.
<2> Jetty Core web applications use `CoreAppContext` as their `ContextHandler`, so web application directories are interpreted as having the xref:operations-guide:deploy/index.adoc#web-application-format-core[Jetty Core web application format].

Your Jetty Core web application is a directory, for example `acme-core-app/` under `/path/to/webapps/`:

[,bash]
----
/path/to/webapps/
└── acme-core-app/
    ├── static         # Static files are served from this directory.
    │   └── favicon.ico
    ├── classes        # Where web application Java classes reside.
    │   └── com
    │       └── acme
    │           └── AcmeHandler.class
    ├── lib            # Where web application Java libraries reside.
    │   └── acme-util.jar
    └── jetty-web.xml  # Web application specific configuration.
----

With this setup you have the following class loader hierarchy, with the standard _parent first_ model for class loading:

[,bash,subs=+attributes]
----
System ClassLoader                 # jetty-server-{jetty-version}.jar, etc.
└── Environment ClassLoader        # jetty-coreapp-{jetty-version}.jar
    └── Jetty Core App ClassLoader # acme-core-app/classes/:acme-core-app/lib/acme-util.jar
----

[[jakarta]]
== Jakarta Deployer Setup

For xref:operations-guide:deploy/index.adoc#web-application-format-jakarta[Jakarta web applications], the setup is the following:

[,java,indent=0,subs=+attributes]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/deploy/DeployDocs.java[tags=jakarta]
----
<1> The environment is configured with its own class-path, that includes the Servlet API `+*.jar+` and the Jetty implementation `+*.jar+`s of the Servlet APIs.
<2> Jakarta web applications use `WebAppContext` as their `ContextHandler`, so web application `+*.war+` files or directories are interpreted as having the xref:operations-guide:deploy/index.adoc#web-application-format-jakarta[Jakarta web application format].

Your Jakarta web application is a `+*.war+` file, for example `acme.war` under `/path/to/webapps/`:

[,bash]
----
/path/to/webapps/
└── acme.war
----

With this setup you have the following class loader hierarchy, with the standard Jakarta _web application first_ model for class loading:

[,bash,subs=+attributes]
----
System ClassLoader                  # jetty-server-{jetty-version}.jar: etc.
└── Environment ClassLoader         # jakarta.servlet-api-{servlet-current-version}.jar: etc.
    └── Jakarta Web App ClassLoader # acme.war!/WEB-INF/classes/:acme.war!/WEB-INF/lib/*.jar
----

[[multi]]
== Multiple Environments

If you need to deploy web applications to different environments, for example `core` and `{ee-current}`, the setup is the following:

[,java,indent=0,subs=+attributes]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/deploy/DeployDocs.java[tags=multi]
----

The class loader hierarchy is the following:

[,bash,subs=+attributes]
----
System ClassLoader
├── core Environment ClassLoader
│   └── Jetty Core App ClassLoader
└── ee11 Environment ClassLoader
    └── Jakarta Web App ClassLoader
----

With multiple environments, it is good practice to specify the web application `+*.properties+` file, as described in xref:operations-guide:deploy/index.adoc#web-application-files[this section].

---

### Documentation Jetty Modules Programming Guide Pages Server Fastcgi (`documentation/jetty/modules/programming-guide/pages/server/fastcgi.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= FastCGI Server Libraries

TODO

---

### Documentation Jetty Modules Programming Guide Pages Server Http (`documentation/jetty/modules/programming-guide/pages/server/http.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= HTTP Server Libraries

Web application development typically involves writing your web applications, optionally packaging them into a web application archive, for example the Jakarta EE `+*.war+` file, and then deploy the web application into a standalone Servlet Container that you have previously installed.

The Jetty server libraries allow you to write web applications components using either the Jetty APIs (by writing <<handler,Jetty ``Handler``s>>) or using the Jakarta EE <<handler-use-servlet,Servlet APIs>> (by writing ``Servlet``s and Servlet ``Filter``s).
These components can then be programmatically assembled together, without the need of creating a web application archive, added to a Jetty ``Server`` instance that is then started.
This result in your web applications to be available to HTTP clients as if you deployed your web applications in a standalone Jetty server.

Jetty `Handler` APIs pros:

* Simple minimalist asynchronous APIs.
* Very low overhead, only configure the features you use.
* Faster turnaround to implement new APIs or new standards.
* Normal classloading behavior (web application classloading isolation also available).

Jakarta EE APIs pros:

* Standard, well known, APIs.

The Maven artifact coordinates are:

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-server</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

An `org.eclipse.jetty.server.Server` instance is the central component that links together a collection of ``Connector``s and a collection of ``Handler``s, with threads from a `ThreadPool` doing the work.

[plantuml]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false
skinparam padding 5

scale 1.5

hide members
hide circle

Server - ThreadPool
Connectors - Server
Server -- Handlers
----

The components that accept connections from clients are `org.eclipse.jetty.server.Connector` implementations.

When a Jetty server interprets the HTTP protocol (HTTP/1.1, HTTP/2 or HTTP/3), it uses `org.eclipse.jetty.server.Handler` instances to process incoming requests and eventually produce responses.

A `Server` must be created, configured and started:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=simple]
----

The example above shows the simplest HTTP/1.1 server; it has no support for HTTP sessions, nor for HTTP authentication, nor for any of the features required by the Servlet specification.

These features (HTTP session support, HTTP authentication support, etc.) are provided by the Jetty server libraries, but not all of them may be necessary in your web application.
You need to put together the required Jetty components to provide the features required by your web applications.
The advantage is that you do not pay the cost for features that you do not use, saving resources and likely increasing performance.

The built-in ``Handler``s provided by the Jetty server libraries allow you to write web applications that have functionalities similar to Apache HTTPD or Nginx (for example: URL redirection, URL rewriting, serving static content, reverse proxying, etc.), as well as generating content dynamically by processing incoming requests.
Read <<handler,this section>> for further details about ``Handler``s.

If you are interested in writing your web application based on the Jakarta EE APIs, jump to <<handler-use-servlet,this section>>.

[[request-processing]]
== Request Processing

The Jetty HTTP request processing is outlined below in the diagram below.
You may want to refer to the xref:arch/io.adoc[Jetty I/O architecture] for additional information about the classes mentioned below.

Request handing is slightly different for each protocol; in HTTP/2 Jetty takes into account multiplexing, something that is not present in HTTP/1.1.

However, the diagram below captures the essence of request handling that is common among all protocols that carry HTTP requests.

[plantuml]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false

participant ManagedSelector
participant EndPoint
participant Connection
participant Parser
participant HttpStream
participant HttpChannel
participant Server
participant Handlers

ManagedSelector -> EndPoint : read ready
EndPoint -> Connection : onFillable()
Connection -> EndPoint : fill()
EndPoint --> Connection
Connection -> Parser : parse()
Parser -> HttpStream : events
Connection -> HttpChannel : onRequest()
HttpChannel -> Server : handle()
Server -> Handlers : handle()
----

First, the Jetty I/O layer emits an event that a socket has data to read.
This event is converted to a call to `AbstractConnection.onFillable()`, where the `Connection` first reads from the `EndPoint` into a `ByteBuffer`, and then calls a protocol specific parser to parse the bytes in the `ByteBuffer`.

The parser emit events that are protocol specific; the HTTP/2 parser, for example, emits events for each HTTP/2 frame that has been parsed, and similarly does the HTTP/3 parser.
The parser events are then converted to protocol independent events such as _"request start"_, _"request headers"_, _"request content chunk"_, etc. detailed in <<request-processing-events,this section>>.

When enough of the HTTP request is arrived, the `Connection` calls `HttpChannel.onRequest()`.

`HttpChannel.onRequest()` calls the <<request-customizers,request customizers>>, that allow to customize the request and/or the response headers on a per-``Connector`` basis.

After request customization, if any, the `Handler` tree is invoked, starting from the `Server` instance, and eventually your web application code is invoked.

[[request-processing-events]]
=== Request Processing Events

Advanced web applications may be interested in the progress of the processing of an HTTP request/response.
A typical case is to know exactly _when_ the HTTP request/response processing starts and when it is complete, for example to monitor processing times.

This is conveniently implemented by `org.eclipse.jetty.server.handler.EventsHandler`, described in more details in <<handler-use-events,this section>>.

[[request-logging]]
== Request Logging

HTTP requests and responses can be logged to provide data that can be later analyzed with other tools.
These tools can provide information such as the most frequently accessed request URIs, the response status codes, the request/response content lengths, geographical information about the clients, etc.

The default request/response log line format is the https://en.wikipedia.org/wiki/Common_Log_Format[NCSA Format] extended with referrer data and user-agent data.

[NOTE]
====
Typically, the extended NCSA format is enough and it's the standard used and understood by most log parsing tools and monitoring tools.

To customize the request/response log line format see the link:{javadoc-url}/org/eclipse/jetty/server/CustomRequestLog.html[`CustomRequestLog` javadocs].
====

Request logging can be enabled at the `Server` level.

The request logging output can be directed to an SLF4J logger named `"org.eclipse.jetty.server.RequestLog"` at `INFO` level, and therefore to any logging library implementation of your choice (see also xref:troubleshooting/logging.adoc[this section] about logging).

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=serverRequestLogSLF4J]
----

Alternatively, the request logging output can be directed to a daily rolling file of your choice, and the file name must contain `yyyy_MM_dd` so that rolled over files retain their date:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=serverRequestLogFile]
----

For maximum flexibility, you can log to multiple ``RequestLog``s using class `RequestLog.Collection`, for example by logging with different formats or to different outputs.

You can use `CustomRequestLog` with a custom `RequestLog.Writer` to direct the request logging output to your custom targets (for example, an RDBMS).
You can implement your own `RequestLog` if you want to have functionalities that are not implemented by `CustomRequestLog`.

[[request-customizers]]
== Request Customizers

A request customizer is an instance of `HttpConfiguration.Customizer`, that can customize the HTTP request and/or the HTTP response headers _before_ the `Handler` tree is invoked.

Request customizers are added to a particular `HttpConfiguration` instance, and therefore are specific to a `Connector` instance: you can have two different ``Connector``s configured with different request customizers.

For example, it is common to configure a secure `Connector` with the `SecureRequestCustomizer` that customizes the HTTP request by adding attributes that expose TLS data associated with the secure communication.

A request customizer may:

* Inspect the received HTTP request method, URI, version and headers.
* Wrap the `Request` object to allow any method to be overridden and customized. Typically this is done to synthesize additional HTTP request headers, or to change the return value of overridden methods.
* Add or modify the HTTP response headers.

The out-of-the-box request customizers include:

* `ForwardedRequestCustomizer` -- to interpret the `Forwarded` (or the the obsolete ``+X-Forwarded-*+``) HTTP header added by a reverse proxy; see <<request-customizer-forwarded,this section>>.
* `HostHeaderCustomizer` -- to customize, or synthesize it when original absent, the HTTP `Host` header; see <<request-customizer-host,this section>>.
* `ProxyCustomizer` -- to expose as `Request` attributes the `ip:port` information carried by the PROXY protocol; see <<request-customizer-proxy,this section>>.
* `RewriteCustomizer` -- to rewrite the request URI; see <<request-customizer-rewrite,this section>>.
* `SecureRequestCustomizer` -- to expose TLS data via `Request` attributes; see <<request-customizer-secure,this section>>.

You can also write your own request customizers and add them to the `HttpConfiguration` instance along existing request customizers.
Multiple request customizers will be invoked in the order they have been added.

Below you can find an example of how to add a request customizer:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=requestCustomizer]
----

[[request-customizer-forwarded]]
=== `ForwardedRequestCustomizer`

`ForwardedRequestCustomizer` should be added when Jetty receives requests from a reverse proxy on behalf of a remote client, and web applications need to access the remote client information.

The reverse proxy adds the `Forwarded` (or the obsolete ``+X-Forwarded-*+``) HTTP header to the request, and may offload TLS so that the request arrives in clear-text to Jetty.

Applications deployed in Jetty may need to access information related to the remote client, for example the remote IP address and port, or whether the request was sent through a secure communication channel.
However, the request is forwarded by the reverse proxy, so the direct information about the remote IP address is that of the proxy, not of the remote client.
Furthermore, the proxy may offload TLS and forward the request in clear-text, so that the URI scheme would be `http` as forwarded by the reverse proxy, not `https` as sent by the remote client.

`ForwardedRequestCustomizer` reads the `Forwarded` header where the reverse proxy saved the remote client information, and wraps the original `Request` so that applications will transparently see the remote client information when calling methods such as `Request.isSecure()`, or `Request.getConnectionMetaData().getRemoteSocketAddress()`, etc.

For more information about how to configure `ForwardedRequestCustomizer`, see also link:{javadoc-url}/org/eclipse/jetty/server/ForwardedRequestCustomizer.html[the javadocs].

[[request-customizer-host]]
=== `HostHeaderCustomizer`

`HostHeaderCustomizer` should be added when Jetty receives requests that may lack the `Host` HTTP header, such as HTTP/1.0, HTTP/2 or HTTP/3 requests, and web applications have logic that depends on the value of the `Host` HTTP header.

For HTTP/2 and HTTP/3, the `Host` HTTP header is missing because the authority information is carried by the `:authority` pseudo-header, as per the respective specifications.

`HostHeaderCustomizer` will look at the `:authority` pseudo-header, then wrap the original `Request` adding a `Host` HTTP header synthesized from the `:authority` pseudo-header.
In this way, web applications that rely on the presence of the `Host` HTTP header will work seamlessly in any HTTP protocol version.

`HostHeaderCustomizer` works also for the WebSocket protocol.

WebSocket over HTTP/2 or over HTTP/3 initiate the WebSocket communication with an HTTP request that only has the `:authority` pseudo-header.
`HostHeaderCustomizer` synthesizes the `Host` HTTP header for such requests, so that WebSocket web applications that inspect the initial HTTP request before the WebSocket communication will work seamlessly in any HTTP protocol version.

For more information about how to configure `HostHeaderCustomizer`, see also link:{javadoc-url}/org/eclipse/jetty/server/HostHeaderCustomizer.html[the javadocs].

[[request-customizer-authority]]
=== `AuthorityCustomizer`

`AuthorityCustomizer` should be added when Jetty receives HTTP/2 or HTTP/3 requests that lack the  `:authority` pseudo-header, and web applications have logic that depends on this value, exposed through the `Request` URI authority via `Request.getHttpURI().getAuthority()`.

The `:authority` pseudo-header may be missing if the request arrived to a proxy in HTTP/1.1 format, and the proxy is converting it to HTTP/2 or HTTP/3 before sending it to the backend server.

`AuthorityCustomizer` will synthesize the authority using the `Host` header field, if present.
If the `Host` header is also missing, it will use the request _server name_ and _server port_, values that may be influenced by the <<request-customizer-forwarded,`Forwarded` HTTP header>> and the <<connector-protocol-proxy-http11,PROXY protocol>>.

The synthesized authority will be exposed as the `Request` URI authority via `Request.getHttpURI().getAuthority()`.

[[request-customizer-proxy]]
=== `ProxyCustomizer`

`ProxyCustomizer` should be added when Jetty receives requests from a reverse proxy on behalf of a remote client, prefixed by the PROXY protocol (see also this section about the <<connector-protocol-proxy-http11,PROXY protocol>>).

`ProxyCustomizer` adds the reverse proxy IP address and port as `Request` attributes.
Web applications may use these attributes in conjunction with the data exposed by `ForwardedRequestCustomizer` (see <<request-customizer-forwarded,this section>>).

For more information about how to configure `ProxyCustomizer`, see also link:{javadoc-url}/org/eclipse/jetty/server/ProxyCustomizer.html[the javadocs].

[[request-customizer-rewrite]]
=== `RewriteCustomizer`

`RewriteCustomizer` is similar to `RewriteHandler` (see <<handler-use-rewrite,this section>>), but a `RewriteCustomizer` cannot send a response or otherwise complete the request/response processing.

A `RewriteCustomizer` is mostly useful if you want to rewrite the request URI _before_ the `Handler` tree is invoked.
However, a very similar effect can be achieved by having the `RewriteHandler` as the first `Handler` (the child `Handler` of the `Server` instance).

Since `RewriteCustomizer` cannot send a response or complete the request/response processing, ``Rule``s that do so such as redirect rules have no effect and are ignored; only ``Rule``s that modify or wrap the `Request` will have effect and be applied.

Due to this limitation, it is often a better choice to use `RewriteHandler` instead of `RewriteCustomizer`.

For more information about how to configure `RewriteCustomizer`, see also link:{javadoc-url}/org/eclipse/jetty/rewrite/RewriteCustomizer.html[the javadocs].

[[request-customizer-secure]]
=== `SecureRequestCustomizer`

`SecureRequestCustomizer` should be added when Jetty receives requests over a secure `Connector`.

`SecureRequestCustomizer` adds TLS information as request attributes, in particular an instance of `EndPoint.SslSessionData` that contains information about the negotiated TLS cipher suite and possibly client certificates, and an instance of `org.eclipse.jetty.util.ssl.X509` that contains information about the server certificate.

`SecureRequestCustomizer` also adds, if configured so, the `Strict-Transport-Security` HTTP response header (for more information about this header, see https://datatracker.ietf.org/doc/html/rfc6797[its specification]).

For more information about how to configure `SecureRequestCustomizer`, see also link:{javadoc-url}/org/eclipse/jetty/server/SecureRequestCustomizer.html[the javadocs].

[[connector]]
== Server Connectors

A `Connector` is the component that handles incoming requests from clients, and works in conjunction with `ConnectionFactory` instances.

The available implementations are:

* `org.eclipse.jetty.server.ServerConnector`, for TCP/IP sockets.
* `org.eclipse.jetty.unixdomain.server.UnixDomainServerConnector` for Unix-Domain sockets (requires Java 16 or later).
* `org.eclipse.jetty.quic.server.QuicServerConnector`, for the low-level QUIC protocol and HTTP/3.
* `org.eclipse.jetty.server.MemoryConnector`, for memory communication between client and server.

`ServerConnector` and `UnixDomainServerConnector` use a `java.nio.channels.ServerSocketChannel` to listen to a socket address and to accept socket connections.
`QuicheServerConnector` uses a `java.nio.channels.DatagramChannel` to listen to incoming UDP packets.
`MemoryConnector` uses memory for the communication between client and server, avoiding the use of sockets.

Since `ServerConnector` wraps a `ServerSocketChannel`, it can be configured in a similar way, for example the TCP port to listen to, the IP address to bind to, etc.:

[,java,indent=0,options=nowrap]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=configureConnector]
----

`UnixDomainServerConnector` also wraps a `ServerSocketChannel` and can be configured with the Unix-Domain path to listen to:

[,java,indent=0,options=nowrap]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=configureConnectorUnix]
----

[IMPORTANT]
====
You can use Unix-Domain sockets only when you run your server with Java 16 or later.
====

`QuicheServerConnector` wraps a `DatagramChannel` and can be configured in a similar way, as shown in the example below.
Since the communication via UDP does not require to "accept" connections like TCP does, the number of <<connector-acceptors,acceptors>> is set to `0` and there is no API to configure their number.

[,java,indent=0,options=nowrap]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=configureConnectorQuic]
----

`MemoryConnector` uses in-process memory, not sockets, for the communication between client and server, that therefore must be in the same process.

Typical usage of `MemoryConnector` is the following:

[,java,indent=0,options=nowrap]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=memoryConnector]
----

[[connector-acceptors]]
=== Acceptors

The _acceptors_ are threads (typically only one) that compete to accept TCP socket connections.
The connectors for the QUIC or HTTP/3 protocol, based on UDP, have zero acceptors.

When a TCP connection is accepted by a `ServerSocketChannel`, `ServerConnector` wraps the accepted `SocketChannel` and registers it to the xref:arch/io.adoc#selector-manager[`SelectorManager`].
Therefore, there is a little moment where the acceptor thread is not accepting new connections because it is busy wrapping the just accepted connection to pass it to the `SelectorManager`.
Connections that are ready to be accepted but are not accepted yet are queued in a bounded queue (at the OS level) whose capacity can be configured with the `acceptQueueSize` parameter.

If your application must withstand a very high rate of connection opening, configuring more than one acceptor thread may be beneficial: when one acceptor thread accepts one connection, another acceptor thread can take over accepting connections.

The number of acceptors may be set to `0`.
In this case, the `ServerSocketChannel` will be configured in non-blocking mode, and registered in the `SelectorManager`.

New connections will be accepted in non-blocking mode by the `SelectorManager`, and when the connection is completely established, the accepted `SocketChannel` is wrapped and registered in the `SelectorManager` as before.

Configuring `0` acceptors uses fewer threads, but may result in increased latency, since accepting connections is now competing with established connections in the Java NIO mechanism.

[[connector-selectors]]
=== Selectors

The _selectors_ are components that manage a set of accepted TCP sockets, implemented by xref:arch/io.adoc#selector-manager[`ManagedSelector`].
For QUIC or HTTP/3, there are no accepted TCP sockets, but only one `DatagramChannel` and therefore there is only one selector.

Each selector requires one thread and uses the Java NIO mechanism to efficiently handle a set of registered channels.

As a rule of thumb, a single selector can easily manage up to 1000-5000 TCP sockets, although the number may vary greatly depending on the application.

For example, web applications for websites tend to use TCP sockets for one or more HTTP requests to retrieve resources and then the TCP socket is idle for most of the time.
In this case a single selector may be able to manage many TCP sockets because chances are that they will be idle most of the time.
On the contrary, web messaging applications or REST applications tend to send many small messages at a very high frequency so that the TCP sockets are rarely idle.
In this case a single selector may be able to manage less TCP sockets because chances are that many of them will be active at the same time, so you may need more than one selector.

[[connector-multiple]]
=== Multiple Connectors

It is possible to configure more than one `Connector` per `Server`.
Typical cases are a `ServerConnector` for clear-text HTTP, and another `ServerConnector` for secure HTTP.
Another case could be a publicly exposed `ServerConnector` for secure HTTP, and an internally exposed `UnixDomainServerConnector` or `MemoryConnector` for clear-text HTTP.
Yet another example could be a `ServerConnector` for clear-text HTTP, a `ServerConnector` for secure HTTP/2, and an `QuicheServerConnector` for QUIC+HTTP/3.

For example:

[,java,indent=0,options=nowrap]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=configureConnectors]
----

If you do not specify the port the connector listens to explicitly, the OS will allocate one randomly when the connector starts.

You may need to use the randomly allocated port to configure other components.
One example is to use the randomly allocated port to configure secure redirects (when redirecting from a URI with the `http` scheme to the `https` scheme).
Another example is to bind both the HTTP/2 connector and the HTTP/3 connector to the same randomly allocated port.
It is possible that the HTTP/2 connector and the HTTP/3 connector share the same port, because one uses TCP, while the other uses UDP.

For example:

[,java,indent=0,options=nowrap]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=sameRandomPort]
----

[[connector-limiting]]
=== Limiting Connections

It is possible to limit the number of TCP connections accepted by the whole server (and therefore across all connectors), or by a specific connector.

This feature is implemented by class `org.eclipse.jetty.server.NetworkConnectionLimit` and you can use it in this way:

[,java,indent=0,options=nowrap]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=networkConnectionLimit]
----

[NOTE]
====
When the maximum number of TCP connections is reached, no more TCP connections will be accepted _at the JVM level_ -- but they could be accepted at the OS level.

This means that if you are using OS tools (like Linux's `ss`) to count the number of established TCP connections, you may find a number that may be greater than the maximum number of TCP connections configured in a `NetworkConnectionLimit`.

Note also that different operative systems may behave differently when Jetty is not accepting TCP connections: some OS accepts connections at the TCP level anyway (but does not notify this event to the JVM), some other OS may not accept connections at the TCP level.
====

[[connector-protocol]]
=== Configuring Protocols

A server `Connector` can be configured with one or more ``ConnectionFactory``s, and this list of ``ConnectionFactory``s represents the protocols that the `Connector` can understand.
If no `ConnectionFactory` is specified then `HttpConnectionFactory` is implicitly configured.

For each accepted connection, the server `Connector` asks a `ConnectionFactory` to create a `Connection` object that handles the traffic on that connection, parsing and generating bytes for a specific protocol (see xref:arch/io.adoc[this section] for more details about `Connection` objects).

TIP: You can listen for `Connection` open and close events as detailed in xref:arch/io.adoc#connection-listener[this section].

Secure protocols like secure HTTP/1.1, secure HTTP/2 or HTTP/3 (HTTP/3 is intrinsically secure -- there is no clear-text HTTP/3) require an `SslContextFactory.Server` to be configured with a KeyStore.

For HTTP/1.1 and HTTP/2, `SslContextFactory.Server` is used in conjunction with `SSLEngine`, which drives the TLS handshake that establishes the secure communication.

Applications may register a `org.eclipse.jetty.io.ssl.SslHandshakeListener` to be notified of TLS handshakes success or failure, by adding the `SslHandshakeListener` as a bean to the `Connector`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=sslHandshakeListener]
----

[[connector-protocol-http11]]
==== Clear-Text HTTP/1.1

`HttpConnectionFactory` creates `HttpConnection` objects that parse bytes and generate bytes for the HTTP/1.1 protocol.

This is how you configure Jetty to support clear-text HTTP/1.1:

[,java,indent=0,options=nowrap]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=http11]
----

[[connector-protocol-http11-tls]]
==== Encrypted HTTP/1.1 (https)

Supporting encrypted HTTP/1.1 (that is, requests with the `https` scheme) is supported by configuring an `SslContextFactory` that has access to the KeyStore containing the private server key and public server certificate, in this way:

[,java,indent=0,options=nowrap]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=tlsHttp11]
----

You can customize the SSL/TLS provider as explained in <<connector-protocol-tls-conscrypt,this section>>.

[[connector-protocol-http2]]
==== Clear-Text HTTP/2

It is well known that the HTTP ports are `80` (for clear-text HTTP) and `443` for encrypted HTTP.
By using those ports, a client had _prior knowledge_ that the server would speak, respectively, the HTTP/1.x protocol and the TLS protocol (and, after decryption, the HTTP/1.x protocol).

HTTP/2 was designed to be a smooth transition from HTTP/1.1 for users and as such the HTTP ports were not changed.
However the HTTP/2 protocol is, on the wire, a binary protocol, completely different from HTTP/1.1.
Therefore, with HTTP/2, clients that connect to port `80` (or to a specific Unix-Domain path) may speak either HTTP/1.1 or HTTP/2, and the server must figure out which version of the HTTP protocol the client is speaking.

Jetty can support both HTTP/1.1 and HTTP/2 on the same clear-text port by configuring both the HTTP/1.1 and the HTTP/2 ``ConnectionFactory``s:

[,java,indent=0,options=nowrap]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=http11H2C]
----

Note how the ``ConnectionFactory``s passed to `ServerConnector` are in order: first HTTP/1.1, then HTTP/2.
This is necessary to support both protocols on the same port: Jetty will start parsing the incoming bytes as HTTP/1.1, but then realize that they are HTTP/2 bytes and will therefore _upgrade_ from HTTP/1.1 to HTTP/2.

This configuration is also typical when Jetty is installed in backend servers behind a load balancer that also takes care of offloading TLS.
When Jetty is behind a load balancer, you can always prepend the PROXY protocol as described in <<connector-protocol-proxy-http11,this section>>.

[[connector-protocol-http2-tls]]
==== Encrypted HTTP/2

When using encrypted HTTP/2, the unencrypted protocol is negotiated by client and server using an extension to the TLS protocol called ALPN.

Jetty supports ALPN and encrypted HTTP/2 with this configuration:

[,java,indent=0,options=nowrap]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=tlsALPNHTTP]
----

Note how the ``ConnectionFactory``s passed to `ServerConnector` are in order: TLS, ALPN, HTTP/2, HTTP/1.1.

Jetty starts parsing TLS bytes so that it can obtain the ALPN extension.
With the ALPN extension information, Jetty can negotiate a protocol and pick, among the ``ConnectionFactory``s supported by the `ServerConnector`, the `ConnectionFactory` correspondent to the negotiated protocol.

The fact that the HTTP/2 protocol comes before the HTTP/1.1 protocol indicates that HTTP/2 is the preferred protocol for the server.

Note also that the default protocol set in the ALPN ``ConnectionFactory``, which is used in case ALPN is not supported by the client, is HTTP/1.1 -- if the client does not support ALPN is probably an old client so HTTP/1.1 is the safest choice.

You can customize the SSL/TLS provider as explained in <<connector-protocol-tls-conscrypt,this section>>.

[[connector-protocol-http3]]
==== HTTP/3

The HTTP/3 protocol is layered on top of the https://datatracker.ietf.org/doc/html/rfc9000[QUIC] protocol, which is based on UDP.
This is rather different with respect to HTTP/1 and HTTP/2, that are based on TCP.

Jetty only implements the HTTP/3 layer in Java; the QUIC implementation is provided by the https://github.com/cloudflare/quiche[Quiche] native library, that Jetty calls via https://github.com/java-native-access/jna[JNA] (and possibly, in the future, via the https://openjdk.org/jeps/454[Foreign APIs]).

NOTE: Jetty's HTTP/3 support can only be used on the platforms (OS and CPU) supported by the Quiche native library.

HTTP/3 clients may not know in advance if the server supports QUIC (over UDP), but the server typically supports either HTTP/1 or HTTP/2 (over TCP) on the default HTTP secure port `443`, and advertises the availability HTTP/3 as an https://datatracker.ietf.org/doc/html/rfc7838[_HTTP alternate service_], possibly on a different port and/or on a different host.

For example, an HTTP/2 response may include the following header:

[source]
----
Alt-Svc: h3=":843"
----

The presence of this header indicates that protocol `h3` is available on the same host (since no host is defined before the port), but on port `843` (although it may be the same port `443`).
The `ma` (max-age) attribute specifies how long (in seconds) the client should cache this information.
The HTTP/3 client may now initiate a QUIC connection on port `843` and make HTTP/3 requests.

NOTE: It is nowadays common to use the same port `443` for both HTTP/2 and HTTP/3. This does not cause problems because HTTP/2 listens on the TCP port `443`, while QUIC listens on the UDP port `443`.

NOTE: The `Alt-Svc` header is automatically added to HTTP/2 responses if an HTTP/3 connector is present, advertising the HTTP/3 connector's port.
By default, only the port is advertised without additional attributes.
The `ma` (max-age) attribute can be configured via `AltSvcCustomizer.setMaxAge(Duration)` and the `persist` attribute via `AltSvcCustomizer.setPersist(boolean)`.

To configure the `AltSvcCustomizer`, retrieve it from the `HttpConfiguration` after creating the `HTTP2ServerConnectionFactory`:

[,java,indent=0,options=nowrap]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=h2altsvc]
----

It is therefore common for HTTP/3 clients to initiate connections using the HTTP/2 protocol over TCP, and if the server supports HTTP/3 switch to HTTP/3 as indicated by the server.

[plantuml]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false

participant client
participant "server:443" as h2server
participant "server:843" as h3server

client -> h2server : HTTP/2 request
h2server -> client : HTTP/2 response\nAlt-Svc: h3=":843"
client -> h3server : HTTP/3 requests
h3server -> client : HTTP/3 responses
...
----

The code necessary to configure HTTP/2 is described in <<connector-protocol-http2-tls,this section>>.

To setup HTTP/3, for example on port `843`, you need the following code (some of which could be shared with other connectors such as HTTP/2's):

[,java,indent=0,options=nowrap]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=h3]
----

[IMPORTANT]
====
The use of the Quiche native library requires the private key and public certificate present in the KeyStore to be exported as PEM files for Quiche to use them.

It is therefore mandatory to configure the PEM directory as shown above.

The PEM directory must also be adequately protected using file system permissions, because it stores the private key PEM file.
You want to grant as few permissions as possible, typically the equivalent of POSIX `rwx` only to the user that runs the Jetty process. Using `/tmp` or any other directory accessible by any user is not a secure choice.
====

[[connector-protocol-tls-conscrypt]]
==== Using Conscrypt as SSL/TLS Provider

If not explicitly configured, the TLS implementation is provided by the JDK you are using at runtime.

OpenJDK's vendors may replace the default TLS provider with their own, but you can also explicitly configure an alternative TLS provider.

The standard TLS provider from OpenJDK is implemented in Java (no native code), and its performance is not optimal, both in CPU usage and memory usage.

A faster alternative, implemented natively, is Google's https://github.com/google/conscrypt/[Conscrypt], which is built on https://boringssl.googlesource.com/boringssl/[BoringSSL], which is Google's fork of https://www.openssl.org/[OpenSSL].

CAUTION: As Conscrypt eventually binds to a native library, there is a higher risk that a bug in Conscrypt or in the native library causes a JVM crash, while the Java implementation will not cause a JVM crash.

To use Conscrypt as TLS provider, you must have the Conscrypt jar and the Jetty dependency `jetty-alpn-conscrypt-server-{jetty-version}.jar` in the class-path or module-path.

Then, you must configure the JDK with the Conscrypt provider, and configure Jetty to use the Conscrypt provider, in this way:

[,java,indent=0,options=nowrap]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=conscrypt]
----

[[connector-protocol-tls-bouncycastle]]
==== Using Bouncy Castle as SSL/TLS Provider

If not explicitly configured, the TLS implementation is provided by the JDK you are using at runtime.

OpenJDK's vendors may replace the default TLS provider with their own, but you can also explicitly configure an alternative TLS provider.

link:https://www.bouncycastle.org/[Bouncy Castle] is a pure Java alternative provider for the Java security APIs, and includes a TLS provider.

To use Bouncy Castle as TLS provider, you must have the Bouncy Castle jars and the Jetty dependency `jetty-alpn-bouncycastle-server-{jetty-version}.jar` in the class-path or module-path.

Then, you must configure the JDK with the Bouncy Castle provider, and configure Jetty to use the Bouncy Castle provider, in this way:

[,java,indent=0,options=nowrap]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=bouncyCastle]
----

[[connector-protocol-tls-keystore-auto-reload]]
==== SslContextFactory KeyStore Auto-Reload

Jetty can be configured to monitor the KeyStore file specified in the `SslContextFactory`, and reload the `SslContextFactory` if any changes are detected to the KeyStore file.

If changes need to be done to other files, such as the TrustStore file, this must be done before the change to the Keystore file which will then trigger the `SslContextFactory` reload.

For embedded usage the `KeyStoreScanner` should be created for the specific `SslContextFactory` and added as a bean on the `Server`.

[,java,indent=0,options=nowrap]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=keyStoreScanner]
----

[[connector-protocol-proxy-http11]]
==== Jetty Behind a Load Balancer

It is often the case that Jetty receives connections from a load balancer configured to distribute the load among many Jetty backend servers.

From the Jetty point of view, all the connections arrive from the load balancer, rather than the real clients, but is possible to configure the load balancer to forward the real client IP address and IP port to the backend Jetty server using the https://www.haproxy.org/download/2.1/doc/proxy-protocol.txt[PROXY protocol].

NOTE: The PROXY protocol is widely supported by load balancers such as http://cbonte.github.io/haproxy-dconv/2.2/configuration.html#5.2-send-proxy[HAProxy] (via its `send-proxy` directive), https://docs.nginx.com/nginx/admin-guide/load-balancer/using-proxy-protocol[Nginx](via its `proxy_protocol on` directive) and others.

To support this case, Jetty can be configured in this way:

[,java,indent=0,options=nowrap]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=proxyHTTP]
----

Note how the ``ConnectionFactory``s passed to `ServerConnector` are in order: first PROXY, then HTTP/1.1.
Note also how the PROXY `ConnectionFactory` needs to know its _next_ protocol (in this example, HTTP/1.1).

Each `ConnectionFactory` is asked to create a `Connection` object for each accepted TCP connection; the `Connection` objects will be chained together to handle the bytes, each for its own protocol.
Therefore the `ProxyConnection` will handle the PROXY protocol bytes and `HttpConnection` will handle the HTTP/1.1 bytes producing a request object and response object that will be processed by ``Handler``s.

The load balancer may be configured to communicate with Jetty backend servers via Unix-Domain sockets (requires Java 16 or later).
For example:

[,java,indent=0,options=nowrap]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=proxyHTTPUnix]
----

Note that the only difference when using Unix-Domain sockets is instantiating `UnixDomainServerConnector` instead of `ServerConnector` and configuring the Unix-Domain path instead of the IP port.

[[handler]]
== Server Handlers

An `org.eclipse.jetty.server.Handler` is the component that processes incoming HTTP requests and eventually produces HTTP responses.

``Handler``s can process the HTTP request themselves, or they can be ``Handler.Container``s that delegate HTTP request processing to one or more contained ``Handler``s.
This allows ``Handler``s to be organized as a tree comprised of:

* Leaf ``Handler``s that generate a response, complete the `Callback`, and return `true` from the `handle(\...)` method.
* A `Handler.Wrapper` can be used to form a chain of ``Handler``s where request, response or callback objects may be wrapped in the `handle(\...)` method before being passed down the chain.
* A `Handler.Sequence` that contains a sequence of ``Handler``s, with each `Handler` being called in sequence until one returns `true` from its `handle(\...)` method.
* A specialized `Handler.Container` that may use properties of the request (for example, the URI, or a header, etc.) to select from one or more contained ``Handler``s to delegate the HTTP request processing to, for example link:{javadoc-url}/org/eclipse/jetty/server/handler/PathMappingsHandler.html[`PathMappingsHandler`].

A `Handler` tree is created by composing ``Handler``s together:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=handlerTree]
----

The corresponding `Handler` tree structure looks like the following:

[,screen]
----
Server
└── GzipHandler
    └── Handler.Sequence
        ├── App1Handler
        └── App2Handler
----

You should prefer using existing ``Handler``s provided by the Jetty server libraries for managing web application contexts, security, HTTP sessions and Servlet support.
Refer to <<handler-use,this section>> for more information about how to use the ``Handler``s provided by the Jetty server libraries.

You should write your own leaf ``Handler``s to implement your web application logic.
Refer to <<handler-impl,this section>> for more information about how to write your own ``Handler``s.

A `Handler` may be declared as non-blocking (by extending `Handler.Abstract.NonBlocking`) or as blocking (by extending `Handler.Abstract`), to allow interaction with the xref:arch/threads.adoc[Jetty threading architecture] for more efficient thread and CPU utilization during the request/response processing.

Container ``Handler``s typically inherit whether they are blocking or non-blocking from their child or children.

Furthermore, container ``Handler``s may be declared as dynamic: they allow adding/removing child ``Handler``s after they have been started (see link:{javadoc-url}/org/eclipse/jetty/server/Handler.AbstractContainer.html[Handler.AbstractContainer] for more information).
Dynamic container ``Handler``s are therefore always blocking, as it is not possible to know if a child `Handler` added in the future will be blocking or non-blocking.

If the `Handler` tree is not dynamic, then it is possible to create a non-blocking `Handler` tree, for example:

[,screen]
----
Server
└── RewriteHandler
    └── GzipHandler
        └── ContextHandler
            └── AppHandler extends Handler.Abstract.NonBlocking
----

When the `Handler` tree is non-blocking, Jetty may use the xref:arch/threads.adoc#execution-strategy-pc[`Produce-Consume`] mode to invoke the `Handler` tree, therefore avoiding a thread hand-off, and saving the cost of being scheduled on a different CPU with cold caches.

The `Produce-Consume` mode is equivalent to what other servers call "event loop" or "selector thread loop" architectures.

This mode has the benefit of reducing OS context switches and CPU cache misses, using fewer threads, and it is overall very efficient.
On the other hand, it requires writing quick, non-blocking code, and partially sequentializes the request/response processing, so that the Nth request in the sequence pays the latency of the processing of the N-1 requests in front of it.

[IMPORTANT]
====
If you declare your `Handler` to be non-blocking by extending `Handler.Abstract.NonBlocking`, the code you write in `handle(\...)` (and recursively all the code called from there) must truly be non-blocking, and possibly execute quickly.

If the code blocks, you risk a server lock-up.
If the code takes a long time to execute, requests from other connections may be delayed.
====

[[handler-use]]
=== Jetty Handlers

Web applications are the unit of deployment in an HTTP server or Servlet Container such as Jetty.

Two different web applications are typically deployed on different __context path__s, where a _context path_ is the initial segment of the URI path.
For example, web application `webappA` that implements a web user interface for an e-commerce site may be deployed to context path `/shop`, while web application `webappB` that implements a REST API for the e-commerce business may be deployed to `/api`.

A client making a request to URI `/shop/cart` is directed by Jetty to `webappA`, while a request to URI `/api/products` is directed to `webappB`.

An alternative way to deploy the two web applications of the example above is to use _virtual hosts_.
A _virtual host_ is a subdomain of the primary domain that shares the same IP address with the primary domain.
If the e-commerce business primary domain is `domain.com`, then a virtual host for `webappA` could be `shop.domain.com`, while a virtual host for `webappB` could be `api.domain.com`.

Web application `webappA` can now be deployed to virtual host `shop.domain.com` and context path `/`, while web application `webappB` can be deployed to virtual host `api.domain.com` and context path `/`.
Both applications have the same context path `/`, but they can be distinguished by the subdomain.

A client making a request to `+https://shop.domain.com/cart+` is directed by Jetty to `webappA`, while a request to `+https://api.domain.com/products+` is directed to `webappB`.

Therefore, in general, a web application is deployed to a _context_ which can be seen as the pair `(virtual_host, context_path)`.
In the first case the contexts were `(domain.com, /shop)` and `(domain.com, /api)`, while in the second case the contexts were `(shop.domain.com, /)` and `(api.domain.com, /)`.
Server applications using the Jetty Server Libraries create and configure a _context_ for each web application.
Many __context__s can be deployed together to enrich the web application offering -- for example a catalog context, a shop context, an API context, an administration context, etc.

Web applications can be written using exclusively the Jakarta EE APIs, since developers know them well, and because they guarantee better portability across Servlet Container implementations, as described in <<handler-use-servlet,this section>>.

On the other hand, web applications can be written using the Jetty APIs, for better performance, or to be able to  access to Jetty specific APIs, or to use features such as redirection from HTTP to HTTPS, support for `gzip` content compression, URI rewriting, etc.
The Jetty Server Libraries provides a number of out-of-the-box ``Handler``s that implement the most common functionalities and are described in the next sections.

[[handler-use-context]]
==== ContextHandler

`ContextHandler` is a `Handler` that represents a _context_ for a web application.
It is a `Handler.Wrapper` that performs some action before and after delegating to the nested `Handler`.
// TODO: expand on what the ContextHandler does, e.g. ServletContext.

The simplest use of `ContextHandler` is the following:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=contextHandler]
----

The `Handler` tree structure looks like the following:

[,screen]
----
Server
└── ContextHandler /shop
    └── ShopHandler
----

[[handler-use-context-collection]]
==== ContextHandlerCollection

You may need to deploy to Jetty more than one web application.

Recall from the <<handler,introduction>> that Jetty offers `Handler.Collection` that contains a sequence of child ``Handler``s.
However, this has no knowledge of the concept of _context_ and just iterates through the sequence of ``Handler``s.

A better choice for multiple web application is `ContextHandlerCollection`, that matches a _context_ from either its _context path_ or _virtual host_, without iterating through the ``Handler``s.

If `ContextHandlerCollection` does not find a match, it just returns `false` from its `handle(\...)` method.
What happens next depends on the `Handler` tree structure: other ``Handler``s may be invoked after `ContextHandlerCollection`, for example `DefaultHandler` (see <<handler-use-default,this section>>).
Eventually, if no `Handler` returns `true` from their own `handle(\...)` method, then Jetty returns an HTTP `404` response to the client.

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=contextHandlerCollection]
----

The `Handler` tree structure looks like the following:

[,screen]
----
Server
└── ContextHandlerCollection
    ├── ContextHandler /shop
    │   └── ShopHandler
    └── ContextHandler /api
        └── RESTHandler
----

[NOTE]
====
It is possible to split the handling of HTTP requests _within_ the same web application using <<handler-use-path-mappings,`PathMappingsHandler`>>.
====

[[handler-use-resource]]
==== ResourceHandler -- Static Content

Static content such as images or files (HTML, JavaScript, CSS) can be sent by Jetty very efficiently because Jetty can write the content asynchronously, using direct ``ByteBuffer``s to minimize data copy, and using a memory cache for faster access to the data to send.

Being able to write content asynchronously means that if the network gets congested (for example, the client reads the content very slowly) and the server stalls the send of the requested data, then Jetty will wait to resume the send _without_ blocking a thread to finish the send.

`ResourceHandler` supports the following features:

* Welcome files, for example serving `/index.html` for request URI `/`
* Precompressed resources, serving a precompressed `/document.txt.gz` for request URI `/document.txt`
* https://tools.ietf.org/html/rfc7233[Range requests], for requests containing the `Range` header, which allows clients to pause and resume downloads of large files
* Directory listing, serving a HTML page with the file list of the requested directory
* Conditional headers, for requests containing the `If-Match`, `If-None-Match`, `If-Modified-Since`, `If-Unmodified-Since` headers.

The number of features supported and the efficiency in sending static content are on the same level as those of common front-end servers used to serve static content such as Nginx or Apache.
Therefore, the traditional architecture where Nginx/Apache was the front-end server used only to send static content and Jetty was the back-end server used only to send dynamic content is somehow obsolete as Jetty can perform efficiently both tasks.
This leads to simpler systems (less components to configure and manage) and more performance (no need to proxy dynamic requests from front-end servers to back-end servers).

NOTE: It is common to use Nginx/Apache as load balancers, or as rewrite/redirect servers.
We typically recommend https://haproxy.org[HAProxy] as load balancer, and Jetty has <<handler-use-rewrite,rewrite/redirect features>> as well.

This is how you configure a `ResourceHandler` to create a simple file server:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=resourceHandler]
----

If you need to serve static resources from multiple directories:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=multipleResourcesHandler]
----

If the resource is not found, `ResourceHandler` will not return `true` from the `handle(\...)` method, so what happens next depends on the `Handler` tree structure.
See also <<handler-use-default,how to use>> `DefaultHandler`.

[[handler-use-conditional]]
==== ConditionalHandler

`ConditionalHandler` is an abstract `Handler` that matches conditions about the request, and allows subclasses to handle the request differently depending on whether the conditions have been met or not.

You may subclass `ConditionalHandler.Abstract` and override these methods:

* `boolean onConditionsMet(Request, Response, Callback)`, to handle the request when the conditions are met.
* `boolean onConditionsNotMet(Request, Response, Callback)`, to handle the request when the conditions are not met.

Alternatively, you can use the following `ConditionalHandler` subclasses that implement common behaviors:

* `ConditionalHandler.ElseNext`:
** When conditions are met, you have to write the implementation.
** When conditions are not met, the handling of the request is forwarded to the next `Handler`.
* `ConditionalHandler.Reject`:
** When conditions are met, a response is written with a configurable HTTP status code.
** When conditions are not met, the handling of the request is forwarded to the next `Handler`.
* `ConditionalHandler.SkipNext`:
** When conditions are met, the handling of the request is forwarded to the next-next `Handler`, skipping the next `Handler`.
** When conditions are not met, the handling of the request is forwarded to the next `Handler`.
* `ConditionalHandler.DontHandle`:
** When conditions are met, the request is not handled.
** When conditions are not met, the handling of the request is forwarded to the next `Handler`.
* `ConditionalHandler.NextElseReject`:
** When conditions are met, the handling of the request is forwarded to the next `Handler`.
** When conditions are not met, a response is written with a configurable HTTP status code.

Conditions can be specified using an include/exclude mechanism, where a condition match if it is not excluded, and either it is included or the include set is empty.

For example, you can specify to match only the specific HTTP request method `DELETE` by adding it to the include set; or you can specify to match all HTTP request methods apart `TRACE` by adding it to the exclude set.

`ConditionalHandler` allows you to specify conditions for the following request properties:

* The request HTTP method.
* The request link:{javadoc-url}/org/eclipse/jetty/server/Request.html#getPathInContext(org.eclipse.jetty.server.Request)[path in context], that is the request URI relative to the context path.
* The request remote address.
* Custom predicates that receive the request to match a custom condition.

Notable subclasses of `ConditionalHandler` are, for example, <<handler-use-qos,`QoSHandler`>>, <<handler-use-thread-limit,`ThreadLimitHandler`>> and <<handler-use-dos,`DoSHandler`>>.

[[handler-use-compression]]
==== CompressionHandler

`CompressionHandler` provides support for automatic decompression of compressed request content and automatic compression of response content.

The Maven artifact coordinates are:

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty.compression</groupId>
  <artifactId>jetty-compression-server</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

The compression algorithms supported are:

* link:https://github.com/google/brotli[brotli]; identified by `br` in `Accept-Encoding` and `Content-Encoding` HTTP headers.
* link:https://en.wikipedia.org/wiki/Gzip[gzip]; identified by `gzip` in `Accept-Encoding` and `Content-Encoding` HTTP headers.
* link:https://github.com/facebook/zstd[zstdandard]; identified by `zstd` in `Accept-Encoding` and `Content-Encoding` HTTP headers.

`CompressionHandler` discovers compression algorithm implementations via Java's `ServiceLoader` mechanism, so it is enough to put the Jetty artifacts with the compression implementation in the module-path or class-path, respectively:

* `jetty-compression-brotli`
* `jetty-compression-gzip`
* `jetty-compression-zstandard`

The Maven artifact coordinates are the following:

[,xml,subs=attributes+]
----
<dependencies>
  <dependency>
    <groupId>org.eclipse.jetty.compression</groupId>
    <artifactId>jetty-compression-brotli</artifactId>
    <version>{jetty-version}</version>
  </dependency>
  <dependency>
    <groupId>org.eclipse.jetty.compression</groupId>
    <artifactId>jetty-compression-gzip</artifactId>
    <version>{jetty-version}</version>
  </dependency>
  <dependency>
    <groupId>org.eclipse.jetty.compression</groupId>
    <artifactId>jetty-compression-zstandard</artifactId>
    <version>{jetty-version}</version>
  </dependency>
</dependencies>
----

[NOTE]
====
The Jetty artifacts `jetty-compression-brotli` and `jetty-compression-zstandard` access
respectively the Brotli and Zstandard implementations, that are written in native code.

Since Java 22, the JVM emits a warning message when Java code tries to access code in native libraries.
For example with Java 25:

----
WARNING: A restricted method in java.lang.System has been called
WARNING: java.lang.System::loadLibrary has been called by com.aayushatharva.brotli4j.Brotli4jLoader in an unnamed module (file:/home/simon/.m2/repository/com/aayushatharva/brotli4j/brotli4j/1.17.0/brotli4j-1.17.0.jar)
WARNING: Use --enable-native-access=ALL-UNNAMED to avoid a warning for callers in this module
WARNING: Restricted methods will be blocked in a future release unless native access is enabled
----

To avoid the warning, you can start the JVM as suggested by the warning message above, by adding `--enable-native-access=ALL-UNNAMED` on the command line.
This grants any code in the class-path access to any native library.

If you are running in JPMS mode, you can explicitly grant access just to the JPMS module(s) that require it.
In the case above, you need `--enable-native-access=com.aayushatharva.brotli4j`.
====

`CompressionHandler` is a `Handler.Wrapper` that inspects the request and, if the conditions are met, it wraps the request and the response to eventually perform decompression of the request content or compression of the response content.
The decompression/compression is not performed until the web application reads request content or writes response content.

`CompressionHandler` automatically adds the `Vary: Accept-Encoding` response header when the request method supports compression (by default `GET` and `POST`).
This header is added regardless of whether the client sends an `Accept-Encoding` request header, ensuring that caching proxies correctly handle responses that may vary based on client compression capabilities.

`CompressionHandler` comes with a default configuration, but can be explicitly configured through a `CompressionConfig` object that can be constructed via the builder pattern.

Furthermore, `CompressionHandler` can be configured either at the server level, or at the context level.

`CompressionHandler` can be configured at the server level in this way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=serverCompressionHandler]
----

The `Handler` tree structure looks like the following:

[,screen]
----
Server
└── CompressionHandler
    └── ContextHandlerCollection
        ├── ContextHandler 1
        :── ...
        └── ContextHandler N
----

However, in less common cases, you can configure `CompressionHandler` on a per-context basis, for example because you want to configure `CompressionHandler` with different parameters for each context, or because you want only some contexts to have compression support:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=contextCompressionHandler]
----

The `Handler` tree structure looks like the following:

[,screen]
----
Server
└── ContextHandlerCollection
    └── ContextHandlerCollection
        ├── CompressionHandler
        │   └── ContextHandler /shop
        │       └── ShopHandler
        └── ContextHandler /api
            └── RESTHandler
----

[[handler-use-gzip]]
==== GzipHandler

`GzipHandler` provides supports for automatic decompression of gzip compressed request content and automatic gzip compression of response content.

`GzipHandler` is now obsoleted by <<handler-use-compression,`CompressionHandler`>>, but still maintained for backwards compatibility.
`CompressionHandler` currently supports `gzip` but also other compression algorithms such as `brotli` and `zstandard`.

`GzipHandler` is a `Handler.Wrapper` that inspects the request and, if the request matches the `GzipHandler` configuration, just installs the required components to eventually perform decompression of the request content or compression of the response content.
The decompression/compression is not performed until the web application reads request content or writes response content.

`GzipHandler` can be configured at the server level in this way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=serverGzipHandler]
----

The `Handler` tree structure looks like the following:

[,screen]
----
Server
└── GzipHandler
    └── ContextHandlerCollection
        ├── ContextHandler 1
        :── ...
        └── ContextHandler N
----

However, in less common cases, you can configure `GzipHandler` on a per-context basis, for example because you want to configure `GzipHandler` with different parameters for each context, or because you want only some contexts to have compression support:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=contextGzipHandler]
----

The `Handler` tree structure looks like the following:

[,screen]
----
Server
└── ContextHandlerCollection
    └── ContextHandlerCollection
        ├── GzipHandler
        │   └── ContextHandler /shop
        │       └── ShopHandler
        └── ContextHandler /api
            └── RESTHandler
----

[[handler-use-rewrite]]
==== RewriteHandler

`RewriteHandler` provides support for URL rewriting, very similarly to https://httpd.apache.org/docs/current/mod/mod_rewrite.html[Apache's mod_rewrite] or https://nginx.org/en/docs/http/ngx_http_rewrite_module.html[Nginx rewrite module].

The Maven artifact coordinates are:

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-rewrite</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

`RewriteHandler` can be configured with a set of __rule__s; a _rule_ inspects the request and when it matches it performs some change to the request (for example, changes the URI path, adds/removes headers, etc.).

The Jetty Server Libraries provide rules for the most common usages, but you can write your own rules by extending the `org.eclipse.jetty.rewrite.handler.Rule` class.

Please refer to the `jetty-rewrite` module link:{javadoc-url}/org/eclipse/jetty/rewrite/handler/package-summary.html[javadocs] for the complete list of available rules.

You typically want to configure `RewriteHandler` at the server level, although it is possible to configure it on a per-context basis.

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=rewriteHandler]
----

The `Handler` tree structure looks like the following:

[,screen]
----
Server
└── RewriteHandler
    └── ContextHandlerCollection
        ├── ContextHandler 1
        :── ...
        └── ContextHandler N
----

[[handler-use-size-limit]]
==== SizeLimitHandler

`SizeLimitHandler` tracks the sizes of request content and response content, and fails the request processing with an HTTP status code of https://www.rfc-editor.org/rfc/rfc9110.html#name-413-content-too-large[`413 Content Too Large`].

Server applications can set up the `SizeLimitHandler` before or after handlers that modify the request content or response content such as <<handler-use-gzip,`GzipHandler`>>.
When `SizeLimitHandler` is before `GzipHandler` in the `Handler` tree, it will limit the compressed content; when it is after, it will limit the uncompressed content.

The `Handler` tree structure look like the following, to limit uncompressed content:

[,screen]
----
Server
└── GzipHandler
    └── SizeLimitHandler
        └── ContextHandlerCollection
            ├── ContextHandler 1
            :── ...
            └── ContextHandler N
----

[[handler-use-statistics]]
==== StatisticsHandler

`StatisticsHandler` gathers and exposes a number of statistic values related to request processing such as:

* Total number of requests
* Current number of concurrent requests
* Minimum, maximum, average and standard deviation of request processing times
* Number of responses grouped by HTTP code (i.e. how many `2xx` responses, how many `3xx` responses, etc.)
* Total response content bytes

Server applications can read these values and use them internally, or expose them via some service, or xref:arch/jmx.adoc[export them to JMX].

`StatisticsHandler` can be configured at the server level or at the context level.

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=statisticsHandler]
----

The `Handler` tree structure looks like the following:

[,screen]
----
Server
└── StatisticsHandler
    └── ContextHandlerCollection
        ├── ContextHandler 1
        :── ...
        └── ContextHandler N
----

[[handler-use-min-data-rate]]
=== MinimumDataRateHandler

`MinimumDataRateHandler` is a subclass of `StatisticsHandler` that enforces a minimum request content read rate and a minimum response content write rate based of the figures that it collects:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=dataRateHandler]
----

The `Handler` tree structure looks like the following:

[,screen]
----
Server
└── MinimumDataRateHandler
    └── ContextHandlerCollection
        ├── ContextHandler 1
        :── ...
        └── ContextHandler N
----

When minimum data rate is not enforced, bad clients may cause threads to be blocked on the server, and excessive memory consumption (as requests and responses stay in memory for longer than expected).

Configuring the minimum read rate protects the server against bad clients that send request content very slowly, without tripping the idle timeout.
When the server detects that the minimum threshold is not reached, it will abort the request, freeing server resources.

Configuring the minimum write rate protects the server against bad clients that read response content very slowly, without tripping the idle timeout, and cause TCP congestion (for HTTP/1.1) or flow control stalls (for HTTP/2 and HTTP/3).
When the server detects that the minimum threshold is not reached, it will abort the response, freeing server resources.

[[handler-use-events]]
==== EventsHandler

`EventsHandler` allows applications to be notified of <<request-processing-events,request processing>> events.

`EventsHandler` must be subclassed, and the relevant `onXYZ()` methods overridden to capture the request processing events you are interested in.
The request processing events can be used in conjunction with `Request` APIs that provide the information you may be interested in.

For example, if you want to use `EventsHandler` to record processing times, you can use the request processing events with the following `Request` APIs:

* `Request.getBeginNanoTime()`, which returns the earliest possible nanoTime the request was received.
* `Request.getHeadersNanoTime()`, which returns the nanoTime at which the parsing of the HTTP headers was completed.

[CAUTION]
====
The `Request` and `Response` objects may be inspected during events, but it is recommended to avoid modifying them, for example by adding/removing headers or by reading/writing content, because any modification may interfere with the processing performed by other ``Handler``s.
====

`EventsHandler` emits the following events:

`beforeHandling`::
Emitted just before `EventsHandler` invokes the `Handler.handle(\...)` method of the next `Handler` in the `Handler` tree.
`afterHandling`::
Emitted just after the invocation to the `Handler.handle(\...)` method of the next `Handler` in the `Handler` tree returns, either normally or by throwing.
`requestRead`::
Emitted every time a chunk of content is read from the `Request`.
`responseBegin`::
Emitted when the response first write happens.
`responseWrite`::
Emitted every time the write of some response content is initiated.
`responseWriteComplete`::
Emitted every time the write of some response content is completed, either successfully or with a failure.
`responseTrailersComplete`::
Emitted when the write of the response trailers is completed.
`complete`::
Emitted when both request and the response have been completely processed.

Your `EventsHandler` subclass should then be linked in the `Handler` tree in the relevant position, typically as the outermost `Handler` after `Server`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=eventsHandler]
----

The `Handler` tree structure looks like the following:

[,screen]
----
Server
└── MyEventsHandler
    └── ContextHandler /app
----

You can link the `EventsHandler` at any point in the `Handler` tree structure, and even have multiple ``EventsHandler``s to be notified of the request processing events at the different stages of the `Handler` tree, for example:

[,screen]
----
Server
└── TotalEventsHandler
    └── SlowHandler
        └── AppEventsHandler
            └── ContextHandler /app
----

In the example above, `TotalEventsHandler` may record the total times of request processing, from `SlowHandler` all the way to the `ContextHandler`.
On the other hand, `AppEventsHandler` may record both the time it takes for the request to flow from `TotalEventsHandler` to `AppEventsHandler`, therefore effectively measuring the processing time due to `SlowHandler`, and the time it takes to process the request by the `ContextHandler`.

Refer to the `EventsHandler` link:{javadoc-url}/org/eclipse/jetty/server/handler/EventsHandler.html[javadocs] for further information.

[[handler-use-qos]]
==== QoSHandler

`QoSHandler` allows web applications to limit the number of _active_ concurrent requests, that is requests that are currently being handled by a thread, and therefore are not waiting asynchronously, for example reading request content or writing response content.

`QoSHandler` extends xref:handler-use-conditional[`ConditionalHandler`], so you may be able to restrict what `QoSHandler` does to only requests that match the conditions (for example, only to `POST` requests, or only for certain request URIs, etc.)

Web applications may need to access resources with limited capacity, for example a relational database accessed through a JDBC connection pool.
Limiting the number of active concurrent requests helps to control the load on the server, so that it does not become overloaded and possibly unresponsive.
In turn, this improves the quality of service (QoS) for end users of the web application, because either their request is being handled actively by the server, or it is rejected sooner (after a configurable timeout in `QoSHandler`) rather than later (after a possibly longer idle timeout).

Consider the case where each HTTP request results in a JDBC query, and the capacity of the database is of 400 queries/s.
Allowing more than 400 HTTP requests/s into the system, for example 500 requests/s, results in 100 requests blocking waiting for a JDBC connection _for every second_.
It is evident that even a short load spike of few seconds eventually results in consuming all the server threads: some will be processing requests and queries, the remaining will be blocked waiting for a JDBC connection.
When no more threads are available, additional requests will queue up as tasks in the thread pool, consuming more memory and potentially causing a complete server failure.
This situation affects the whole server, so one bad behaving web application may affect other well behaving web applications.
From the end user perspective the quality of service is terrible, because requests will take a lot of time to be served and eventually time out.

In cases of load spikes, caused for example by popular events (weather or social events), usage bursts (Black Friday sales), or even denial-of-service attacks, it is desirable to give priority to certain requests rather than others.
For example, in an e-commerce site requests that lead to the checkout and to the payments should have higher priorities than requests to browse the products.
Another example is to prioritize requests for certain users such as paying users or administrative users.

`QoSHandler` allows you to configure the maximum number of concurrent requests; by extending `QoSHandler` you can prioritize suspended requests for faster processing.

A simple example that just limits the number of concurrent requests:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=simpleQoSHandler]
----

This is an example of a `QoSHandler` subclass where you can implement a custom prioritization logic:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=advancedQoSHandler]
----

Note that `QoSHandler` limits the number of _active_ concurrent requests for the whole server; if you want to apply limits for each remote client, see <<handler-use-thread-limit,`ThreadLimitHandler`>> and <<handler-use-dos,`DoSHandler`>>.

[[handler-use-secured]]
==== SecuredRedirectHandler -- Redirect from HTTP to HTTPS

`SecuredRedirectHandler` allows to redirect requests made with the `http` scheme (and therefore to the clear-text port) to the `https` scheme (and therefore to the encrypted port).

For example a request to `+http://domain.com:8080/path?param=value+` is redirected to `+https://domain.com:8443/path?param=value+`.

Server applications must configure a `HttpConfiguration` object with the secure scheme and secure port so that `SecuredRedirectHandler` can build the redirect URI.

`SecuredRedirectHandler` is typically configured at the server level, although it can be configured on a per-context basis.

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=securedHandler]
----

[[handler-use-cross-origin]]
==== CrossOriginHandler

`CrossOriginHandler` supports the server-side requirements of the https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS[CORS protocol] implemented by browsers when performing cross-origin requests.

An example of a cross-origin request is when a script downloaded from the origin domain `+http://domain.com+` uses `fetch()` or `XMLHttpRequest` to make a request to a cross domain such as `+http://cross.domain.com+` (a subdomain of the origin domain) or to `+http://example.com+` (a completely different domain).

This is common, for example, when you embed reusable components such as a chat component into a web page: the web page and the chat component files are downloaded from `+http://domain.com+`, but the chat server is at `+http://chat.domain.com+`, so the chat component must make cross-origin requests to the chat server.

This kind of setup exposes to https://owasp.org/www-community/attacks/csrf[cross-site request forgery (CSRF) attacks], and the CORS protocol has been established to protect against this kind of attacks.

For security reasons, browsers by default do not allow cross-origin requests, unless the response from the cross domain contains the right CORS headers.

`CrossOriginHandler` relieves server-side web applications from handling CORS headers explicitly.
You can set up your `Handler` tree with the `CrossOriginHandler`, configure it, and it will take care of the CORS headers separately from your application, where you can concentrate on the business logic.

The `Handler` tree structure looks like the following:

[,screen]
----
Server
└── CrossOriginHandler
    └── ContextHandler /app
        └── AppHandler
----

The most important `CrossOriginHandler` configuration parameter that must be configured is `allowedOrigins`, which by default is the empty set, therefore disallowing all origins.

You want to restrict requests to your cross domain to only origins you trust.
From the chat example above, the chat server at `+http://chat.domain.com+` knows that the chat component is downloaded from the origin server at `+http://domain.com+`, so the `CrossOriginHandler` is configured in this way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=crossOriginAllowedOrigins]
----

Browsers send cross-origin requests in two ways:

* Directly, if the cross-origin request meets some simple criteria.
* By issuing a hidden _preflight_ request before the actual cross-origin request, to verify with the server if it is willing to reply properly to the actual cross-origin request.

Both preflight requests and cross-origin requests will be handled by `CrossOriginHandler`, which will analyze the request and possibly add appropriate CORS response headers.

By default, preflight requests are not delivered to the `CrossOriginHandler` child `Handler`, but it is possible to configure `CrossOriginHandler` by setting `deliverPreflightRequests=true` so that the web application can fine-tune the CORS response headers.

Another important `CrossOriginHandler` configuration parameter is `allowCredentials`, which controls whether cookies and authentication headers that match the cross-origin request to the cross domain are sent in the cross-origin requests.
By default, `allowCredentials=false` so that cookies and authentication headers are not sent in cross-origin requests.

If the application deployed in the cross domain requires cookies or authentication, then you must set `allowCredentials=true`, but you also need to restrict the allowed origins only to the ones your trust, otherwise your cross domain application will be vulnerable to CSRF attacks.

For more `CrossOriginHandler` configuration options, refer to the link:{javadoc-url}/org/eclipse/jetty/server/handler/CrossOriginHandler.html[`CrossOriginHandler` javadocs].

[[handler-use-state-tracking]]
==== StateTrackingHandler

`StateTrackingHandler` is a xref:troubleshooting/index.adoc[troubleshooting] `Handler` that tracks whether `Handler`/`Request`/`Response` asynchronous APIs are properly used by applications.</p>

Asynchronous APIs are notoriously more difficult to troubleshoot than blocking APIs, and may be subject to restrictions that applications need to respect (a typical case is that they cannot perform blocking operations).

For example, a `Handler` implementation whose `handle(\...)` method returns `true` _must_ eventually complete the callback passed to `handle(\...)` (for more details on the `Handler` APIs, see <<handler-impl,this section>>).

When an application forgets to complete the callback passed to `handle(\...)`, the HTTP response may not be sent to the client, but it will be difficult to troubleshoot why the client is not receiving responses.

`StateTrackingHandler` helps with this troubleshooting because it tracks the callback passed to `handle(\...)` and emits an event if the callback is not completed within a configurable timeout:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=stateTrackingHandle]
----

By default, events are logged at warning level, but it is possible to specify a listener to be notified of the events tracked by `StateTrackingHandler`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=stateTrackingListener]
----

Other events tracked by `StateTrackingHandler` are demand callbacks that block, writes that do not complete their callbacks, or write callbacks that block.
The complete list of events is specified by the `StateTrackingHandler.Listener` class (link:{javadoc-url}/org/eclipse/jetty/server/handler/StateTrackingHandler.Listener.html[javadocs]).

[[handler-use-default]]
==== DefaultHandler

`DefaultHandler` is a terminal `Handler` that always returns `true` from its `handle(\...)` method and performs the following:

* Serves the `favicon.ico` Jetty icon when it is requested
* Sends a HTTP `404` response for any other request
* The HTTP `404` response content nicely shows a HTML table with all the contexts deployed on the `Server` instance

`DefaultHandler` is set directly on the server, for example:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=defaultHandler]
----

The `Handler` tree structure looks like the following:

[,screen]
----
Server
  ├── ContextHandlerCollection
  │   ├── ContextHandler 1
  │   :── ...
  │   └── ContextHandler N
  └── DefaultHandler
----

In the example above, `ContextHandlerCollection` will try to match a request to one of the contexts; if the match fails, `Server` will call the `DefaultHandler` that will return a HTTP `404` with an HTML page showing the existing contexts deployed on the `Server`.

NOTE: `DefaultHandler` just sends a nicer HTTP `404` response in case of wrong requests from clients.
Jetty will send an HTTP `404` response anyway if `DefaultHandler` has not been set.

[[handler-use-eager]]
==== EagerContentHandler

`EagerContentHandler` reads eagerly the HTTP request content, and invokes the next `Handler` in the `Handler` tree when the request content has been read.

`EagerContentHandler` should be installed when web applications use blocking I/O to read the request content, which is the typical case for Servlet or RESTful (JAX-RS) web applications.

Because the request content is read eagerly and asynchronously, the web application will never (or rarely) block while reading the request content.
In this way, the application obtains the benefits of asynchronous I/O without forcing web application developers to use more complicated asynchronous I/O APIs.

The `Handler` tree structure looks like the following:

[,screen]
----
Server
└── (GzipHandler) // optional
    └── EagerContentHandler
        └── ContextHandler /app
            └── AppHandler
----

`EagerContentHandler` should be installed in the `Handler` tree _after_ other ``Handler``s that may modify or transform the request content, like for example the `GzipHandler`.

`EagerContentHandler` eagerly reads request content in the following cases:

* Form request content.
* MultiPart request content.
* Any other type of request content.

For Form request content, `EagerContentHandler` reads the whole request content, parses it into a `Fields` object, and then invokes the next `Handler`.
This allows web applications that use blocking API calls such as `HttpServletRequest.getParameterMap()` to avoid blocking, since they can directly use the already created `Fields` object.

Similarly, for MultiPart request content, `EagerContentHandler` reads the whole request content, parses it into `MultiPartFormData.Parts`, and then invokes the next `Handler`.
This allows web applications that use blocking API calls such as `HttpServletRequest.getParts()` to avoid blocking, since the can directly use the already created `MultiPartFormData.Parts` object.

For other types of request content, `EagerContentHandler` reads and retains request content bytes up to a configurable amount, and then invokes the next `Handler`, without any further processing of the request content bytes.
This allows web applications that use blocking API calls such as `HttpServletRequest.getInputStream()` to avoid blocking in most cases (if the request is smaller than what has been configured in `EagerContentHandler`).

[[handler-use-thread-limit]]
==== ThreadLimitHandler

`ThreadLimitHandler` tracks remote IP addresses and limits the number of concurrent requests (and therefore threads) for each remote IP address to protect against denial-of-service attacks.

`ThreadLimitHandler` extends xref:handler-use-conditional[`ConditionalHandler`], so you may be able to restrict what `ThreadLimitHandler` does to only requests that match the conditions (for example, only to `POST` requests, or only for certain request URIs, etc.)

The remote IP address can be derived from the network, or from the `Forwarded` (or the now obsolete `X-Forwarded-For`) HTTP header.
The `Forwarded` header is typically present in requests that have been forwarded to Jetty by a reverse proxy such as HAProxy, Nginx, Apache, etc.

Note that `ThreadLimitHandler` is different from xref:handler-use-qos[`QoSHandler`] in that it limits the number of concurrent requests per remote IP address, while `QoSHandler` limits the total number of concurrent requests.

Note also that `ThreadLimitHandler` is different from xref:handler-use-dos[`DoSHandler`] in that it limits the number of concurrent requests per remote IP address, while `DoSHandler` limits the request rate per remote IP address.

[[handler-use-dos]]
==== DoSHandler

`DoSHandler` tracks remote clients (in a pluggable way, by default using the remote IP address), and limits the rate of requests for each remote client, to protect against denial-of-service attacks.

`DoSHandler` extends xref:handler-use-conditional[`ConditionalHandler`], so you may be able to restrict what `DoSHandler` does to only requests that match the conditions (for example, only to `POST` requests, or only for certain request URIs, etc.)

`DoSHandler` allows you to specify how to identify a remote client (by default using the remote IP address, but this is configurable), and the algorithm to use to calculate the rate of requests for each remote client (by default the link:https://en.wikipedia.org/wiki/Leaky_bucket[Leaky Bucket Algorithm]).

If a remote client exceeds the configured request rate, `DoSHandler` forwards the request handling to a configurable `Request.Handler` that allows you to decide how to handle the request.
`DosHandler` provides two such ``Request.Handler``s out of the box:

* `StatusRejectHandler` (the default) that responds with a configurable HTTP status code (by default `429 Too Many Requests`)
* `DelayedRejectHandler` that delays the request handling by a configurable timeout, and then forwards to request handling to another `Request.Handler` (by default `StatusRejectHandler`).
Delaying the request handling is of little cost for the server (since the request is delayed asynchronously), but the attacker would not know whether the request it sent was processed or rejected until the timeout elapses.
Hopefully, the delay forces the attacker to use more resources, and/or reduce the request rate, therefore reducing the attack impact on the server.

[CAUTION]
====
While `DoSHandler` may be a good start for simple setups, denial-of-service protection is typically best performed _outside_ the server itself, for example in load balancers or other data center network infrastructure components.
====

Here is a simple example that shows how to use `DoSHandler`:

[,java,indent=0,options=nowrap]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=dosHandler]
----

For other denial-of-service protections, see also <<handler-use-qos,`QoSHandler`>> and <<handler-use-thread-limit,`ThreadLimitHandler`>>.

[[handler-use-graceful]]
==== GracefulHandler

`GracefulHandler` allows to stop the Jetty server in a graceful way, by rejecting new requests but allowing existing requests to complete within a configurable timeout.

In this way, existing requests can be completed normally rather than with failures caused by the fact that the server is stopping; for example, stopping the server causes all TCP connections to be closed, so trying to write a response will result in a failure.

In order to stop Jetty gracefully, you need the following:

* Install the `GracefulHandler`, typically just after the `Server` at the root of the `Handler` tree.
* Configure `Server.stopTimeout` to a positive value.

When the `Server` component is stopped, it will check the `Server.stopTimeout`, and if positive, it will initiate a graceful shutdown by notifying all components that implement the `Graceful` interface that the shutdown has been initiated.

`GracefulHandler` implements `Graceful`, so it will start rejecting new requests with status code `503 Service Available`, but will allow existing requests to complete for a period of time up to `Server.stopTimeout`.

When all existing requests have completed, the `Server` stops all ``Connector``s, closes all connections, and finally stops all the components.

This is how you configure and use `GracefulHandler`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=gracefulHandler]
----

The `Handler` tree structure looks like the following:

[,screen]
----
Server
  └── GracefulHandler
      └── ContextHandler
          └── MyWebApp
----

[[handler-use-moved]]
==== MovedContextHandler

`MovedContextHandler` allows you to redirect an old URI to a new one, specifying the HTTP redirect code.

Use of this class is less flexible than using the <<handler-use-rewrite,`RewriteHandler`>> with a redirect rule, because it unconditionally redirects old URIs to the new URIs, while a `RewriteHandler` redirect rule performs the redirection only if the rule matches.

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=movedContextHandler]
----

[[handler-use-path-mappings]]
==== PathMappingsHandler

The section about <<handler-use-context-collection,`ContextHandlerCollection`>> showed how it is possible to split handling of HTTP requests across different web applications.

You can also split the handling of HTTP requests _within_ the same web application, so that they are handled by different ``Handler``s, to separate different concerns within a single web application.

For example, a shop web application with a context path of `/shop` may want to handle requests to browse the shop catalog separately from requests that manage the shopping cart, separately from requests that perform the checkout.

A typical way to separate these concerns is to design the request URIs so that each feature is identified by a different link:{javadoc-url}/org/eclipse/jetty/server/Request.html#getPathInContext(org.eclipse.jetty.server.Request)[path in context], that is the request URI relative to the context path.

In this case, requests to browse the shop catalog can have a URI path of `+/shop/catalog/*+`, while requests that manage the shopping cart can have a URI path of `+/shop/cart/*+`, and requests that perform the checkout can have a URI path of `+/shop/checkout/*+`.

`PathMappingsHandler` supports exactly this use case:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=pathMappingsHandler]
----

The `Handler` tree structure looks like the following:

[,screen]
----
Server
  └── ContextHandler /shop
      └── PathMappingsHandler
          ├── CatalogHandler  /catalog/*
          ├── CartHandler     /cart/*
          └── CheckoutHandler /checkout/*
----

[NOTE]
====
The paths configured in `PathMappingsHandler` are the paths _after_ the <<handler-use,context path>>.

Class link:{javadoc-url}/org/eclipse/jetty/http/pathmap/PathSpec.html[`PathSpec`] is used to specify the paths, and supports both Servlet path mappings and regular expression path mappings.
====

[TIP]
====
The ``Handler``s children of `PathMappingsHandler` may extend <<handler-use-conditional,`ConditionalHandler`>> to further control the conditions under which the `Handler` is invoked.

For example, a `LoginHandler` that extends `ConditionalHandler` may be configured to only accept `POST` requests.
====

[[handler-use-security]]
==== SecurityHandler

`SecurityHandler` allows to configure authentication and authorization for your web applications, and it is discussed in details in xref:security/index.adoc[this section].

[[handler-use-connect]]
==== ConnectHandler

`ConnectHandler` provides support for the HTTP `CONNECT` method, and it is used in forward proxies, as detailed in <<handler-use-proxy-forward,this section>>.

[[handler-use-proxy]]
==== ProxyHandler

`ProxyHandler` provides proxying capabilities for two use cases:

* _Forward_ proxying, implemented by <<handler-use-proxy-forward,`ProxyHandler.Forward`>>.
A forward proxy is chosen by clients (for example, it is configured in browsers or in `HttpClient`), and it is typically used intercept an organization's HTTP requests for sake of providing security or selective access to domains or resources.
Some forward proxies apply transformations to HTTP messages or their content as they are forwarded (for example, rewriting of URIs in HTML documents).
* _Reverse_ proxying, implemented by <<handler-use-proxy-reverse,`ProxyHandler.Reverse`>>.
A reverse proxy, or _gateway_, is an intermediary that acts as an origin server.
Clients cannot tell whether they are connecting to a reverse proxy or directly to the origin server: from the client point of view, the reverse proxy _is_ the origin server.
Reverse proxies receive requests and forward them to another server, and are typically used for TLS offloading, caching, and load balancing.

`ProxyHandler.Forward` and `ProxyHandler.Reverse` are typically subclassed to implement the custom logic that you want to apply in either case, and to configure the xref:client/http.adoc[`HttpClient`] instance, for example by specifying different `HttpClient` xref:client/http.adoc#transport[transports].

The most common methods to override are the following (consult the link:{javadoc-url}/org/eclipse/jetty/proxy/ProxyHandler.html[javadocs] for the full list):

* `newHttpClient()` to customize the `HttpClient` transport.
* `configureHttpClient(\...)` to customize the `HttpClient` configuration.
* `handle(\...)` to customize the proxy-to-server request and proxy-to-client response, for example to deny certain requests.
* `newProxyToServerRequest(\...)` to customize the proxy-to-server request.
* `newProxyToServerRequestContent(\...)` to customize the proxy-to-server request content.
* `newServerToProxyResponseListener(\...)` to customize the server-to-proxy response.

You may want to override `newHttpClient()` to customize the `HttpClient` transport, which is by default only HTTP/1.1, to also support HTTP/2, as shown below for `ProxyHandler.Forward` (but the same could be done for `ProxyHandler.Reverse`):

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=proxyNewHttpClient]
----

[[handler-use-proxy-forward]]
===== ProxyHandler.Forward

HTTP clients, such as browsers or `HttpClient`, interact with forward proxies in two ways:

* When the request is plain-text, using the `http` scheme, the request is sent to the forward proxy with little modifications.
The forward proxy typically just forwards the request to the origin server.
* When the request is secure, using the `https` scheme, the client first sends an HTTP `CONNECT` request to the forward proxy to establish a tunnel to the origin server.
Then the client sends the original request through the tunnel, and the forward proxy only acts as a pass-through for the encrypted bytes and cannot decrypt them.

The Jetty component that manages HTTP `CONNECT` requests and establishes tunnels to origin server is `ConnectHandler`.

A forward proxy that only supports plain-text requests has the following simple `Handler` tree structure:

[,screen]
----
Server
└── ProxyHandler.Forward
----

You can override `ProxyHandler.Forward.handle(\...)` to deny requests to certain URIs, or to certain domains:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=proxyForwardForbidden]
----

On the other hand, a forward proxy that supports both plain-text and secure requests has the following `Handler` tree structure:

[,screen]
----
Server
└── ConnectHandler
    └── ProxyHandler.Forward
----

If your forward proxy only accepts secure requests (see also <<handler-use-secured,`SecuredRedirectHandler`>>), you can also configure it with just `ConnectHandler`:

[,screen]
----
Server
└── ConnectHandler
----

You can configure `ConnectHandler` with a white list of origin servers the forward proxy is allowed to connect to.
Similarly, you can configure `ConnectHandler` with a black list of origin servers the forward proxy is forbidden to connect to.

You may need to add authentication to the forward proxy.

For simpler setups, authentication can be implemented by overriding `ConnectionHandler.handleAuthentication(\...)` for `CONNECT` requests, and by overriding `ProxyHandler.Forward.handle(\...)` for plain-text requests.

Full-blown authentication support can be achieved using <<handler-use-security,`SecurityHandler`>>:

[,screen]
----
Server
└── SecurityHandler
    └── ConnectHandler
        └── ProxyHandler.Forward
----

Forward proxies are configured in `HttpClient` as described in xref:client/http.adoc#proxy[this section].

[[handler-use-proxy-reverse]]
===== ProxyHandler.Reverse

Reverse proxy acts as origin servers (from the client point of view) and process incoming requests typically by forwarding them to a backend server, although they can also directly return cached responses.

A simple reverse proxy server has the following `Handler` tree structure:

[,screen]
----
Server
└── ProxyHandler.Reverse
----

A reverse proxy that forwards requests to a backend server, for example, may be configured to accept requests at `+https://domain.com/path+` and forward them to the backend server at `+http://backend1/proxy/path+`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=proxyReverse]
----

You can implement a very simple stateless load balancer in this way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=proxyReverseLoadBalancer]
----

[NOTE]
====
Reverse proxies do not typically need to be set up with a `ConnectHandler`.

Clients typically only send HTTP `CONNECT` requests to forward proxies, not to reverse proxies.
====

Reverse proxies can be set up to communicate with the backend using a protocol different from the one used on the frontend.

For example, it is possible to set up a reverse proxy that accepts on the frontend both HTTP/1.1 and HTTP/2 over network, both plain-text and secure, and communicates with the backend using FastCGI over Unix-Domain sockets.
This allows you to use WordPress to serve your website through Jetty: see xref:operations-guide:protocols/index.adoc#fcgi[this section] for more information.

[[handler-use-servlet]]
=== Servlet API Handlers

[[handler-use-servlet-context]]
==== ServletContextHandler

``Handler``s are easy to write, but often web applications have already been written using the Jakarta EE APIs, using ``Servlet``s and ``Filter``s.

`ServletContextHandler` is a `ContextHandler` that provides support for the Jakarta EE Servlet APIs and implements the behaviors required by the Servlet specification.

However, differently from <<handler-use-webapp-context,`WebAppContext`>>, it does not require web application to be packaged as a `+*.war+`, nor it requires a `web.xml` for configuration.

With `ServletContextHandler` you can just put all your Servlet components in a `+*.jar+` and configure each component using the `ServletContextHandler` APIs, in a way equivalent to what you would write in a `web.xml`.

The Maven artifact coordinates depend on the version of Jakarta EE you want to use, and they are:

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty.{ee-all}</groupId>
  <artifactId>jetty-{ee-all}-servlet</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

For example, for Jakarta {ee-current-caps} the coordinates are: `org.eclipse.jetty.{ee-current}:jetty-{ee-current}-servlet:{jetty-version}`.

Below you can find an example of how to set up a Jakarta {ee-current-caps} `ServletContextHandler`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=servletContextHandler-servlet]
----
[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=servletContextHandler-setup]
----

The `Handler` and Servlet components tree structure looks like the following:

[,screen,subs=+quotes]
----
Server
└── ServletContextHandler /shop
    ├── _ShopCartServlet /cart/*_
    └── _CrossOriginFilter /*_
----

Note how the Servlet components (they are not ``Handler``s) are represented in _italic_.

Note also how adding a `Servlet` or a `Filter` returns a _holder_ object that can be used to specify additional configuration for that particular `Servlet` or `Filter`, for example initialization parameters (equivalent to `<init-param>` in `web.xml`).

When a request arrives to `ServletContextHandler` the request URI will be matched against the ``Filter``s and ``Servlet`` mappings and only those that match will process the request, as dictated by the Servlet specification.

IMPORTANT: `ServletContextHandler` is a terminal `Handler`, that is it always returns `true` from its `handle(\...)` method when invoked.
Server applications must be careful when creating the `Handler` tree to put ``ServletContextHandler``s as last ``Handler``s in any `Handler.Collection` or as children of a `ContextHandlerCollection`.

// TODO: revise what above, as ServletContextHandler is not a terminal handler.
// TODO: introduce the fact that ServletContextHandler can have a class loader that may be used to "isolate" web application classes.

[[handler-use-webapp-context]]
==== WebAppContext

`WebAppContext` is a `ServletContextHandler` that autoconfigures itself by reading a `web.xml` Servlet configuration file.

The Maven artifact coordinates depend on the version of Jakarta EE you want to use, and they are:

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty.{ee-all}</groupId>
  <artifactId>jetty-{ee-all}-webapp</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

Jakarta EE web applications can specify a `+*.war+` file or a directory with the structure of a `+*.war+` file to `WebAppContext` to deploy a standard Jakarta EE web application packaged as defined by the Servlet specification.

Where web applications using `ServletContextHandler` must manually invoke methods to add ``Servlet``s and ``Filter``s as described in <<handler-use-servlet-context,this section>>, `WebAppContext` reads `WEB-INF/web.xml` to add ``Servlet``s and ``Filter``s, and also enforces a number of restrictions defined by the Servlet specification, in particular related to class loading.

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=webAppContextHandler]
----

[[handler-use-webapp-context-class-loading]]
==== WebAppContext Class Loading

The Jakarta EE Servlet specification requires that a Jakarta EE web application class loader must load classes found in `WEB-INF/classes` and `WEB_INF/lib` in preference to using the parent class loader.
This is significantly different to typical Java class loaders where loaders first delegate to their parent class loader.
For Servlet specification compliant web applications, the typical parent-first class loading model is _inverted_.

Furthermore, the Servlet specification requires that Jakarta EE web applications cannot load or otherwise access the Servlet Container implementation classes, also called _hidden classes_ (previously called _server classes_).
Jakarta EE web applications receive the HTTP request object as an instance of the `jakarta.servlet.http.HttpServletRequest` interface, and cannot downcast it to the Jetty specific implementation of that interface to access Jetty specific features -- this ensures maximum web application portability across Servlet Container implementations.

Lastly, the Servlet specification requires that specific classes are _protected classes_ (previously called _system classes_), in that they cannot be replaced by the web application loader and normal class loader priority applies.
These are classes such as `jakarta.servlet.http.HttpServletRequest` and JDK classes such as `java.lang.String` or `java.sql.Connection`.

`WebAppContext` implements this class loader logic using a single class loader, `WebAppClassLoader`, with filtering capabilities: when it loads a class, it checks whether the class is a _protected class_ or a _hidden class_.

When `WebAppClassLoader` is asked to load a class, it first tries to find the class locally (since it must use the inverted child-first model); if the class is found, and it is not a _protected class_, the class is loaded; otherwise the class is not found locally.
If the class is not found locally, the parent class loader is asked to load the class; the parent class loader uses the standard parent-first model, so it delegates the class loading to its parent, and so on.
If the class is found, and it is not a _hidden class_, the class is loaded; otherwise the class is not found and a `ClassNotFoundException` is thrown.

Unfortunately, the Servlet specification does not define exactly which classes are _hidden classes_ and which classes are _protected classes_.
However, Jetty picks good defaults and allows server applications to customize _hidden classes_ and _protected classes_ in `WebAppContext`.

Below you can find an example of how to set up additional classloading configuration, including hidden and protected classes in code:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=webAppContextClassLoader]
----

Specifying _protected_ and _hidden_ classes is also possible when using Jetty as a standalone server, as explained in xref:operations-guide:modules/standard.adoc#eeN-webapp[this section].

[[handler-use-webapp-context-class-loading-caching]]
===== CachingWebAppClassLoader

A `WebAppClassLoader` may be repeatedly asked to load classes or to find resources that do not exist.
In this case, the standard behavior is to try to load the class or find the resource _every time_, first by asking locally to the `WebAppClassLoader` itself, and then walking the parent `ClassLoader` hierarchy.

`CachingWebAppClassLoader` is a `WebAppClassLoader` subclass that caches _not-found_ attempts to load classes and find resources, so that a subsequent attempt to load a class or find a resource that does not exist is immediately responded, rather than being performed again.

`CachingWebAppClassLoader` may therefore increase performance, at the cost of dynamic resolution of classes or resources that may become available at a later time.

Below you can find an example of how to set up `CachingWebAppClassLoader`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=cachingWebAppClassLoader]
----

[[handler-use-webapp-context-class-loading-jetty-libraries]]
===== Web Applications using Jetty Libraries

Jakarta EE web applications may want to use Jetty utility classes in their code.
Typical examples are annotating web application classes with Jetty's xref:arch/jmx.adoc#annotation[JMX annotations] (for the Jetty server to export the instances as MBeans), or use Jetty's xref:client/index.adoc[`HttpClient`], or Jetty's xref:arch/json.adoc[JSON support].

There are two cases:

* The web application is using Jetty classes independently of the server.
For example, using Jetty's `HttpClient`, or JSON support from within a web application.
The web application may be deployed on a different Servlet Container.
* The web application is using Jetty classes with the goal of interacting with the Jetty server.
For example, annotating web application classes with Jetty's JMX annotations, so that the server exports instances as MBeans; or extending from `org.eclipse.jetty.util.component.LifeCycle` so that web application components can be made part of the xref:arch/bean.adoc[Jetty server component tree].
The web application can only be deployed in Jetty.

In the first case, it is enough to package all the Jetty libraries, and their transitive dependencies, in the web application `+*.war+` file, typically as `+*.jar+` files under `WEB-INF/lib/`.
For example, to use Jetty's `HttpClient` with HTTP/1.1 support in a web application you would need the following:

[,subs=attributes+]
----
my-webapp.war
└── WEB-INF
    └── lib
        ├── jetty-client-{jetty-version}.jar
        ├── jetty-http-{jetty-version}.jar
        ├── jetty-io-{jetty-version}.jar
        └── jetty-util-{jetty-version}.jar
----

The second case is more complicated due to class loading.
If the Jetty classes are present in the web application's `WEB-INF/lib/`, they will be loaded by the web application class loader, and therefore they will be a different type with respect to the same class loaded by the server class loader.

Because the web application wants to interact with the server, the Jetty JMX classes, or the Jetty component classes (such as `LifeCycle`) used by the web application must be the same type that the server is using.

To achieve that, you need the following:

* The web application `+*.war+` file must _not_ contain the Jetty library `+*.jar+` files.
* _Protect_ and _expose_ the relevant Jetty classes from the server to the web application.

When interacting with the _protected_ and _hidden_ classes, the string patterns are interpreted as follows:

* For full qualified class names, the class name itself, for example `org.eclipse.jetty.jmx.ObjectMBean`.
* For packages (its sub-packages), the package name ending with a `.`, for example `org.eclipse.jetty.util.annotation.`.
* For `+*.jar+` files or directories, their `file` URI, for example `file:/opt/jetty/lib/jetty-util-{jetty-version}.jar`.
* For JPMS modules, their `jrt` URI, for example `jrt:/org.eclipse.jetty.util`.

Any pattern that begins with `-` inverts the matching.
For example, adding to the _hidden_ classes the package pattern `-org.eclipse.jetty.jmx.` results in _not_ hiding (and therefore exposing) classes of that package (and its sub-packages) to the web application.

For example, to _protect_ (that is, avoid that the web application provides a modified version of the same class) and _expose_ (that is, not _hiding_ the class to the web application) the Jetty JMX classes:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=protectAndExpose]
----

[[handler-use-default-servlet]]
==== DefaultServlet -- Static Content for Servlets

If you have a <<handler-use-servlet-context,Jakarta EE web application>>, you may want to use a `DefaultServlet` instead of `ResourceHandler`.
The features are similar, but `DefaultServlet` is more commonly used to serve static files for Jakarta EE web applications.

The Maven artifact coordinates depend on the version of Jakarta EE you want to use, and they are:

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty.{ee-all}</groupId>
  <artifactId>jetty-{ee-all}-servlet</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

Below you can find an example of how to set up `DefaultServlet`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=defaultServlet]
----

If you wish to serve resources from a non-default location, then `ResourceServlet` should be used instead of the `DefaultServlet`.

Below you can find an example of how to set up both a `DefaultServlet` and a `ResourceServlet`:

[source,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=resourceServlet]
----

[[handler-impl]]
=== Implementing Handler

The `Handler` API consist fundamentally of just one method:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=handlerAPI]
----

The code that implements the `handle(\...)` method must respect the following contract:

* It may inspect `Request` immutable information such as URI and headers, typically to decide whether to return `true` or `false` (see below).
* Returning `false` means that the implementation will not handle the request, and it **must not** complete the `callback` parameter, nor read the request content, nor write response content.
* Returning `true` means that the implementation will handle the request, and it **must** eventually complete the `callback` parameter.
The completion of the `callback` parameter may happen synchronously within the invocation to `handle(\...)`, or at a later time, asynchronously, possibly from another thread.
If the response has not been explicitly written when the `callback` has been completed, the Jetty implementation will write a `200` response with no content if the `callback` has been succeeded, or an error response if the `callback` has been failed.

[CAUTION]
====
Violating the contract above may result in undefined or unexpected behavior, and possibly leak resources.

For example, returning `true` from `handle(\...)`, but not completing the `callback` parameter may result in the request or the response never be completed, likely causing the client to time out.

Similarly, returning `false` from `handle(\...)` but then either writing the response or completing the `callback` parameter will likely result in a garbled response be sent to the client, as the implementation will either invoke another `Handler` (that may write a response) or write a default response.
====

Applications may wrap the request, the response, or the callback and forward the wrapped request, response and callback to a child `Handler`.

[[handler-impl-hello]]
==== Hello World `Handler`

A simple "Hello World" `Handler` is the following:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=handlerHello]
----

Such a simple `Handler` can access the request and response main features, such as <<handler-impl-request,reading request headers and content>>, or <<handler-impl-response,writing response headers and content>>.

Note how `HelloWorldHandler` extends from `Handler.Abstract.NonBlocking`.
This is a declaration that `HelloWorldHandler` does not use blocking APIs (of any kind) to perform its logic, allowing Jetty to apply optimizations (see <<handler,here>>) that are not applied to ``Handler``s that declare themselves as blocking.

If your `Handler` implementation uses blocking APIs (of any kind), extend from `Handler.Abstract`.

Note how the `callback` parameter is passed to `Content.Sink.write(\...)` -- a utility method that eventually calls `Response.write(\...)`, so that when the write completes, also the `callback` parameter is completed.
Note also that because the `callback` parameter will eventually be completed, the value returned from `handle(\...)` is `true`.

In this way the <<handler-impl,`Handler` contract>> is fully respected: when `true` is returned, the `callback` will eventually be completed.

[[handler-impl-filter]]
==== Filtering `Handler`

A filtering `Handler` is a handler that perform some modification to the request or response, and then either forwards the request to another `Handler` or produces an error response:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=handlerFilter]
----

Note how a filtering `Handler` extends from `Handler.Wrapper` and as such needs another handler to forward the request processing to, and how the two ``Handler``s needs to be linked together to work properly.

[[handler-impl-request]]
==== Using the `Request`

The `Request` object can be accessed by web applications to inspect the HTTP request URI, the HTTP request headers and read the HTTP request content.

Since the `Request` object may be wrapped by <<handler-impl-filter,filtering ``Handler``s>>, the design decision for the `Request` APIs was to keep the number of virtual methods at a minimum.
This minimizes the effort necessary to write `Request` wrapper implementations and provides a single source for the data carried by `Request` objects.

To use the `Request` APIs, you should look up the relevant methods in the following order:

1. `Request` virtual methods.
For example, `Request.getMethod()` returns the HTTP method used in the request, such as `GET`, `POST`, etc.
2. `Request` `static` methods.
These are utility methods that provide more convenient access to request features.
For example, the HTTP URI query is a string and can be directly accessed via the non-``static`` method `request.getHttpURI().getQuery()`; however, the query string typically holds key/value parameters and applications should not have the burden to parse the query string, so the `static Request.extractQueryParameters(Request)` method is provided.
3. Super class `static` methods.
Since `Request` _is-a_ `Content.Source`, look also for `static` methods in `Content.Source` that take a `Content.Source` as a parameter, so that you can pass the `Request` object as a parameter.

Below you can find a list of the most common `Request` features and how to access them.
Refer to the `Request` link:{javadoc-url}/org/eclipse/jetty/server/Request.html[javadocs] for the complete list.

`Request` URI::
The `Request` URI is accessed via `Request.getHttpURI()` and the link:{javadoc-url}/org/eclipse/jetty/http/HttpURI.html[`HttpURI`] APIs.

`Request` HTTP headers::
The `Request` HTTP headers are accessed via `Request.getHeaders()` and the link:{javadoc-url}/org/eclipse/jetty/http/HttpFields.html[`HttpFields`] APIs.

`Request` cookies::
The `Request` cookies are accessed via `static Request.getCookies(Request)` and the link:{javadoc-url}/org/eclipse/jetty/http/HttpCookie.html[`HttpCookie`] APIs.

`Request` parameters::
The `Request` parameters are accessed via `static Request.extractQueryParameters(Request)` for those present in the HTTP URI query, and via `static Request.getParametersAsync(Request)` for both query parameters and request content parameters received via form upload with `Content-Type: application/x-www-url-form-encoded`, and the link:{javadoc-url}/org/eclipse/jetty/util/Fields.html[`Fields`] APIs.
If you are only interested in the request content parameters received via form upload, you can use `static FormFields.from(Request)`, see also <<handler-impl-request-content,this section>>.

`Request` HTTP session::
The `Request` HTTP session is accessed via `Request.getSession(boolean)` and the link:{javadoc-url}/org/eclipse/jetty/server/Session.html[`Session`] APIs.
For more information about how to set up support for HTTP sessions, see <<session,this section>>.

[[handler-impl-request-content]]
==== Reading the `Request` Content

Since `Request` _is-a_ `Content.Source`, the xref:arch/io.adoc#content-source[section] about reading from `Content.Source` applies to `Request` as well.
The `static Content.Source` utility methods will allow you to read the request content as a string, or as an `InputStream`, for example.

There are two special cases that are specific to HTTP for the request content: form parameters (sent when submitting an HTML form) and multipart form data (sent when submitting an HTML form with file upload).

For form parameters, typical of HTML form submissions, you can use the `FormFields` APIs as shown here:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=handlerForm]
----
<1> If the `Content-Type` is `application/x-www-form-urlencoded`, read the request content with `FormFields`.
<2> When all the request content has arrived, process the `Fields`.
<3> Specify that processing of the form fields is non-blocking.

[WARNING]
====
The `Handler` returns `true`, so the `callback` parameter **must** be completed.

It is therefore mandatory to use `CompletableFuture` APIs that are invoked even when reading the request content failed, such as `whenComplete(BiConsumer)`, `handle(BiFunction)`, `exceptionally(Function)`, etc.

Failing to do so may result in the `Handler` `callback` parameter to never be completed, causing the request processing to hang forever.
====

For multipart form data, typical for HTML form file uploads, you can use the `MultiPartFormData.Parser` APIs as shown here:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=handlerMultiPart]
----
<1> If the `Content-Type` is `multipart/form-data`, parse the request content with `MultiPartFormData`.
<2> When all the request content has arrived, process the `MultiPartFormData.Parts`.
<3> Specify that processing of the parts is non-blocking.

[WARNING]
====
The `Handler` returns `true`, so the `callback` parameter **must** be completed.

It is therefore mandatory to use `CompletableFuture` APIs that are invoked even when reading the request content failed, such as `whenComplete(BiConsumer)`, `handle(BiFunction)`, `exceptionally(Function)`, etc.

Failing to do so may result in the `Handler` `callback` parameter to never be completed, causing the request processing to hang forever.
====

[[handler-impl-request-listeners]]
==== `Request` Listeners

Application may add listeners to the `Request` object to be notified of particular events happening during the request/response processing.

`Request.addIdleTimeoutListener(Predicate<TimeoutException>)` allows you to add an idle timeout listener, which is invoked when an idle timeout period elapses during the request/response processing, if the idle timeout event is not notified otherwise.

When an idle timeout event happens, it is delivered to the application as follows:

* If there is pending demand (via `Request.demand(Runnable)`), then the demand `Runnable` is invoked and the application may see the idle timeout failure by reading from the `Request`, obtaining a xref:arch/io.adoc#content-source[transient failure chunk].
* If there is a pending response write (via `Response.write(boolean, ByteBuffer, Callback)`), the response write `Callback` is failed.
* If neither of the above, the idle timeout listeners are invoked, in the same order they have been added.
The first idle timeout listener that returns `true` stops the Jetty implementation from invoking the idle timeout listeners that follow.

The idle timeout listeners are therefore invoked only when the application is really idle, neither trying to read nor trying to write.

An idle timeout listener may return `true` to indicate that the idle timeout should be treated as a fatal failure of the request/response processing; otherwise the listener may return `false` to indicate that no further handling of the idle timeout is needed from the Jetty implementation.

When idle timeout listeners return `false`, then any subsequent idle timeouts are handled as above.
In the case that the application does not initiate any read or write, then the idle timeout listeners are invoked again after another idle timeout period.

`Request.addFailureListener(Consumer<Throwable>)` allows you to add a failure listener, which is invoked when a failure happens during the request/response processing.

When a failure happens during the request/response processing, then:

* The pending demand for request content, if any, is invoked; that is, the `Runnable` passed to `Request.demand(Runnable)` is invoked.
* The callback of an outstanding call to `Response.write(boolean, ByteBuffer, Callback)`, if any, is failed.
* The failure listeners are invoked, in the same order they have been added.

Failure listeners are invoked also in case of idle timeouts, in the following cases:

* At least one idle timeout listener returned `true` to indicate to the Jetty implementation to treat the idle timeout as a fatal failure.
* There are no idle timeout listeners.

Failures reported to a failure listener are always fatal failures; see also xref:arch/io.adoc#content-source[this section] about fatal versus transient failures.
This means that it is not possible to read or write from a failure listener: the read returns a fatal failure chunk, and the write will immediately fail the write callback.

[NOTE]
====
Applications are always required to complete the `Handler` callback, as described <<handler-impl,here>>.
In case of asynchronous failures, failure listeners are a good place to complete (typically by failing it) the `Handler` callback.
====

`Request.addCompletionListener(Consumer<Throwable>)` allows you to add a completion listener, which is invoked at the very end of the request/response processing.
This is equivalent to adding an `HttpStream` wrapper and overriding both `HttpStream.succeeded()` and `HttpStream.failed(Throwable)`.

Completion listeners are typically (but not only) used to recycle or dispose resources used during the request/response processing, or get a precise timing for when the request/response processing finishes, to be paired with `Request.getBeginNanoTime()`.

Note that while failure listeners are invoked as soon as the failure happens, completion listeners are invoked only at the very end of the request/response processing: after the `Callback` passed to `Handler.handle(Request, Response, Callback)` has been completed, all container dispatched threads have returned, and all the response writes have been completed.

In case of many completion listeners, they are invoked in the reverse order they have been added.

[[handler-impl-response]]
==== Using the `Response`

The `Response` object can be accessed by web applications to set the HTTP response status code, the HTTP response headers and write the HTTP response content.

The design of the `Response` APIs is similar to that of the `Request` APIs, described in <<handler-impl-request,this section>>.

To use the `Response` APIs, you should look up the relevant methods in the following order:

1. `Response` virtual methods.
For example, `Response.setStatus(int)` to set the HTTP response status code.
2. `Request` `static` methods.
These are utility methods that provide more convenient access to response features.
For example, adding an HTTP cookie could be done by adding a `Set-Cookie` response header, but it would be extremely error-prone.
The utility method `static Response.addCookie(Response, HttpCookie)` is provided instead.
3. Super class `static` methods.
Since `Response` _is-a_ `Content.Sink`, look also for `static` methods in `Content.Sink` that take a `Content.Sink` as a parameter, so that you can pass the `Response` object as a parameter.

Below you can find a list of the most common `Response` features and how to access them.
Refer to the `Response` link:{javadoc-url}/org/eclipse/jetty/server/Response.html[javadocs] for the complete list.

`Response` status code::
The `Response` HTTP status code is accessed via `Response.getStatus()` and `Response.setStatus(int)`.

`Response` HTTP headers::
The `Response` HTTP headers are accessed via `Response.getHeaders()` and the link:{javadoc-url}/org/eclipse/jetty/http/HttpFields.Mutable.html[`HttpFields.Mutable`] APIs.
The response headers are mutable until the response is _committed_, as defined in <<handler-impl-response-content,this section>>.

`Response` cookies::
The `Response` cookies are accessed via `static Response.addCookie(Response, HttpCookie)`, `static Response.replaceCookie(Response, HttpCookie)` and the link:{javadoc-url}/org/eclipse/jetty/http/HttpCookie.html[`HttpCookie`] APIs.
Since cookies translate to HTTP headers, they can be added/replaces until the response is _committed_, as defined in <<handler-impl-response-content,this section>>.

[[handler-impl-response-content]]
==== Writing the `Response` Content

Since `Response` _is-a_ `Content.Sink`, the xref:arch/io.adoc#content-sink[section] about writing to `Content.Sink` applies to `Response` as well.
The `static Content.Sink` utility methods will allow you to write the response content as a string, or as an `OutputStream`, for example.

IMPORTANT: The first call to `Response.write(boolean, ByteBuffer, Callback)` _commits_ the response.

Committing the response means that the response status code and response headers are sent to the other peer, and therefore cannot be modified anymore.
Trying to modify them may result in an `IllegalStateException` to be thrown, as it is an application mistake to commit the response and then try to modify the headers.

You can explicitly commit the response by performing an empty, non-last write:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=flush]
----

[WARNING]
====
The `Handler` returns `true`, so the `callback` parameter **must** be completed.

It is therefore mandatory to use `CompletableFuture` APIs that are invoked even when writing the response content failed, such as `whenComplete(BiConsumer)`, `handle(BiFunction)`, `exceptionally(Function)`, etc.

Failing to do so may result in the `Handler` `callback` parameter to never be completed, causing the request processing to hang forever.
====

Jetty can perform important optimizations for the HTTP/1.1 protocol if the response content length is known before the response is committed:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=contentLength]
----

NOTE: Setting the response content length is an _optimization_; Jetty will work well even without it.
If you set the response content length, however, remember that it must specify the number of _bytes_, not the number of characters.

[[handler-impl-response-interim]]
==== Sending Interim ``Response``s

The HTTP protocol (any version) allows applications to write https://www.rfc-editor.org/rfc/rfc9110#name-status-codes[interim responses].

An interim response has a status code in the `1xx` range (but not `101`), and an application may write zero or more interim response before the final response.

This is an example of writing an interim `100 Continue` response:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=continue]
----
<1> Using `Response.writeInterim(\...)` to send the interim response.
<2> The completion of the callback must take into account both success and failure.

Note how writing an interim response is as asynchronous operation.
As such you must perform subsequent operations using the `CompletableFuture` APIs, and remember to complete the `Handler` `callback` parameter both in case of success or in case of failure.

This is an example of writing an interim `103 Early Hints` response:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=earlyHints103]
----
<1> Using `Response.writeInterim(\...)` to send the interim response.
<2> The completion of the callback must take into account both success and failure.

An interim response may or may not have its own HTTP headers (this depends on the interim response status code), and they are typically different from the final response HTTP headers.

[[session]]
=== HTTP Session Support

Some web applications (but not all of them) have the concept of a _user_, that is a way to identify a specific client that is interacting with the web application.

The HTTP session is a feature offered by servers that allows web applications to maintain a temporary, per-user, storage for user-specific data.

The storage can be accessed by the web application across multiple request/response interactions with the client.
This makes the web application stateful, because a computation performed by a previous request may be stored in the HTTP session and used in subsequent requests without the need to perform again the computation.

Since not all web applications need support for the HTTP session, Jetty offers this feature optionally.

The Maven coordinates for the Jetty HTTP session support are:

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-session</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

The HTTP session support is provided by the `org.eclipse.jetty.session.SessionHandler`, that must be set up in the `Handler` tree between a `ContextHandler` and your `Handler` implementation:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/SessionHandlerDocs.java[tags=session]
----

The corresponding `Handler` tree structure looks like the following:

[,screen]
----
Server
└── ContextHandler /myApp
    └── SessionHandler
        └── MyAppHandler
----

With the ``Handler``s set up in this way, you can access the HTTP session from your `MyAppHandler` using `Request.getSession(boolean)`, and then use the link:{javadoc-url}/org/eclipse/jetty/server/Session.html[`Session` APIs].

The support provided by Jetty for HTTP sessions is advanced and completely pluggable, providing features such as first-level and second-level caching, eviction, etc.

You can configure the HTTP session support from a very simple local in-memory configuration, to a replicated (across nodes in a cluster), persistent (for example over file system, database or https://memcached.org/[memcached]) configuration for the most advanced use cases.
The advanced configuration of Jetty's HTTP session support is discussed in more details in xref:server/session.adoc[this section].

[[security]]
=== Securing HTTP Server Applications

Web applications may need to restrict access to resources to authenticated requests.

The Jetty support for web application security is provided by the artifact at the following Maven coordinates:

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-security</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

Web application security is primarily provided by <<handler-use-security,`org.eclipse.jetty.security.SecurityHandler`>>, and it is covered in details in xref:security/index.adoc[this section].

[[application]]
=== Writing HTTP Server Applications

Writing HTTP applications is typically simple, especially when using blocking APIs.
However, there are subtle cases where it is worth clarifying what a server application should do to obtain the desired results when run by Jetty.

[[application-1xx]]
==== Sending 1xx Responses

The https://tools.ietf.org/html/rfc7231#section-5.1.1[HTTP/1.1 RFC] allows for `1xx` informational responses to be sent before a real content response.
Unfortunately the servlet specification does not provide a way for these to be sent, so Jetty has had to provide non-standard handling of these headers.

[[application-100]]
==== 100 Continue

The `100 Continue` response should be sent by the server when a client sends a request with an `Expect: 100-continue` header, as the client will not send the body of the request until the `100 Continue` response has been sent.

The intent of this feature is to allow a server to inspect the headers and to tell the client to not send a request body that might be too large or insufficiently private or otherwise unable to be handled.

Jetty achieves this by waiting until the input stream or reader is obtained by the filter/servlet, before sending the `100 Continue` response.
Thus a filter/servlet may inspect the headers of a request before getting the input stream and send an error response (or redirect etc.) rather than the 100 continues.

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=continue100]
----

[[jetty-102-processing]]
==== 102 Processing

https://tools.ietf.org/html/rfc2518[RFC 2518] defined the `102 Processing` status code that can be sent:

[quote,RFC 2518 section 10.1]
when the server has a reasonable expectation that the request will take significant time to complete.
As guidance, if a method is taking longer than 20 seconds (a reasonable, but arbitrary value) to process the server SHOULD return a `102 Processing` response.

However, a later update of RFC 2518, https://tools.ietf.org/html/rfc4918[RFC 4918], removed the `102 Processing` status code for https://tools.ietf.org/html/rfc4918#appendix-F.3["lack of implementation"].

Jetty supports the `102 Processing` status code.
If a request is received with the `Expect: 102-processing` header, then a filter/servlet may send a `102 Processing` response (without terminating further processing) by calling `response.sendError(102)`.

---

### Documentation Jetty Modules Programming Guide Pages Server Http2 (`documentation/jetty/modules/programming-guide/pages/server/http2.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= HTTP/2 Server Library

In the vast majority of cases, server applications should use the generic, high-level, xref:server/http.adoc[HTTP server library] that also provides HTTP/2 support via the HTTP/2 ``ConnectionFactory``s as described in details xref:server/http.adoc#connector-protocol-http2[here].

The low-level HTTP/2 server library has been designed for those applications that need low-level access to HTTP/2 features such as _sessions_, _streams_ and _frames_, and this is quite a rare use case.

See also the correspondent xref:client/http2.adoc[HTTP/2 client library].

[[intro]]
== Introduction

The Maven artifact coordinates for the HTTP/2 client library are the following:

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty.http2</groupId>
  <artifactId>jetty-http2-server</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

HTTP/2 is a multiplexed protocol: it allows multiple HTTP/2 requests to be sent on the same TCP connection, or _session_.
Each request/response cycle is represented by a _stream_.
Therefore, a single _session_ manages multiple concurrent _streams_.
A _stream_ has typically a very short life compared to the _session_: a _stream_ only exists for the duration of the request/response cycle and then disappears.

[[flow-control]]
== HTTP/2 Flow Control

The HTTP/2 protocol is _flow controlled_ (see https://tools.ietf.org/html/rfc7540#section-5.2[the specification]).
This means that a sender and a receiver maintain a _flow control window_ that tracks the number of data bytes sent and received, respectively.
When a sender sends data bytes, it reduces its flow control window.
When a receiver receives data bytes, it also reduces its flow control window, and then passes the received data bytes to the application.
The application consumes the data bytes and tells back the receiver that it has consumed the data bytes.
The receiver then enlarges the flow control window, and the implementation arranges to send a message to the sender with the number of bytes consumed, so that the sender can enlarge its flow control window.

A sender can send data bytes up to its whole flow control window, then it must stop sending.
The sender may resume sending data bytes when it receives a message from the receiver that the data bytes sent previously have been consumed.
This message enlarges the sender flow control window, which allows the sender to send more data bytes.

HTTP/2 defines _two_ flow control windows: one for each _session_, and one for each _stream_.
Let's see with an example how they interact, assuming that in this example the session flow control window is 120 bytes and the stream flow control window is 100 bytes.

The sender opens a session, and then opens `stream_1` on that session, and sends `80` data bytes.
At this point the session flow control window is `40` bytes (`120 - 80`), and ``stream_1``'s flow control window is `20` bytes (`100 - 80`).
The sender now opens `stream_2` on the same session and sends `40` data bytes.
At this point, the session flow control window is `0` bytes (`40 - 40`), while ``stream_2``'s flow control window is `60` (`100 - 40`).
Since now the session flow control window is `0`, the sender cannot send more data bytes, neither on `stream_1` nor on `stream_2`, nor on other streams, despite all the streams having their stream flow control windows greater than `0`.

The receiver consumes ``stream_2``'s `40` data bytes and sends a message to the sender with this information.
At this point, the session flow control window is `40` (``0 + 40``), ``stream_1``'s flow control window is still `20` and ``stream_2``'s flow control window is `100` (``60 + 40``).
If the sender opens `stream_3` and would like to send `50` data bytes, it would only be able to send `40` because that is the maximum allowed by the session flow control window at this point.

It is therefore very important that applications notify the fact that they have consumed data bytes as soon as possible, so that the implementation (the receiver) can send a message to the sender (in the form of a `WINDOW_UPDATE` frame) with the information to enlarge the flow control window, therefore reducing the possibility that sender stalls due to the flow control windows being reduced to `0`.

How a server application should handle HTTP/2 flow control is discussed in details in <<request,this section>>.

[[setup]]
== Server Setup

The low-level HTTP/2 support is provided by `org.eclipse.jetty.http2.server.RawHTTP2ServerConnectionFactory` and `org.eclipse.jetty.http2.api.server.ServerSessionListener`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http2/HTTP2ServerDocs.java[tags=setup]
----

Where server applications using the xref:server/http.adoc[high-level server library] deal with HTTP requests and responses in ``Handler``s, server applications using the low-level HTTP/2 server library deal directly with HTTP/2 __session__s, __stream__s and __frame__s in a `ServerSessionListener` implementation.

The `ServerSessionListener` interface defines a number of methods that are invoked by the implementation upon the occurrence of HTTP/2 events, and that server applications can override to react to those events.

Please refer to the `ServerSessionListener` link:{javadoc-url}/org/eclipse/jetty/http2/api/server/ServerSessionListener.html[javadocs] for the complete list of events.

The first event is the _accept_ event and happens when a client opens a new TCP connection to the server and the server accepts the connection.
This is the first occasion where server applications have access to the HTTP/2 `Session` object:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http2/HTTP2ServerDocs.java[tags=accept]
----

After connecting to the server, a compliant HTTP/2 client must send the https://tools.ietf.org/html/rfc7540#section-3.5[HTTP/2 client preface], and when the server receives it, it generates the _preface_ event on the server.
This is where server applications can customize the connection settings by returning a map of settings that the implementation will send to the client:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http2/HTTP2ServerDocs.java[tags=preface]
----

[[request]]
== Receiving a Request

Receiving an HTTP request from the client, and sending a response, creates a _stream_ that encapsulates the exchange of HTTP/2 frames that compose the request and the response.

An HTTP request is made of a `HEADERS` frame, that carries the request method, the request URI and the request headers, and optional `DATA` frames that carry the request content.

Receiving the `HEADERS` frame opens the `Stream`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http2/HTTP2ServerDocs.java[tags=request]
----

Server applications should return a `Stream.Listener` implementation from `onNewStream(\...)` to be notified of events generated by the client, such as `DATA` frames carrying request content, or a `RST_STREAM` frame indicating that the client wants to _reset_ the request, or an idle timeout event indicating that the client was supposed to send more frames but it did not.

The example below shows how to receive request content:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http2/HTTP2ServerDocs.java[tags=requestContent]
----

[NOTE]
====
When `onDataAvailable(Stream stream)` is invoked, the demand is implicitly cancelled.

Just returning from the `onDataAvailable(Stream stream)` method does _not_ implicitly demand for more `DATA` frames.

Applications must call `Stream.demand()` to explicitly require that `onDataAvailable(Stream stream)` is invoked again when more `DATA` frames are available.
====

Applications that consume the content buffer within `onDataAvailable(Stream stream)` (for example, writing it to a file, or copying the bytes to another storage) should call `Data.release()` as soon as they have consumed the content buffer.
This allows the implementation to reuse the buffer, reducing the memory requirements needed to handle the content buffers.

Alternatively, an application may store away the `Data` object to consume the buffer bytes later, or pass the `Data` object to another asynchronous API (this is typical in proxy applications).

[IMPORTANT]
====
The call to `Stream.readData()` tells the implementation to enlarge the stream and session flow control windows so that the sender will be able to send more `DATA` frames without stalling.
====

Applications can unwrap the `Data` object into some other object that may be used later, provided that the _release_ semantic is maintained:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/HTTP2Docs.java[tags=dataUnwrap]
----

[IMPORTANT]
====
Applications that implement `onDataAvailable(Stream stream)` must remember to call `Stream.demand()` eventually.

If they do not call `Stream.demand()`, the implementation will not invoke `onDataAvailable(Stream stream)` to deliver more `DATA` frames and the application will stall threadlessly until an idle timeout fires to close the stream or the session.
====

[[response]]
== Sending a Response

After receiving an HTTP request, a server application must send an HTTP response.

An HTTP response is typically composed of a `HEADERS` frame containing the HTTP status code and the response headers, and optionally one or more `DATA` frames containing the response content bytes.

The HTTP/2 protocol also supports response trailers (that is, headers that are sent after the response content) that also are sent using a `HEADERS` frame.

A server application can send a response in this way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http2/HTTP2ServerDocs.java[tags=response;!exclude]
----

[[reset]]
== Resetting a Request

A server application may decide that it does not want to accept the request.
For example, it may throttle the client because it sent too many requests in a time window, or the request is invalid (and does not deserve a proper HTTP response), etc.

A request can be reset in this way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http2/HTTP2ServerDocs.java[tags=reset;!exclude]
----

[[push]]
== HTTP/2 Push of Resources

A server application may _push_ secondary resources related to a primary resource.

A client may inform the server that it does not accept pushed resources(see https://tools.ietf.org/html/rfc7540#section-8.2[this section] of the specification) via a `SETTINGS` frame.
Server applications must track `SETTINGS` frames and verify whether the client supports HTTP/2 push, and only push if the client supports it:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http2/HTTP2ServerDocs.java[tags=push]
----

---

### Documentation Jetty Modules Programming Guide Pages Server Http3 (`documentation/jetty/modules/programming-guide/pages/server/http3.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= HTTP/3 Server Library

In the vast majority of cases, server applications should use the generic, high-level, xref:server/http.adoc[HTTP server library] that also provides HTTP/3 support via the HTTP/3 connector and ``ConnectionFactory``s as described in details xref:server/http.adoc#connector-protocol-http3[here].

The low-level HTTP/3 server library has been designed for those applications that need low-level access to HTTP/3 features such as _sessions_, _streams_ and _frames_, and this is quite a rare use case.

See also the correspondent xref:client/http3.adoc[HTTP/3 client library].

[[intro]]
== Introduction

The Maven artifact coordinates for the HTTP/3 server library are the following:

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty.http3</groupId>
  <artifactId>jetty-http3-server</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

The HTTP/3 protocol is layered on top of the underlying QUIC protocol.

The QUIC protocol implementation underlying HTTP/3 is provided by Jetty classes that wrap the link:https://github.com/cloudflare/quiche[Quiche] native library, so you need also the following artifact:

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty.quic</groupId>
  <artifactId>jetty-quic-quiche-server</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

Furthermore, the binding to the Quiche native library has two implementations: one based on link:https://github.com/java-native-access/jna[JNA] that works with Java 17 or later, and one based on JDK's https://openjdk.org/jeps/454[Foreign Function & Memory (FFM)] APIs that works with Java 22 or later.

You must choose the binding you want to use (depending on the Java version you are using), with either of the following artifacts:

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty.quic</groupId>
  <artifactId>jetty-quic-quiche-jna</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty.quic</groupId>
  <artifactId>jetty-quic-quiche-foreign</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

[NOTE]
====
Since Java 22, the JVM emits a warning message when Java code tries to access code in native libraries.
For example with Java 25:

----
WARNING: A restricted method in java.lang.foreign.AddressLayout has been called
WARNING: java.lang.foreign.AddressLayout::withTargetLayout has been called by org.eclipse.jetty.quic.quiche.foreign.NativeHelper in an unnamed module (file:/home/simon/opensource/jetty/12.1.x/jetty-core/jetty-quic/jetty-quic-quiche/jetty-quic-quiche-foreign/target/classes/)
WARNING: Use --enable-native-access=ALL-UNNAMED to avoid a warning for callers in this module
WARNING: Restricted methods will be blocked in a future release unless native access is enabled
----

To avoid the warning, you can start the JVM as suggested by the warning message above, by adding `--enable-native-access=ALL-UNNAMED` on the command line.
This grants any code in the class-path access to any native library.

If you are running in JPMS mode, you can explicitly grant access just to the JPMS module(s) that require it.
To grant Jetty access to the Quiche native library, use `--enable-native-access=org.eclipse.jetty.quic.quiche.foreign`.
This grants only to the Jetty JPMS module `org.eclipse.jetty.quic.quiche.foreign` in the module-path access to any native library.
====

When a client wants to establish a connection with a server, the QUIC protocol establishes the connection with the server, represented by a QUIC _session_.

A QUIC _session_ typically has a long life -- once the connection is established, it remains active until it is not used anymore (and therefore it is closed by the idle timeout mechanism), until a fatal error occurs (for example, a network failure), or if one of the peers decides unilaterally to close the connection.

There is a 1-to-1 relationship between a QUIC _session_ and an HTTP/3 _session_, defined by class `org.eclipse.jetty.http3.api.Session.Server`.

The QUIC protocol is multiplexed, that is it allows multiple, independent, QUIC _streams_ to exchange data to and from the server, over a single connection.
Therefore, a single QUIC _session_ manages multiple concurrent QUIC _streams_.

There is a 1-to-1 relationship between a QUIC _stream_ and an HTTP/3 _stream_, defined by class `org.eclipse.jetty.http3.api.Stream.Server`.

Each HTTP/3 request/response cycle is carried by an HTTP/3 _stream_, and has typically a very short life compared to the _session_: a _stream_ only exists for the duration of the request/response cycle and then disappears.

// TODO: flow control?
//[[pg-server-http3-flow-control]]
//==== HTTP/3 Flow Control
//
//include::../../http3.adoc[tag=flowControl]
//
//How a server application should handle HTTP/3 flow control is discussed in details in <<request,this section>>.

[[setup]]
== Server Setup

The low-level HTTP/3 support is provided by `org.eclipse.jetty.http3.server.RawHTTP3ServerConnectionFactory` and `org.eclipse.jetty.http3.api.Session.Server.Listener`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http3/HTTP3ServerDocs.java[tags=setup]
----

Where server applications using the xref:server/http.adoc[high-level server library] deal with HTTP requests and responses in ``Handler``s, server applications using the low-level HTTP/3 server library deal directly with HTTP/3 __session__s, __stream__s and __frame__s in a `Session.Server.Listener` implementation.

The `Session.Server.Listener` interface defines a number of methods that are invoked by the implementation upon the occurrence of HTTP/3 events, and that server applications can override to react to those events.

Please refer to the `Session.Server.Listener` link:{javadoc-url}/org/eclipse/jetty/http3/api/Session.Server.Listener.html[javadocs] for the complete list of events.

The first event is the _accept_ event and happens when a client opens a new QUIC connection to the server and the server accepts the connection.
This is the first occasion where server applications have access to the HTTP/3 `Session` object:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http3/HTTP3ServerDocs.java[tags=accept]
----

After the QUIC connection has been established, both client and server must send an HTTP/3 `SETTINGS` frame to exchange their HTTP/3 configuration.
This generates the _preface_ event, where applications can customize the HTTP/3 configuration by returning a map of settings that the implementation will send to the other peer:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http3/HTTP3ServerDocs.java[tags=preface]
----

[[request]]
== Receiving a Request

Receiving an HTTP request from the client, and sending a response, creates a _stream_ that encapsulates the exchange of HTTP/3 frames that compose the request and the response.

An HTTP request is made of a `HEADERS` frame, that carries the request method, the request URI and the request headers, and optional `DATA` frames that carry the request content.

Receiving the `HEADERS` frame opens the `Stream`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http3/HTTP3ServerDocs.java[tags=request]
----

Server applications should return a `Stream.Server.Listener` implementation from `onRequest(\...)` to be notified of events generated by the client, such as `HEADERS` frame carrying the request headers, `DATA` frames carrying request content, or a failure event indicating that the client wants to cancel the request, or an idle timeout event indicating that the client was supposed to send more frames, but it did not.

The example below shows how to receive request content:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http3/HTTP3ServerDocs.java[tags=requestContent]
----

// TODO: flow control?
//include::../../http3.adoc[tag=apiFlowControl]

[[response]]
== Sending a Response

After receiving an HTTP request, a server application must send an HTTP response.

An HTTP response is typically composed of a `HEADERS` frame containing the HTTP status code and the response headers, and optionally one or more `DATA` frames containing the response content bytes.

The HTTP/3 protocol also supports response trailers (that is, headers that are sent after the response content) that also are sent using a `HEADERS` frame.

A server application can send a response in this way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http3/HTTP3ServerDocs.java[tags=response;!exclude]
----

[[terminate]]
== Terminating a Request

A server application may decide that it does not want to accept the request.
For example, it may throttle the client because it sent too many requests in a time window, or the request is invalid (and does not deserve a proper HTTP response), etc.

A request can be abruptly terminated in this way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http3/HTTP3ServerDocs.java[tags=terminate;!exclude]
----

---

### Documentation Jetty Modules Programming Guide Pages Server Index (`documentation/jetty/modules/programming-guide/pages/server/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Server Libraries

The Eclipse Jetty Project provides server-side libraries that allow you to configure and start programmatically an HTTP or WebSocket server from a main class, or embed it in your existing application.
A typical example is a HTTP server that needs to expose a REST endpoint.
Another example is a proxy application that receives HTTP requests, processes them, and then forwards them to third party services, for example using the Jetty xref:client/index.adoc[client libraries].

While historically Jetty is an HTTP server, it is possible to use the Jetty server-side libraries to write a generic network server that interprets any network protocol (not only HTTP).
If you are interested in the low-level details of how the Eclipse Jetty server libraries work, or are interested in writing a custom protocol, look at the xref:server/io-arch.adoc[server I/O architecture] and the xref:server/threads.adoc[server threading architecture].

The Jetty server-side libraries provide:

* HTTP high-level support for HTTP/1.0, HTTP/1.1, HTTP/2, clear-text or encrypted, HTTP/3, for applications that want to embed Jetty as a generic HTTP server or proxy (no matter the HTTP version), via the xref:server/http.adoc[HTTP libraries]
* HTTP/2 low-level support, for applications that want to explicitly handle low-level HTTP/2 _sessions_, _streams_ and _frames_, via the xref:server/http2.adoc[HTTP/2 libraries]
* HTTP/3 low-level support, for applications that want to explicitly handle low-level HTTP/3 _sessions_, _streams_ and _frames_, via the xref:server/http3.adoc[HTTP/3 libraries]
* WebSocket support, for applications that want to embed a WebSocket server, via the xref:server/websocket.adoc[WebSocket libraries]
* FCGI support, to delegate requests to PHP, Python, Ruby or similar scripting languages.

---

### Documentation Jetty Modules Programming Guide Pages Server Io Arch (`documentation/jetty/modules/programming-guide/pages/server/io-arch.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Server I/O Architecture

The Jetty server libraries provide the basic components and APIs to implement a network server.

They build on the common xref:arch/io.adoc[Jetty I/O Architecture] and provide server specific concepts.

The Jetty server libraries provide I/O support for TCP/IP sockets (for both IPv4 and IPv6) and, when using Java 16 or later, for Unix-Domain sockets.

Support for Unix-Domain sockets is interesting when Jetty is deployed behind a proxy or a load-balancer: it is possible to configure the proxy or load balancer to communicate with Jetty via Unix-Domain sockets, rather than via the loopback network interface.

The central I/O server-side component are `org.eclipse.jetty.server.ServerConnector`, that handles the TCP/IP socket traffic, and `org.eclipse.jetty.unixdomain.server.UnixDomainServerConnector`, that handles the Unix-Domain socket traffic.

`ServerConnector` and `UnixDomainServerConnector` are very similar, and while in the following sections `ServerConnector` is used, the same concepts apply to `UnixDomainServerConnector`, unless otherwise noted.

A `ServerConnector` manages a list of ``ConnectionFactory``s, that indicate what protocols the connector is able to speak.

[[connection-factory]]
== Creating Connections with `ConnectionFactory`

Recall from the xref:arch/io.adoc#connection[`Connection` section] of the Jetty I/O architecture that `Connection` instances are responsible for parsing bytes read from a socket and generating bytes to write to that socket.

On the server-side, a `ConnectionFactory` creates `Connection` instances that know how to parse and generate bytes for the specific protocol they support -- it can be either HTTP/1.1, or TLS, or FastCGI, or the https://www.haproxy.org/download/2.1/doc/proxy-protocol.txt[PROXY protocol].

For example, this is how clear-text HTTP/1.1 is configured for TCP/IP sockets:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=http]
----

With this configuration, the `ServerConnector` will listen on port `8080`.

Similarly, this is how clear-text HTTP/1.1 is configured for Unix-Domain sockets:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=httpUnix]
----

With this configuration, the `UnixDomainServerConnector` will listen on file `/tmp/jetty.sock`.

[NOTE]
====
`ServerConnector` and `UnixDomainServerConnector` only differ by how they are configured -- for `ServerConnector` you specify the IP port it listens to, for `UnixDomainServerConnector` you specify the Unix-Domain path it listens to.

Both configure ``ConnectionFactory``s in exactly the same way.
====

When a new socket connection is established, `ServerConnector` delegates to the `ConnectionFactory` the creation of the `Connection` instance for that socket connection, that is linked to the corresponding `EndPoint`:

[plantuml]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false
skinparam padding 5

hide members
hide circle

scale 1.5

circle network
circle application

network - SocketChannelEndPoint
SocketChannelEndPoint - HttpConnection
HttpConnection - application
----

For every socket connection there will be an `EndPoint` + `Connection` pair.

[[connection-factory-wrapping]]
== Wrapping a `ConnectionFactory`

A `ConnectionFactory` may wrap another `ConnectionFactory`; for example, the TLS protocol provides encryption for any other protocol.
Therefore, to support encrypted HTTP/1.1 (also known as `https`), you need to configure the `ServerConnector` with two ``ConnectionFactory``s -- one for the TLS protocol and one for the HTTP/1.1 protocol, like in the example below:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=tlsHttp]
----

With this configuration, the `ServerConnector` will listen on port `8443`.
When a new socket connection is established, the first `ConnectionFactory` configured in `ServerConnector` is invoked to create a `Connection`.
In the example above, `SslConnectionFactory` creates a `SslConnection` and then asks to its wrapped `ConnectionFactory` (in the example, `HttpConnectionFactory`) to create the wrapped `Connection` (an `HttpConnection`) and will then link the two ``Connection``s together, in this way:

[plantuml]
----
skinparam backgroundColor transparent
skinparam monochrome true
skinparam shadowing false
skinparam padding 5

hide members
hide circle

scale 1.5

circle network
circle application

network - SocketChannelEndPoint
SocketChannelEndPoint - SslConnection
SslConnection -- SslEndPoint
SslEndPoint - HttpConnection
HttpConnection - application
----

Bytes read by the `SocketChannelEndPoint` will be interpreted as TLS bytes by the `SslConnection`, then decrypted and made available to the `SslEndPoint` (a component part of `SslConnection`), which will then provide them to `HttpConnection`.

The application writes bytes through the `HttpConnection` to the `SslEndPoint`, which will encrypt them through the `SslConnection` and write the encrypted bytes to the `SocketChannelEndPoint`.

[[connection-factory-detecting]]
== Choosing `ConnectionFactory` via Bytes Detection

Typically, a network port is associated with a specific protocol.
For example, port `80` is associated with clear-text HTTP, while port `443` is associated with encrypted HTTP (that is, the TLS protocol wrapping the HTTP protocol, also known as `https`).

In certain cases, applications need to listen to the same port for two or more protocols, or for different but incompatible versions of the same protocol, which can only be distinguished by reading the initial bytes and figuring out to what protocol they belong to.

The Jetty server libraries support this case by placing a `DetectorConnectionFactory` in front of other ``ConnectionFactory``s.
`DetectorConnectionFactory` accepts a list of ``ConnectionFactory``s that implement `ConnectionFactory.Detecting`, which will be called to see if one of them recognizes the initial bytes.

In the example below you can see how to support both clear-text and encrypted HTTP/1.1 (i.e. both `http` and `https`) _on the same network port_:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=detector]
----

<1> Creates the `DetectorConnectionFactory` with the `SslConnectionFactory` as the only detecting `ConnectionFactory`.
With this configuration, the detector will delegate to `SslConnectionFactory` to recognize the initial bytes, which will detect whether the bytes are TLS protocol bytes.
<2> Creates the `ServerConnector` with `DetectorConnectionFactory` as the first `ConnectionFactory`, and `HttpConnectionFactory` as the next `ConnectionFactory` to invoke if the detection fails.

In the example above `ServerConnector` will listen on port 8181.
When a new socket connection is established, `DetectorConnectionFactory` is invoked to create a `Connection`, because it is the first `ConnectionFactory` specified in the `ServerConnector` list.
`DetectorConnectionFactory` reads the initial bytes and asks to its detecting ``ConnectionFactory``s if they recognize the bytes.
In the example above, the detecting ``ConnectionFactory`` is `SslConnectionFactory` which will therefore detect whether the initial bytes are TLS bytes.
If one of the detecting ``ConnectionFactory``s recognizes the bytes, it creates a `Connection`; otherwise `DetectorConnectionFactory` will try the next `ConnectionFactory` after itself in the `ServerConnector` list.
In the example above, the next `ConnectionFactory` after `DetectorConnectionFactory` is `HttpConnectionFactory`.

The final result is that when new socket connection is established, the initial bytes are examined: if they are TLS bytes, a `SslConnectionFactory` will create a `SslConnection` that wraps an `HttpConnection` as explained <<connection-factory-wrapping,here>>, therefore supporting `https`; otherwise they are not TLS bytes and an `HttpConnection` is created, therefore supporting `http`.

[[connection-factory-custom]]
== Writing a Custom `ConnectionFactory`

This section explains how to use the Jetty server-side libraries to write a generic network server able to parse and generate any protocol..

Let's suppose that we want to write a custom protocol that is based on JSON but has the same semantic as HTTP; let's call this custom protocol `JSONHTTP`, so that a request would look like this:

[,json]
----
{
  "type": "request",
  "method": "GET",
  "version": "HTTP/1.1",
  "uri": "http://localhost/path",
  "fields": {
    "content-type": "text/plain;charset=ASCII"
  },
  "content": "HELLO"
}
----

In order to implement this custom protocol, we need to:

* implement a `JSONHTTPConnectionFactory`
* implement a `JSONHTTPConnection`
* parse bytes and generate bytes in the `JSONHTTP` format
* design an easy to use API that applications use to process requests and respond

First, the `JSONHTTPConnectionFactory`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=jsonHttpConnectionFactory]
----

Note how `JSONHTTPConnectionFactory` extends `AbstractConnectionFactory` to inherit facilities common to all `ConnectionFactory` implementations.

Second, the `JSONHTTPConnection`.
Recall from the xref:arch/io.adoc#echo[echo `Connection` example] that you need to override `onOpen()` to call `fillInterested()` so that the Jetty I/O system will notify your `Connection` implementation when there are bytes to read by calling `onFillable()`.
Furthermore, because the Jetty libraries are non-blocking and asynchronous, you need to use `IteratingCallback` to implement `onFillable()`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=jsonHttpConnection]
----

Again, note how `JSONHTTPConnection` extends `AbstractConnection` to inherit facilities that you would otherwise need to re-implement from scratch.

When `JSONHTTPConnection` receives a full JSON object it calls `invokeApplication(...)` to allow the application to process the incoming request and produce a response.

At this point you need to design a non-blocking asynchronous API that takes a `Callback` parameter so that applications can signal to the implementation when the request processing is complete (either successfully or with a failure).

A simple example of this API design could be the following:

* Wrap the JSON `Map` into a `JSONHTTPRequest` parameter so that applications may use more specific HTTP APIs such as `JSONHTTPRequest.getMethod()` rather than a generic `Map.get("method")`
* Provide an equivalent `JSONHTTPResponse` parameter so that applications may use more specific APIs such as `JSONHTTPResponse.setStatus(int)` rather than a generic `Map.put("status", 200)`
* Provide a `Callback` (or a `CompletableFuture`) parameter so that applications may indicate when the request processing is complete

This results in the following API:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=jsonHttpAPI]
----

The important part of this simple API example is the `Callback` parameter that makes the API non-blocking and asynchronous.

---

### Documentation Jetty Modules Programming Guide Pages Server Session (`documentation/jetty/modules/programming-guide/pages/server/session.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= HTTP Session Management

Sessions are a concept within the Servlet API which allow requests to store and retrieve information across the time a user spends in an application.

[[architecture]]
== Session Architecture

Jetty session support has been architected to provide a core implementation that is independent of the Servlet specification.
This allows programmers who use core Jetty - without the Servlet API - to still have classic Servlet session-like support for their ``Request``s and ``Handler``s.

These core classes are adapted to each of the various Servlet specification environments to deliver classic ``HttpSession``s for ``Servlet``s,`Filter``s, etc

Full support for the session lifecycle is supported, in addition to L1 and L2 caching, and a number of pluggable options for persisting session data.

Here are some of the most important concepts that will be referred to throughout the documentation:

SessionIdManager::
responsible for allocation of unique session ids.
HouseKeeper::
responsible for orchestrating the detection and removal of expired sessions.
SessionManager::
responsible for managing the lifecycle of sessions.
SessionHandler::
an implementation of `SessionManager` that adapts sessions to either the core or Servlet specification environment.
SessionCache::
an L1 cache of in-use `ManagedSession` objects
Session::
a session consisting of `SessionData` that can be associated with a `Request`
ManagedSession::
a `Session` that supports caching and lifecycle management
SessionData::
encapsulates the attributes and metadata associated with a `Session`
SessionDataStore::
responsible for creating, persisting and reading `SessionData`
CachingSessionDataStore::
an L2 cache of `SessionData`

Diagrammatically, these concepts can be represented as:

[plantuml]
----
title Session Composition Diagram
class Server

interface SessionIdManager

class HouseKeeper

interface SessionManager

class SessionHandler

interface SessionCache

interface SessionDataStore

class CachingSessionDataStore

interface Session

class ManagedSession

class SessionData

class Request

Server "1" *-down- "1" SessionIdManager
SessionIdManager "1" *-left- "1" HouseKeeper
Server "1" *-down- "n" SessionHandler
Request "1" *-down- "0/1" Session
SessionManager "1" *-down- "1" SessionCache
SessionManager <|-- SessionHandler
SessionCache "1" *-down- "1" SessionDataStore
SessionCache  o-down- ManagedSession
ManagedSession "1" *-- "1" SessionData
Session <|-- ManagedSession
SessionDataStore --> SessionData: CRUD
SessionDataStore <|-- CachingSessionDataStore
CachingSessionDataStore o- SessionData
----

[[idmgr]]
== The SessionIdManager

There is a maximum of one `SessionIdManager` per `Server` instance.
Its purpose is to generate fresh, unique session ids and to coordinate the re-use of session ids amongst co-operating contexts.

The `SessionIdManager` is agnostic with respect to the type of clustering technology chosen.

Jetty provides a default implementation - the link:{javadoc-url}/org/eclipse/jetty/session/DefaultSessionIdManager.html[DefaultSessionIdManager]  - which should meet the needs of most users.

[[defaultidmgr]]
=== The DefaultSessionIdManager

[[workername]]
A single instance of the `DefaultSessionIdManager` should be created and registered as a bean on the `Server` instance so that all ``SessionHandler``'s share the same instance.
This is done by the Jetty `session` module, but can be done programmatically instead.
As a fallback, when an individual `SessionHandler` starts up, if it does not find the `SessionIdManager` already present for the `Server` it will create and register a bean for it.
That instance will be shared by the other ``SessionHandler``s.

The most important configuration parameter for the `DefaultSessionIdManager` is the `workerName`, which uniquely identifies the server in a cluster.
If a `workerName` has not been explicitly set, then the value is derived as follows:

 node[JETTY_WORKER_NAME]

where `JETTY_WORKER_NAME` is an environment variable whose value can be an integer or string.
If the environment variable is not set, then it defaults to `0`, yielding the default `workerName` of `"node0"`.
It is _essential_ to change this default if you have more than one `Server`.

Here is an example of explicitly setting up a `DefaultSessionIdManager` with a `workerName` of `server3` in code:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/session/SessionDocs.java[tags=default]
----

[[housekeeper]]
=== The HouseKeeper

The `DefaultSessionIdManager` creates a link:{javadoc-url}/org/eclipse/jetty/session/HouseKeeper.html[HouseKeeper], which periodically scans for, and eliminates, expired sessions (referred to as "scavenging").
The period of the scan is controlled by the `setIntervalSec(int)` method, defaulting to 600secs.
Setting a negative or 0 value prevents scavenging occurring.


[IMPORTANT]
====
The `HouseKeeper` semi-randomly adds 10% to the configured `intervalSec`.
This is to help prevent sync-ing up of servers in a cluster that are all restarted at once, and slightly stagger their scavenge cycles to ensure any load on the persistent storage mechanism is spread out.
====

Here is an example of creating and configuring a `HouseKeeper` for the `DefaultSessionIdManager` in code:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/session/SessionDocs.java[tags=housekeeper]
----

=== Implementing a Custom SessionIdManager

If the `DefaultSessionIdManager` does not meet your needs, you can extend it, or implement the `SessionIdManager` interface directly.

When implementing a `SessionIdManager` pay particular attention to the following:

* the `getWorkerName()` method _must_ return a name that is unique to the `Server` instance.
The `workerName` becomes important in clustering scenarios because sessions can migrate from node to node:  the `workerName` identifies which node was last managing a `Session`.
* the contract of the `isIdInUse(String id)` method is very specific: a session id may _only_ be reused _iff_ it is already in use by another context.
This restriction is important to support cross-context dispatch.
* you should be _very_ careful to ensure that the `newSessionId(HttpServletRequest request, long created)` method does not return duplicate or predictable session ids.

[[handler]]
== The SessionHandler

A `SessionHandler` is a `Handler` that implements the `SessionManager`, and is thus responsible for the creation, maintenance and propagation of sessions.
There are `SessionHandlers` for both the core and the various Servlet environments.

Note that in the Servlet environments, each `ServletContextHandler` or `WebAppContext` has at most a single `SessionHandler`.

Both core and Servlet environment `SessionHandlers` can be configured programmatically.
Here are some of the most important methods that you may call to customize your session setup.
Note that in Servlet environments, some of these methods also have analogous Servlet API methods and/or analogous `web.xml` declarations and also equivalent  context init params.
These alternatives are noted below.

setCheckingRemoteSessionIdEncoding(boolean)  _[Default:false]_ ::
This controls whether response urls will be encoded with the session id as a path parameter when the URL is destined for a remote node. +
_Servlet environment alternatives:_
* `org.eclipse.jetty.session.CheckingRemoteSessionIdEncoding` context init parameter

setMaxInactiveInterval(int) _[Default:-1]_ ::
This is the amount of time in seconds after which an unused session may be scavenged. +
_Servlet environment alternatives:_
* `<session-config><session-timeout/></session-config>` element in `web.xml` (NOTE! this element is specified in _minutes_ but this method uses _seconds_).
* `ServletContext.setSessionTimeout(int)` where the timeout is configured in _minutes_.

setHttpOnly(boolean) _[Default:false]_ ::
If `true`, the session cookie will not be exposed to client-side scripting code. +
_Servlet environment alternatives:_
* `SessionCookieConfig.setHttpOnly(boolean)`
* `<session-config><cookie-config><http-only/></cookie-config></session-config>` element in `web.xml`

[[handler-refreshcookie]]
setRefreshCookieAge(int) _[Default:-1]_ ::
Value in seconds that controls resetting the session cookie when `SessionCookieConfig.setMaxAge(int)` is non-zero.
See also <<handler-maxAge,setting the max session cookie age with an init parameter>>.
If the amount of time since the session cookie was last set exceeds this time, the session cookie is regenerated to keep the session cookie valid.

setSameSite(HttpCookie.SameSite) _[Default:null]_ ::
The values are `HttpCookie.SameSite.NONE`, `HttpCookie.SameSite.STRICT`, `HttpCookie.SameSite.LAX`.

setSecureRequestOnly(boolean) _[Default:true]_::
If `true` and the request is HTTPS, the set session cookie will be marked as `secure`, meaning the client will only send the session cookie to the server on subsequent requests over HTTPS. +
_Servlet environment alternatives:_
*  `SessionCookieConfig.setSecure(true)`, in which case the set session cookie will _always_ be marked as `secure`, even if the request triggering the creation of the cookie was not over HTTPS.
* `<session-config><cookie-config><secure/></cookie-config></session-config>` element in `web.xml`

setSessionCookie(String) _[Default:"JSESSIONID"]_::
This is the name of the session cookie. +
_Servlet environment alternatives:_
* `SessionCookieConfig.setName(String)`
* `<session-config><cookie-config><name/></cookie-config></session-config>` element in `web.xml`
* `org.eclipse.jetty.session.SessionCookie` context init parameter.

setSessionIdPathParameterName(String) _[Default:"jsessionid"]_::
This is the name of the path parameter used to transmit the session id on request URLs, and on encoded URLS in responses. +
_Servlet environment alternatives:_
* `org.eclipse.jetty.session.SessionIdPathParameterName` context init parameter

setSessionTrackingModes(Set<SessionTrackingMode>) _[Default:{`SessionTrackingMode.COOKIE`, `SessionTrackingMode.URL`}]_::
_Servlet environment alternatives:_
* `ServletContext.setSessionTrackingModes<Set<SessionTrackingMode>)`
* defining up to three ``<tracking-mode>``s for the `<session-config>` element in `web.xml`

setUsingCookies(boolean) _[Default:true]_ ::
Determines whether the `SessionHandler` will look for session cookies on requests, and will set session cookies on responses.
If `false` session ids must be transmitted as path params on URLs.

[[handler-maxAge]]
setMaxAge(int) _[Default:-1]_::
This is the maximum number of seconds that the session cookie will be considered to be valid.
By default, the cookie has no maximum validity time.
See also <<handler-refreshcookie,refreshing the session cookie>>. +
_Servlet environment alternatives:_
* `ServletContext.getSessionCookieConfig().setMaxAge(int)`
* `org.eclipse.jetty.session.MaxAge` context init parameter

setSessionDomain(String) _[Default:null]_ ::
This is the domain of the session cookie. +
_Servlet environment alternatives:_
* `ServletContext.getSessionCookieConfig().setDomain(String)`
* `<session-config><cookie-config><domain/></cookie-config></session-config>` element in `web.xml`
* `org.eclipse.jetty.session.SessionDomain` context init parameter

setSessionPath(String) _[Default:null]_::
This is used when creating a new session cookie.
If nothing is configured, the context path is used instead, defaulting to `/`. +
_Servlet environment alternatives:_
* `ServletContext.getSessionCookieConfig().setPath(String)`
* `<session-config><cookie-config><path/></cookie-config></session-config>` element in `web.xml`
* `org.eclipse.jetty.session.SessionPath` context init parameter

=== Statistics

Some statistics about the sessions for a context can be obtained from the `SessionHandler`, either by calling the methods directly or via JMX:

getSessionsCreated()::
This is the total number of sessions that have been created for this context since Jetty started.

getSessionTimeMax()::
The longest period of time a session was valid in this context before being invalidated.

getSessionTimeMean()::
The average period of time a session in this context was valid.

getSessionTimeStdDev()::
The standard deviation of the session validity times for this context.

getSessionTimeTotal()::
The total time that all sessions in this context have remained valid.

[[cache]]
== The SessionCache

There is one `SessionCache` per `SessionManager`, and thus one per context.
Its purpose is to provide an L1 cache of `ManagedSession` objects.
Having a working set of `ManagedSession` objects in memory allows multiple simultaneous requests for the same session (ie the _same_ session id in the _same_ context) to share the same `ManagedSession` object.
A `SessionCache` uses a `SessionDataStore` to create, read, store, and delete the `SessionData` associated with the `ManagedSession`.

There are two ways to create a `SessionCache` for a `SessionManager`:

. allow the `SessionManager` to create one lazily at startup.
The `SessionManager` looks for a `SessionCacheFactory` bean on the `Server` to produce the `SessionCache` instance.
It then looks for a `SessionDataStoreFactory` bean on the `Server` to produce a `SessionDataStore` instance to use with the `SessionCache`.
If no `SessionCacheFactory`  is present, it defaults to creating a `DefaultSessionCache`.
If no `SessionDataStoreFactory` is present, it defaults to creating a `NullSessionDataStore`.

. pass a fully configured `SessionCache` instance to the `SessionManager`.
You are responsible for configuring both the `SessionCache` instance and its `SessionDataStore`

More on ``SessionDataStore``s <<datastore,later>>, this section concentrates on the `SessionCache` and `SessionCacheFactory`.


The link:{javadoc-url}/org/eclipse/jetty/session/AbstractSessionCache.html[AbstractSessionCache] provides most of the behaviour of ``SessionCache``s.
If you are implementing a custom `SessionCache` it is strongly recommended that you extend this class because it implements the numerous subtleties  of the Servlet specification.

Some of the important behaviours of ``SessionCache``s are:

eviction::
By default, ``ManagedSession``s remain in a cache until they are expired or invalidated.
If you have many or large sessions that are infrequently referenced you can use eviction to reduce the memory consumed by the cache.
When a session is evicted, it is removed from the cache but it is _not_ invalidated.
If you have configured a `SessionDataStore` that persists or distributes the session in some way, it will continue to exist, and can be read back in when it needs to be referenced again.
The eviction strategies are:
    NEVER_EVICT:::
    This is the default, sessions remain in the cache until expired or invalidated.
    EVICT_ON_SESSION_EXIT:::
    When the last simultaneous request for a session finishes, the session will be evicted from the cache.
    EVICT_ON_INACTIVITY:::
    If a session has not been referenced for a configurable number of seconds, then it will be evicted from the cache.

saveOnInactiveEviction::
This controls whether a session will be persisted to the `SessionDataStore` if it is being evicted due to the EVICT_ON_INACTIVITY policy.
Usually sessions are written to the `SessionDataStore` whenever the last simultaneous request exits the session.
However, as ``SessionDataStore``s` can be configured to <<datastore-skip,skip some writes>>, this option ensures that the session will be written out.

saveOnCreate::
Usually a session will be written through to the configured `SessionDataStore` when the last request for it finishes.
In the case of a freshly created session, this means that it will not be persisted until the request is fully finished.
If your application uses context forwarding or including, the newly created session id will not be available in the subsequent contexts.
You can enable this feature to ensure that a freshly created session is immediately persisted after creation: in this way the session id will be available for use in other contexts accessed during the same request.

removeUnloadableSessions::
If a session becomes corrupted in the persistent store, it cannot be re-loaded into the `SessionCache`.
This can cause noisy log output during scavenge cycles, when the same corrupted session fails to load over and over again.
To prevent his, enable this feature and the `SessionCache` will ensure that if a session fails to be loaded, it will be deleted.

invalidateOnShutdown::
Some applications want to ensure that all cached sessions are removed when the server shuts down.
This option will ensure that all cached sessions are invalidated.
The `AbstractSessionCache` does not implement this behaviour, a subclass must implement the link:{javadoc-url}/org/eclipse/jetty/session/SessionCache.html#shutdown()[SessionCache.shutdown()] method.

flushOnResponseCommit::
This forces a "dirty" session to be written to the `SessionDataStore` just before a response is returned to the client, rather than waiting until the request is finished.
A "dirty" session is one whose attributes have changed, or it has been freshly created.
Using this option ensures that all subsequent requests - either to the same or a different node - will see the latest changes to the session.

Jetty provides two `SessionCache` implementations: the link:{javadoc-url}/org/eclipse/jetty/session/DefaultSessionCache.html[DefaultSessionCache] and the link:{javadoc-url}/org/eclipse/jetty/session/NullSessionCache.html[NullSessionCache].

[[hash]]
=== The DefaultSessionCache

The link:{javadoc-url}/org/eclipse/jetty/session/DefaultSessionCache.html[DefaultSessionCache] retains `ManagedSession` objects in memory in a `ConcurrentHashMap`.
It is suitable for non-clustered and clustered deployments.
For clustered deployments, a sticky load balancer is *strongly* recommended, otherwise you risk indeterminate session state as the session bounces around multiple nodes.

It implements the link:{javadoc-url}/org/eclipse/jetty/session/SessionCache.html#shutdown()[SessionCache.shutdown()] method.

It also provides some statistics on sessions, which are convenient to access either directly in code or remotely via JMX:

current sessions::
The link:{javadoc-url}/org/eclipse/jetty/session/DefaultSessionCache.html#getSessionsCurrent()[DefaultSessionCache.getSessionsCurrent()] method reports the number of sessions in the cache at the time of the method call.

max sessions::
The link:{javadoc-url}/org/eclipse/jetty/session/DefaultSessionCache.html#getSessionsCurrent()[DefaultSessionCache.getSessionsMax()] method reports the highest number of sessions in the cache at the time of the method call.

total sessions::
The link:{javadoc-url}/org/eclipse/jetty/session/DefaultSessionCache.html#getSessionsTotal()[DefaultSessionCache.getSessionsTotal()] method reports the cumulative total of the number of sessions in the cache at the time of the method call.

If you create a link:{javadoc-url}/org/eclipse/jetty/session/DefaultSessionCacheFactory.html[DefaultSessionFactory] and register it as a `Server` bean, a `SessionManger` will be able to lazily create a `DefaultSessionCache`.
The `DefaultSessionCacheFactory` has all of the same configuration setters as a `DefaultSessionCache`.
Alternatively, if you only have a single `SessionManager`, or you need to configure a `DefaultSessionCache` differently for every `SessionManager`, then you could dispense with the `DefaultSessionCacheFactory` and simply instantiate, configure, and pass in the `DefaultSessionCache` yourself.

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/session/SessionDocs.java[tags=defaultsessioncache]
----

NOTE: If you don't configure any `SessionCache` or `SessionCacheFactory`, a `SessionManager` will automatically create its own `DefaultSessionCache`.

[[null]]
=== The NullSessionCache

The link:{javadoc-url}/org/eclipse/jetty/session/NullSessionCache.html[NullSessionCache] does not actually cache any objects: each request uses a fresh `ManagedSession` object.
It is suitable for clustered deployments without a sticky load balancer and non-clustered deployments when purely minimal support for sessions is needed.

As no sessions are actually cached, of course functions like `invalidateOnShutdown` and all of the eviction strategies have no meaning for the `NullSessionCache`.

There is a link:{javadoc-url}/org/eclipse/jetty/session/NullSessionCacheFactory.html[NullSessionCacheFactory] which you can instantiate, configure and set as a `Server` bean to enable a `SessionManager` to automatically create new ``NullSessionCache``s as needed.
All of the same configuration options are available on the `NullSessionCacheFactory` as the `NullSessionCache` itself.
Alternatively, if you only have a single `SessionManager`, or you need to configure a `NullSessionCache` differently for every `SessionManager`, then you could dispense with the `NullSessionCacheFactory` and simply instantiate, configure, and pass in the `NullSessionCache` yourself.


[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/session/SessionDocs.java[tags=nullsessioncache]
----

[[customcache]]
=== Implementing a custom SessionCache

As previously mentioned, it is strongly recommended that you extend the link:{javadoc-url}/org/eclipse/jetty/session/AbstractSessionCache.html[AbstractSessionCache].

=== Heterogeneous caching

Using one of the ``SessionCacheFactory``s will ensure that every time a `SessionManager` starts it will create a new instance of the corresponding type of `SessionCache`.

But, what if you deploy multiple web applications, and for one of them, you don't want to use sessions?
Or alternatively, you don't want to use sessions, but you have one web application that now needs them?
In that case, you can configure the `SessionCacheFactory` appropriate to the majority, and then specifically create the right type of `SessionCache` for the others.
Here's an example where we configure the `DefaultSessionCacheFactory` to handle most web applications, but then specifically use a `NullSessionCache` for another:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/session/SessionDocs.java[tags=mixedsessioncache]
----

[[datastore]]
== The SessionDataStore

A link:{javadoc-url}/org/eclipse/jetty/session/SessionDataStore.html[SessionDataStore] mediates the storage, retrieval and deletion of `SessionData`.
There is one `SessionDataStore` per `SessionCache` and thus one per context.
Jetty provides a number of alternative `SessionDataStore` implementations:

[plantuml]
----
title SessionDataStores

interface SessionDataStore
class AbstractSessionDataStore
class NullSessionDataStore
class FileSessionDataStore
class GCloudSessionDataStore
class HazelcastSessionDataStore
class InfinispanSessionDataStore
class JDBCSessionDataStore
class MongoSessionDataStore
class CachingSessionDataStore


SessionDataStore <|-- AbstractSessionDataStore
AbstractSessionDataStore <|-- NullSessionDataStore
AbstractSessionDataStore <|-- FileSessionDataStore
AbstractSessionDataStore <|-- GCloudSessionDataStore
AbstractSessionDataStore <|-- HazelcastSessionDataStore
AbstractSessionDataStore <|-- InfinispanSessionDataStore
AbstractSessionDataStore <|-- JDBCSessionDataStore
AbstractSessionDataStore <|-- MongoSessionDataStore
SessionDataStore <|-- CachingSessionDataStore
----

NullSessionDataStore::
Does not store `SessionData`, meaning that sessions will exist in-memory only.
See <<datastore-null,NullSessionDataStore>>

FileSessionDataStore::
Uses the file system to persist `SessionData`.
See <<datastore-file,FileSessionDataStore>> for more information.

GCloudSessionDataStore::
Uses GCloud Datastore for persisting `SessionData`.
See <<datastore-gcloud,GCloudSessionDataStore>> for more information.

HazelcastSessionDataStore::
Uses Hazelcast for persisting `SessionData`.

InfinispanSessionDataStore::
Uses http://infinispan.org[Infinispan] for persisting `SessionData`.
See <<datastore-infinispan,InfinispanSessionDataStore>> for more information.

JDBCSessionDataStore::
Uses a relational database via JDBC API to persist `SessionData`.
See <<datastore-jdbc,JDBCSessionDataStore>> for more information.

MongoSessionDataStore::
Uses http://www.mongodb.com[MongoDB] document database to persist `SessionData`.
See <<datastore-mongo,MongoSessionDataStore>> for more information.

CachingSessionDataStore::
Uses http://memcached.org[memcached] to provide an L2 cache of `SessionData` while delegating to another `SessionDataStore` for persistence of `SessionData`.
See <<cachingsessiondatastore,CachingSessionDataStore>> for more information.

Most of the behaviour common to ``SessionDataStore``s is provided by the link:{javadoc-url}/org/eclipse/jetty/session/AbstractSessionDataStore.html[AbstractSessionDataStore] class.
You are strongly encouraged to use this as the base class for implementing your custom `SessionDataStore`.

Some important methods are:

isPassivating()::
Boolean. "True" means that session data is _serialized_.
Some persistence mechanisms serialize, such as JDBC, GCloud Datastore etc.
Others can store an object in shared memory, e.g. Infinispan and thus don't serialize session data.
In Servlet environments, whether a `SessionDataStore` reports that it is capable of passivating controls whether ``HttpSessionActivationListener``s will be called.
When implementing a custom `SessionDataStore` you need to decide whether you will support passivation or not.

[[datastore-skip]]
//tag::common-datastore-config[]
setSavePeriodSec(int) _[Default:0]_ ::
This is an interval defined in seconds.
It is used to reduce the frequency with which `SessionData` is written.
Normally, whenever the last concurrent request leaves a `Session`, the `SessionData` for that `Session` is always persisted, even if the only thing that changed is the `lastAccessTime`.
If the `savePeriodSec` is non-zero, the `SessionData` will not be persisted if no session attributes changed, _unless_ the time since the last save exceeds the `savePeriod`.
Setting a non-zero value can reduce the load on the persistence mechanism, but in a clustered environment runs the risk that other nodes will see the session as expired because it has not been persisted sufficiently recently.

setGracePeriodSec(int) _[Default:3600]_ ::
The `gracePeriod` is an interval defined in seconds.
It is an attempt to deal with the non-transactional nature of sessions with regard to finding sessions that have expired.
In a clustered configuration - even with a sticky load balancer - it is always possible that a session is "live" on a node but not yet updated in the persistent store.
This means that it can be hard to determine at any given moment whether a clustered session has truly expired.
Thus, we use the `gracePeriod` to provide a bit of leeway around the moment of expiry during <<housekeeper,scavenging>>:

* on every <<housekeeper,scavenge>> cycle an `AbstractSessionDataStore` searches for sessions that belong to the context that expired at least one `gracePeriod` ago
* infrequently the `AbstractSessionDataStore` searches for and summarily deletes sessions - from any context - that expired at least 10 ``gracePeriod``s ago
//end::common-datastore-config[]

=== Custom SessionDataStores
When implementing a `SessionDataStore` for a particular persistence technology, you should base it off the `AbstractSessionDataStore` class.

Firstly, it is important to understand the components of a unique key for a session suitable for storing in a persistence mechanism.
Consider that although multiple contexts may share the _same_ session id (ie cross-context dispatch), the data in those sessions must be distinct.
Therefore, when storing session data in a persistence mechanism that is shared by many nodes in a cluster, the session must be identified by a combination of the id _and_ the context.

The ``SessionDataStore``s use the following information to synthesize a unique key for session data that is suitable to the particular persistence mechanism :
[[key]]
id::
This is the id as generated by the `SessionIdManager`
context::
The path of the context associated with the session.
virtual host::
The first virtual host - if any - associated with the context.

The link:{javadoc-url}/org/eclipse/jetty/session/SessionContext.html[SessionContext] class, of which every `AbstractSessionDataStore` has an instance, will provide these components to you in a canonicalized form.

Then you will need to implement the following methods:

public boolean doExists(String id)::
Check if data for the given session exists in your persistence mechanism.
The id is always relative to the context, see <<key,above>>.

public void doStore(String id, SessionData data, long lastSaveTime)::
Store the session data into your persistence mechanism.
The id is always relative to the context, see <<key,above>>.

public SessionData doLoad(String id)::
Load the session from your persistent mechanism.
The id is always relative to the context, see <<key,above>>.

public Set<String> doCheckExpired(Set<String> candidates, long time)::
Verify which of the suggested session ids have expired since the time given, according to the data stored in your persistence mechanism.
This is used during scavenging to ensure that a session that is a candidate for expiry according to _this_ node is not in-use on _another_ node.
The sessions matching these ids will be loaded as ``ManagedSession``s and have their normal expiration lifecycle events invoked.
The id is always relative to the context, see <<key,above>>.

public Set<String> doGetExpired(long before)::
Find the ids of sessions that expired at or before the time given.
The sessions matching these ids will be loaded as ``ManagedSession``s and have their normal expiration lifecycle events invoked.
The id is always relative to the context, see <<key,above>>.

public void doCleanOrphans(long time)::
Find the ids of sessions that expired at or before the given time, _independent of the context they are in_.
The purpose is to find sessions that are no longer being managed by any node.
These sessions may even belong to contexts that no longer exist.
Thus, any such sessions must be summarily deleted from the persistence mechanism and cannot have their normal expiration lifecycle events invoked.

=== The SessionDataStoreFactory

Every `SessionDataStore` has a factory class that creates instances based on common configuration.

All `SessionDataStoreFactory` implementations support configuring:

setSavePeriodSec(int)::
setGracePeriodSec(int)::

[[datastore-null]]
=== The NullSessionDataStore

The `NullSessionDataStore` is a trivial implementation of `SessionDataStore` that does not persist `SessionData`.
Use it when you want your sessions to remain in memory _only_.
Be careful of your `SessionCache` when using the `NullSessionDataStore`:

* if using a `NullSessionCache` then your sessions are neither shared nor saved
* if using a `DefaultSessionCache` with eviction settings, your session will cease to exist when it is evicted from the cache

If you have not configured any other <<datastore,SessionDataStore>>, when a `SessionHandler` aka `AbstractSessionManager` starts up, it will instantiate a `NullSessionDataStore`.

[[datastore-file]]
=== The FileSessionDataStore

The `FileSessionDataStore` supports persistent storage of session data in a filesystem.

IMPORTANT: Persisting sessions to the local file system should *never* be used in a clustered environment.

One file represents one session in one context.

File names follow this pattern:

 [expiry]_[contextpath]_[virtualhost]_[id]

expiry::
This is the expiry time in milliseconds since the epoch.

contextpath::
This is the context path with any special characters, including `/`, replaced by the `_` underscore character.
For example, a context path of `/catalog` would become `_catalog`.
A context path of simply `/` becomes just `__`.

virtualhost::
This is the first virtual host associated with the context and has the form of 4 digits separated by `.` characters.
If there are no virtual hosts associated with a context, then `0.0.0.0` is used:

  [digit].[digit].[digit].[digit]

id::
This is the unique id of the session.

Putting all of the above together as an example, a session with an id of `node0ek3vx7x2y1e7pmi3z00uqj1k0` for the context with path `/test` with no virtual hosts and an expiry of `1599558193150` would have a file name of:

`1599558193150__test_0.0.0.0_node0ek3vx7x2y1e7pmi3z00uqj1k0`


You can configure either a link:{javadoc-url}/org/eclipse/jetty/session/FileSessionDataStore.html[FileSessionDataStore] individually, or a `FileSessionDataStoreFactory` if you want multiple ``SessionHandler``s to use ``FileSessionDataStore``s that are identically configured.
The configuration methods are:

setStoreDir(File) _[Default:null]_ ::
This is the location for storage of session files.
If the directory does not exist at startup, it will be created.
If you use the same `storeDir` for multiple `SessionHandlers`, then the sessions for all of those contexts are stored in the same directory.
This is not a problem, as the name of the file is unique because it contains the context information.
You _must_ supply a value for this, otherwise startup of the `FileSessionDataStore` will fail.

deleteUnrestorableFiles(boolean) _[Default:false]_ ::

If set to `true`, unreadable files will be deleted.
This is useful to prevent repeated logging of the same error when the <<housekeeper,scavenger>> periodically (re-)attempts to load the corrupted information for a session in order to expire it.

setSavePeriodSec(int) _[Default:0]_ ::
This is an interval defined in seconds.
It is used to reduce the frequency with which `SessionData` is written.
Normally, whenever the last concurrent request leaves a `Session`, the `SessionData` for that `Session` is always persisted, even if the only thing that changed is the `lastAccessTime`.
If the `savePeriodSec` is non-zero, the `SessionData` will not be persisted if no session attributes changed, _unless_ the time since the last save exceeds the `savePeriod`.
Setting a non-zero value can reduce the load on the persistence mechanism, but in a clustered environment runs the risk that other nodes will see the session as expired because it has not been persisted sufficiently recently.

setGracePeriodSec(int) _[Default:3600]_ ::
The `gracePeriod` is an interval defined in seconds.
It is an attempt to deal with the non-transactional nature of sessions with regard to finding sessions that have expired.
In a clustered configuration - even with a sticky load balancer - it is always possible that a session is "live" on a node but not yet updated in the persistent store.
This means that it can be hard to determine at any given moment whether a clustered session has truly expired.
Thus, we use the `gracePeriod` to provide a bit of leeway around the moment of expiry during <<housekeeper,scavenging>>:

* on every <<housekeeper,scavenge>> cycle an `AbstractSessionDataStore` searches for sessions that belong to the context that expired at least one `gracePeriod` ago
* infrequently the `AbstractSessionDataStore` searches for and summarily deletes sessions - from any context - that expired at least 10 ``gracePeriod``s ago

Here's an example of configuring a `FileSessionDataStoreFactory`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/session/SessionDocs.java[tags=filesessiondatastorefactory]
----

Here's an alternate example, configuring a `FileSessionDataStore` directly:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/session/SessionDocs.java[tags=filesessiondatastore]
----

[[datastore-jdbc]]
=== The JDBCSessionDataStore

The link:{javadoc-url}/org/eclipse/jetty/session/JDBCSessionDataStore.html[JDBCSessionDataStore] supports persistent storage of session data in a relational database.
To do that, it requires a `DatabaseAdaptor` that handles the differences between databases (eg Oracle, Postgres etc), and a `SessionTableSchema` that allows for the customization of table and column names.

[plantuml]
----
class JDBCSessionDataStore
class DatabaseAdaptor
class SessionTableSchema

JDBCSessionDataStore "1" *-- "1" DatabaseAdaptor
JDBCSessionDataStore "1" *-- "1" SessionTableSchema
----


The link:{javadoc-url}/org/eclipse/jetty/session/JDBCSessionDataStore.html[JDBCSessionDataStore] and corresponding link:{javadoc-url}/org/eclipse/jetty/session/JDBCSessionDataStoreFactory.html[JDBCSessionDataStoreFactory] support the following configuration:

setSavePeriodSec(int) _[Default:0]_ ::
This is an interval defined in seconds.
It is used to reduce the frequency with which `SessionData` is written.
Normally, whenever the last concurrent request leaves a `Session`, the `SessionData` for that `Session` is always persisted, even if the only thing that changed is the `lastAccessTime`.
If the `savePeriodSec` is non-zero, the `SessionData` will not be persisted if no session attributes changed, _unless_ the time since the last save exceeds the `savePeriod`.
Setting a non-zero value can reduce the load on the persistence mechanism, but in a clustered environment runs the risk that other nodes will see the session as expired because it has not been persisted sufficiently recently.

setGracePeriodSec(int) _[Default:3600]_ ::
The `gracePeriod` is an interval defined in seconds.
It is an attempt to deal with the non-transactional nature of sessions with regard to finding sessions that have expired.
In a clustered configuration - even with a sticky load balancer - it is always possible that a session is "live" on a node but not yet updated in the persistent store.
This means that it can be hard to determine at any given moment whether a clustered session has truly expired.
Thus, we use the `gracePeriod` to provide a bit of leeway around the moment of expiry during <<housekeeper,scavenging>>:

* on every <<housekeeper,scavenge>> cycle an `AbstractSessionDataStore` searches for sessions that belong to the context that expired at least one `gracePeriod` ago
* infrequently the `AbstractSessionDataStore` searches for and summarily deletes sessions - from any context - that expired at least 10 ``gracePeriod``s ago

setDatabaseAdaptor(DatabaseAdaptor)::
A `JDBCSessionDataStore` requires a `DatabaseAdapter`, otherwise an `Exception`  is thrown  at start time.

setSessionTableSchema(SessionTableSchema)::
If a `SessionTableSchema` has not been explicitly set, one with all values defaulted is created at start time.

==== The DatabaseAdaptor

Many databases use different keywords for types such as `long`, `blob` and `varchar`.
Jetty will detect the type of the database at runtime by interrogating the metadata associated with a database connection.
Based on that metadata Jetty will try to select that database's preferred keywords.
However, you may need to instead explicitly configure these as described below.

setDatasource(String)::
setDatasource(Datasource)::
Either the JNDI name of a `Datasource` to look up, or the `Datasource` itself.
Alternatively you can set the *driverInfo*, see  below.

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/session/SessionDocs.java[tags=dbaDatasource]
----

setDriverInfo(String, String)::
setDriverInfo(Driver, String)::
This is the name or instance of a `Driver` class and a connection URL.
Alternatively you can set the *datasource*, see above.
[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/session/SessionDocs.java[tags=dbaDriver]
----

setBlobType(String) _[Default: "blob" or "bytea" for Postgres]_ ::
The type name used to represent "blobs" by the database.

setLongType(String) _[Default: "bigint" or "number(20)" for Oracle]_ ::
The type name  used to represent large integers by the database.

setStringType(String) _[Default: "varchar"]_::
The type name used to represent character data by the database.


==== The SessionTableSchema

`SessionData` is stored in a table with one row per session.
This is the definition of the table with the table name, column names, and type keywords all at their default settings:

[caption="Table:"]
.JettySessions
[frame=all]
[cols=12*,options="header"]
|===
|sessionId
|contextPath
|virtualHost
|lastNode
|accessTime
|lastAccessTime
|createTime
|cookieTime
|lastSavedTime
|expiryTime
|maxInterval
|map
|120 varchar|60 varchar|60 varchar|60 varchar|long|long|long|long|long|long|long|blob
|===

Use the `SessionTableSchema` class to customize these names.

setSchemaName(String), setCatalogName(String) _[Default: null]_ ::
The exact meaning of these two are dependent on your database vendor, but can broadly be described as further scoping for the session table name.
See https://en.wikipedia.org/wiki/Database_schema and https://en.wikipedia.org/wiki/Database_catalog.
These extra scoping names come into play at startup time when Jetty determines if the session table already exists, or creates it on-the-fly.
If your database is not using schema or catalog name scoping, leave these unset.
If your database is configured with a schema or catalog name, use the special value "INFERRED" and Jetty will extract them from the database metadata.
Alternatively, set them explicitly using these methods.

setTableName(String) _[Default:"JettySessions"]_ ::
This is the name of the table in which session data is stored.

setAccessTimeColumn(String) _[Default: "accessTime"]_ ::
This is the name of the column that stores the time - in ms since the epoch - at which a session was last accessed

setContextPathColumn(String) _[Default: "contextPath"]_ ::
This is the name of the column that stores the `contextPath` of a session.

setCookieTimeColumn(String) _[Default: "cookieTime"]_::
This is the name of the column that stores the time - in ms since the epoch - that the cookie was last set for a session.

setCreateTimeColumn(String) _[Default: "createTime"]_ ::
This is the name of the column that stores the time - in ms since the epoch - at which a session was created.

setExpiryTimeColumn(String) _[Default: "expiryTime"]_ ::
This is name of the column that stores - in ms since the epoch - the time at which a session will expire.

setLastAccessTimeColumn(String) _[Default: "lastAccessTime"]_ ::
This is the name of the column that stores the time - in ms since the epoch - that a session was previously accessed.

setLastSavedTimeColumn(String) _[Default: "lastSavedTime"]_ ::
This is the name of the column that stores the time - in ms since the epoch - at which a session was last written.

setIdColumn(String) _[Default: "sessionId"]_ ::
This is the name of the column that stores the id of a session.

setLastNodeColumn(String) _[Default: "lastNode"]_ ::
This is the name of the column that stores the `workerName` of the last node to write a session.

setVirtualHostColumn(String) _[Default: "virtualHost"]_ ::
This is the name of the column that stores the first virtual host of the context of a session.

setMaxIntervalColumn(String) _[Default: "maxInterval"]_ ::
This is the name of the column that stores the interval - in ms - during which a session can be idle before being considered expired.

setMapColumn(String) _[Default: "map"]_ ::
This is the name of the column that stores the serialized attributes of a session.

[[datastore-mongo]]
=== The MongoSessionDataStore

The `MongoSessionDataStore` supports persistence of `SessionData` in a nosql database.

The best description for the document model for session information is found in the javadoc for the link:{javadoc-url}/org/eclipse/jetty/nosql/mongodb/MongoSessionDataStore.html[MongoSessionDataStore].
In overview, it can be represented thus:

[plantuml]
----
database HttpSessions {
  folder jettySessions {
    file session {
      file "context" {
        rectangle attributes
      }
    }
  }
}
----

The database contains a document collection for the sessions.
Each document represents a session id, and contains one nested document per context in which that session id is used.
For example, the session id `abcd12345` might be used by two contexts, one with path `/contextA` and one with path `/contextB`.
In that case, the outermost document would refer to `abcd12345` and it would have a nested document for `/contextA` containing the session attributes for that context, and another nested document for `/contextB` containing the session attributes for that context.
Remember, according to the Servlet Specification, a session id can be shared by many contexts, but the attributes must be unique per context.

The outermost document contains these fields:

id::
The session id.
created::
The time (in ms since the epoch) at which the session was first created in any context.
maxIdle::
The time (in ms) for which an idle session is regarded as valid.
As maxIdle times can be different for ``Session``s from different contexts, this is the _shortest_ maxIdle time.
expiry::
The time (in ms since the epoch) at which the session will expire.
As the expiry time can be different for ``Session``s from different contexts, this is the _shortest_ expiry time.

Each nested context-specific document contains:

attributes::
The session attributes as a serialized map.
lastSaved::
The time (in ms since the epoch) at which the session in this context was saved.
lastAccessed::
The time (in ms since the epoch) at which the session in this context was previously accessed.
accessed::
The time (in ms since the epoch) at which this session was most recently accessed.
lastNode::
The <<workername,workerName>> of the last server that saved the session data.
version::
An object that is updated every time a session is written for a context.

You can configure either a link:{javadoc-url}/org/eclipse/jetty/nosql/mongodb/MongoSessionDataStore.html[MongoSessionDataStore] individually, or a link:{javadoc-url}/org/eclipse/jetty/nosql/mongodb/MongoSessionDataStoreFactory.html[MongoSessionDataStoreFactory] if you want multiple ``SessionHandler``s to use ``MongoSessionDataStore``s that are identically configured.
The configuration methods for the `MongoSessionDataStoreFactory` are:

setSavePeriodSec(int) _[Default:0]_ ::
This is an interval defined in seconds.
It is used to reduce the frequency with which `SessionData` is written.
Normally, whenever the last concurrent request leaves a `Session`, the `SessionData` for that `Session` is always persisted, even if the only thing that changed is the `lastAccessTime`.
If the `savePeriodSec` is non-zero, the `SessionData` will not be persisted if no session attributes changed, _unless_ the time since the last save exceeds the `savePeriod`.
Setting a non-zero value can reduce the load on the persistence mechanism, but in a clustered environment runs the risk that other nodes will see the session as expired because it has not been persisted sufficiently recently.

setGracePeriodSec(int) _[Default:3600]_ ::
The `gracePeriod` is an interval defined in seconds.
It is an attempt to deal with the non-transactional nature of sessions with regard to finding sessions that have expired.
In a clustered configuration - even with a sticky load balancer - it is always possible that a session is "live" on a node but not yet updated in the persistent store.
This means that it can be hard to determine at any given moment whether a clustered session has truly expired.
Thus, we use the `gracePeriod` to provide a bit of leeway around the moment of expiry during <<housekeeper,scavenging>>:

* on every <<housekeeper,scavenge>> cycle an `AbstractSessionDataStore` searches for sessions that belong to the context that expired at least one `gracePeriod` ago
* infrequently the `AbstractSessionDataStore` searches for and summarily deletes sessions - from any context - that expired at least 10 ``gracePeriod``s ago

setDbName(String)::
This is the name of the database.
setCollectionName(String)::
The name of the document collection.

setConnectionString(String)::
a mongodb url, eg "mongodb://localhost".
Alternatively, you can specify the *host,port* combination instead, see below.
setHost(String)::
setPort(int)::
the hostname and port number of the mongodb instance to contact.
Alternatively, you can specify the *connectionString* instead, see above.

This is an example of configuring a `MongoSessionDataStoreFactory`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/session/SessionDocs.java[tags=mongosdfactory]
----

[[datastore-infinispan]]
=== The InfinispanSessionDataStore

The `InfinispanSessionDataStore` supports persistent storage of session data via the https://infinispan.org/[Infinispan] data grid.

You may use Infinispan in either _embedded mode_, where it runs in the same process as Jetty, or in _remote mode_ mode, where your Infinispan instance is on another node.

For more information on Infinispan, including some code examples, consult the https://infinispan.org/[Infinispan documentation].
See below for some code examples of configuring the link:{javadoc-url}/org/eclipse/jetty/session/infinispan/InfinispanSessionDataStore.html[InfinispanSessionDataStore] in Jetty.
Note that the configuration options are the same for both the `InfinispanSessionDataStore` and the link:{javadoc-url}/org/eclipse/jetty/session/infinispan/InfinispanSessionDataStoreFactory.html[InfinispanSessionDataStoreFactory].
Use the latter to apply the same configuration to multiple ``InfinispanSessionDataStore``s.

setSavePeriodSec(int) _[Default:0]_ ::
This is an interval defined in seconds.
It is used to reduce the frequency with which `SessionData` is written.
Normally, whenever the last concurrent request leaves a `Session`, the `SessionData` for that `Session` is always persisted, even if the only thing that changed is the `lastAccessTime`.
If the `savePeriodSec` is non-zero, the `SessionData` will not be persisted if no session attributes changed, _unless_ the time since the last save exceeds the `savePeriod`.
Setting a non-zero value can reduce the load on the persistence mechanism, but in a clustered environment runs the risk that other nodes will see the session as expired because it has not been persisted sufficiently recently.

setGracePeriodSec(int) _[Default:3600]_ ::
The `gracePeriod` is an interval defined in seconds.
It is an attempt to deal with the non-transactional nature of sessions with regard to finding sessions that have expired.
In a clustered configuration - even with a sticky load balancer - it is always possible that a session is "live" on a node but not yet updated in the persistent store.
This means that it can be hard to determine at any given moment whether a clustered session has truly expired.
Thus, we use the `gracePeriod` to provide a bit of leeway around the moment of expiry during <<housekeeper,scavenging>>:

* on every <<housekeeper,scavenge>> cycle an `AbstractSessionDataStore` searches for sessions that belong to the context that expired at least one `gracePeriod` ago
* infrequently the `AbstractSessionDataStore` searches for and summarily deletes sessions - from any context - that expired at least 10 ``gracePeriod``s ago

setCache(BasicCache<String, InfinispanSessionData> cache)::
Infinispan uses a cache API as the interface to the data grid and this method configures Jetty with the cache instance.
This cache can be either an _embedded_ cache - also called a "local" cache in Infinispan parlance - or a _remote_ cache.

setSerialization(boolean) _[Default: false]_ ::
When the `InfinispanSessionDataStore` starts, if it detects the Infinispan classes for remote caches on the classpath, it will automatically assume `serialization` is true, and thus that `SessionData` will be serialized over-the-wire to a remote cache.
You can use this parameter to override this.
If this parameter is `true`, the `InfinispanSessionDataStore` returns true for the `isPassivating()` method, but false otherwise.

setInfinispanIdleTimeoutSec(int) _[Default: 0]_ ::
This controls the Infinispan option whereby it can detect and delete entries that have not been referenced for a configurable amount of time.
A value of 0 disables it.

NOTE: If you use this option, expired sessions will be summarily deleted from Infinispan _without_ the normal session invalidation handling (eg calling of lifecycle listeners).
Only use this option if you do not have session lifecycle listeners that must be called when a session is invalidated.

setQueryManager(QueryManager)::
If this parameter is not set, the `InfinispanSessionDataStore` will be unable to scavenge for unused sessions.
In that case, you can use the `infinispanIdleTimeoutSec` option instead to prevent the accumulation of expired sessions.
When using Infinispan in _embedded_ mode, configure the link:{javadoc-url}/org/eclipse/jetty/session/infinispan/EmbeddedQueryManager.html[EmbeddedQueryManager] to enable Jetty to query for expired sessions so that they may be property invalidated and lifecycle listeners called.
When using Infinispan in _remote_ mode, configure the link:{javadoc-url}/org/eclipse/jetty/session/infinispan/RemoteQueryManager.html[RemoteQueryManager] instead.

Here is an example of configuring an `InfinispanSessionDataStore` in code using an _embedded_ cache:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/session/SessionDocs.java[tags=infinispanembed]
----

Here is an example of configuring an `InfinispanSessionDataStore` in code using a _remote_ cache:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/session/SessionDocs.java[tags=infinispanremote]
----

[[datastore-gcloud]]
=== The GCloudSessionDataStore

The `GCloudSessionDataStore` supports persistent storage of session data into https://cloud.google.com/datastore[Google Cloud DataStore].

[[datastore-gcloud-prep]]
==== Preparation

You will first need to create a project and enable the Google Cloud API: https://cloud.google.com/docs/authentication#preparation[].
Take note of the `project id` that you create in this step as you need to supply it in later steps.

You can choose to use Jetty either inside or outside of Google infrastructure.

. Outside of Google infrastructure
+
Before running Jetty, you will need to choose one of the following methods to set up the local environment to enable remote GCloud DataStore communications:

.. Using the GCloud SDK
  * Ensure you have the GCloud SDK installed: https://cloud.google.com/sdk/?hl=en[]
  * Use the GCloud tool to set up the project you created in the preparation step: `gcloud config set project PROJECT_ID`
  * Use the GCloud tool to authenticate a Google account associated with the project created in the preparation step: `gcloud auth login ACCOUNT`

 .. Using environment variables
  * Define the environment variable `GCLOUD_PROJECT` with the project id you created in the preparation step.
  * Generate a JSON https://cloud.google.com/storage/docs/authentication?hl=en#service_accounts[service account key] and then define the environment variable `GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/key.json`

. Inside of Google infrastructure
+
The Google deployment tools will automatically configure the project and authentication information for you.

Jetty GCloud session support provides some indexes as optimizations that can speed up session searches.
This will particularly benefit session scavenging, although it may make write operations slower.
By default, indexes will _not_ be used.
You will see a log `WARNING` message informing you about the absence of indexes:

 WARN: Session indexes not uploaded, falling back to less efficient queries

In order to use them, you will need to manually upload the file to GCloud that defines the indexes.
This file is named `index.yaml` and you can find it in your distribution in `$JETTY_BASE/etc/sessions/gcloud/index.yaml`.

Follow the instructions https://cloud.google.com/datastore/docs/tools/#the_development_workflow_using_gcloud[here] to upload the pre-generated `index.yaml` file.

==== Configuration

The following configuration options apply to both the link:{javadoc-url}/org/eclipse/jetty/session/GCloudSessionDataStore.html[GCloudSessionDataStore] and the link:{javadoc-url}/org/eclipse/jetty/session/GCloudSessionDataStoreFactory.html[GCloudSessionDataStoreFactory].
Use the latter if you want multiple ``SessionHandler``s to use ``GCloudSessionDataStore``s that are identically configured.

setSavePeriodSec(int) _[Default:0]_ ::
This is an interval defined in seconds.
It is used to reduce the frequency with which `SessionData` is written.
Normally, whenever the last concurrent request leaves a `Session`, the `SessionData` for that `Session` is always persisted, even if the only thing that changed is the `lastAccessTime`.
If the `savePeriodSec` is non-zero, the `SessionData` will not be persisted if no session attributes changed, _unless_ the time since the last save exceeds the `savePeriod`.
Setting a non-zero value can reduce the load on the persistence mechanism, but in a clustered environment runs the risk that other nodes will see the session as expired because it has not been persisted sufficiently recently.

setGracePeriodSec(int) _[Default:3600]_ ::
The `gracePeriod` is an interval defined in seconds.
It is an attempt to deal with the non-transactional nature of sessions with regard to finding sessions that have expired.
In a clustered configuration - even with a sticky load balancer - it is always possible that a session is "live" on a node but not yet updated in the persistent store.
This means that it can be hard to determine at any given moment whether a clustered session has truly expired.
Thus, we use the `gracePeriod` to provide a bit of leeway around the moment of expiry during <<housekeeper,scavenging>>:

* on every <<housekeeper,scavenge>> cycle an `AbstractSessionDataStore` searches for sessions that belong to the context that expired at least one `gracePeriod` ago
* infrequently the `AbstractSessionDataStore` searches for and summarily deletes sessions - from any context - that expired at least 10 ``gracePeriod``s ago

setProjectId(String) _[Default: null]_ ::
Optional.
The `project id` of your project.
You don't need to set this if you carried out the instructions in the <<datastore-gcloud-prep,Preparation>> section, but you might want to set this - along with the `host` and/or `namespace` parameters - if you want more explicit control over connecting to GCloud.

setHost(String) _[Default: null]_ ::
Optional.
This is the name of the host for the GCloud DataStore.
If you leave it unset, then the GCloud DataStore library will work out the host to contact.
You might want to use this - along with `projectId` and/or `namespace` parameters - if you want more explicit control over connecting to GCloud.

setNamespace(String) _[Default: null]_ ::
Optional.
If set, partitions the visibility of session data in multi-tenant deployments.
More information can be found https://cloud.google.com/datastore/docs/concepts/multitenancy[here.]

setMaxRetries(int) _[Default: 5]_ ::
This is the maximum number of retries to connect to GCloud DataStore in order to write a session.
This is used in conjunction with the `backoffMs` parameter to control the frequency with which Jetty will retry to contact GCloud to write out a session.

setBackoffMs(int) _[Default: 1000]_ ::
This is the interval that Jetty will wait in between retrying failed writes.
Each time a write fails, Jetty doubles the previous backoff.
Used in conjunction with the `maxRetries` parameter.

setEntityDataModel(EntityDataModel)::
The `EntityDataModel` encapsulates the type (called "kind" in GCloud DataStore) of stored session objects and the names of its fields.
If you do not set this parameter, `GCloudSessionDataStore` uses all default values, which should be sufficient for most needs.
Should you need to customize this, the methods and their defaults are:
  * *setKind(String)* _[Default: "GCloudSession"]_ this is the type of the session object.
  * *setId(String)* _[Default: "id"]_  this is the name of the field storing the session id.
  * *setContextPath(String)* _[Default: "contextPath"]_ this is name of the field storing the canonicalized context path of the context to which the session belongs.
  * *setVhost(String)* _[Default: "vhost"]_  this the name of the field storing the canonicalized virtual host of the context to which the session belongs.
  * *setAccessed(String)* _[Default: "accessed"]_  this is the name of the field storing the current access time of the session.
  * *setLastAccessed(String)* _[Default: "lastAccessed"]_ this is the name of the field storing the last access time of the session.
  * *setCreateTime(String)* _[Default: "createTime"]_ this is the name of the field storing the time in ms since the epoch, at which the session was created.
  * *setCookieSetTime(String)* _[Default: "cookieSetTime"]_ this is the name of the field storing time at which the session cookie was last set.
  * *setLastNode(String)* _[Default: "lastNode"]_ this is the name of the field storing the `workerName` of the last node to manage the session.
  * *setExpiry(String)* _[Default: "expiry"]_ this is the name of the field storing the time, in ms since the epoch, at which the session will expire.
  * *setMaxInactive(String)* _[Default: "maxInactive"]_ this is the name of the field storing the session timeout in ms.
  * *setAttributes(String)* _[Default: "attributes"]_ this is the name of the field storing the session attribute map.


Here's an example of configuring a `GCloudSessionDataStoreFactory`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/session/SessionDocs.java[tags=gcloudsessiondatastorefactory]
----

[[cachingsessiondatastore]]
=== The CachingSessionDataStore

[plantuml]
----
interface SessionDataMap
class CachingSessionDataStore
interface SessionDataStore

CachingSessionDataStore "1" *-down- "1" SessionDataMap
CachingSessionDataStore "1" *-down- "1" SessionDataStore
SessionDataMap <|-- MemcachedSessionDataMap
----

The link:{javadoc-url}/org/eclipse/jetty/session/CachingSessionDataStore.html[CachingSessionDataStore] is a special type of `SessionDataStore` that checks an L2 cache for `SessionData` before checking a delegate `SessionDataStore`.
This can improve the performance of slow stores.

The L2 cache is an instance of a link:{javadoc-url}/org/eclipse/jetty/session/SessionDataMap.html[SessionDataMap].
Jetty provides one implementation of this L2 cache based on `memcached`, link:{javadoc-url}/org/eclipse/jetty/memcached/session/MemcachedSessionDataMap.html[MemcachedSessionDataMap].

This is an example of how to programmatically configure ``CachingSessionDataStore``s, using a <<datastore-file,FileSessionDataStore>> as a delegate, and `memcached` as the L2 cache:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/session/SessionDocs.java[tags=cachingsds]
----

---

### Documentation Jetty Modules Programming Guide Pages Server Threads (`documentation/jetty/modules/programming-guide/pages/server/threads.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Server Threading Architecture

As explained in the general xref:arch/threads.adoc[Jetty threading architecture], web applications can specify the `InvocationType` of the Jetty implementation tasks that invoke web application code.

There are two main Jetty implementation tasks that invoke web application code:

* The task that invokes the `Handler` tree, detailed <<invocation-type-handler,here>>.
* The task that invokes the `Request` demand callback, detailed <<invocation-type-demand,here>>.

[[invocation-type-handler]]
== `Handler` Tree `InvocationType`

The `InvocationType` of the task that invokes the `Handler` tree is determined by the _structure_ of the `Handler` tree, and defines the `InvocationType` with which HTTP request are processed.

As explained in xref:server/http.adoc#request-processing[this section], the `Handler` tree starts at the `Server` instance.

In a simple `Handler` tree, the `Server` wraps a `ContextHandler` instance, that wraps an application code `Handler`, for example:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=simpleHandlerTreeInvocationType]
----
<1> Specifies that the `ShopHandler.handle(\...)` implementation is `NON_BLOCKING`.

The correspondent structure is the following:

[,screen]
----
Server
└── ContextHandler /shop
    └── ShopHandler
----

Both `Server` and `ContextHandler` extend from `Handler.Wrapper`, which reports the same `InvocationType` as its child `Handler`.
`ShopHandler` specifies its own `InvocationType` to be `NON_BLOCKING`, so the whole `Handler` tree is `NON_BLOCKING`.

A more complex `Handler` tree is one that uses xref:server/http.adoc#handler-use-context-collection[`ContextHandlerCollection`], for example:

[,screen]
----
Server
└── ContextHandlerCollection
    ├── ContextHandler /shop
    │   └── ShopHandler
    └── ContextHandler /api
        └── RESTHandler
----

Another more complex `Handler` tree is one that uses xref:server/http.adoc#handler-use-path-mappings[`PathMappingsHandler`], for example:

[,screen]
----
Server
└── ContextHandler /shop
    └── PathMappingsHandler
        ├── CatalogHandler  /catalog/*
        └── CheckoutHandler /checkout/*
----

Both `ContextHandlerCollection` and `PathMappingsHandler` extend from `Handler.Container` that calculates the `InvocationType` combining the ``InvocationType``s of all the children ``Handler``s (see link:{javadoc-url}/org/eclipse/jetty/util/thread/Invocable.html#combine(org.eclipse.jetty.util.thread.Invocable.InvocationType,org.eclipse.jetty.util.thread.Invocable.InvocationType)[Invocable.combine(InvocationType, InvocationType)]).

When all the children ``Handler``s are `NON_BLOCKING`, then the `Handler.Container` is also `NON_BLOCKING`.
Otherwise, if one of the children is `BLOCKING`, then the `Handler.Container` is also `BLOCKING`.

=== `WebSocketUpgradeHandler` `InvocationType`
xref:server/websocket.adoc#jetty[`WebSocketUpgradeHandler`] is a special case of `Handler.Wrapper` because it combines the `InvocationType` of the child `Handler` (that processes HTTP requests) with the `InvocationType` of the `ServerWebSocketContainer` (that processes WebSocket messages).

WebSocket applications should set the `InvocationType` of the `ServerWebSocketContainer` to match whether xref:server/websocket.adoc#jetty-endpoints[WebSocket EndPoints] have been implemented in a blocking or non-blocking way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=webSocketInvocationType]
----
<1> Setting the `InvocationType` for the `ServerWebSocketContainer`.
<2> Setting the `InvocationType` for the leaf HTTP `Handler`.

In the example above, the `Handler` tree is the following:

[,screen]
----
Server
└── ContextHandler /ws
    └── WebSocketUpgradeHandler
        └── Handler.Abstract
----

[NOTE]
====
HTTP requests that flow through the `WebSocketUpgradeHandler` are influenced by the `InvocationType` of the `WebSocketUpgradeHandler`, even if `WebSocketUpgradeHandler` just forwards them to its child `Handler`.
====

[[invocation-type-demand]]
== `Request` Demand `InvocationType`

The `InvocationType` of the task that invokes the xref:arch/io.adoc#content-source[`Request`] demand callback is determined from the demand callback object passed to link:{javadoc-url}/org/eclipse/jetty/server/Request.html#demand(java.lang.Runnable)[`Request.demand(Runnable)`], and defines the `InvocationType` with which the demand callback is invoked.

Rather than passing a simple `Runnable` or a lambda expression (for which the `InvocationType` is assumed to be `BLOCKING`), you can pass an `Invocable.Task`, that implements both `Invocable` (to specify the `InvocationType`) and `Runnable` (to specify the demand callback).

For example:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=demandInvocationType]
----
<1> Specifies the ``Handler``'s `InvocationType` for HTTP request processing.
<2> Specifies the demand callback to be an `Invocable.Task` with `InvocationType` `NON_BLOCKING`.

---

### Documentation Jetty Modules Programming Guide Pages Server Websocket (`documentation/jetty/modules/programming-guide/pages/server/websocket.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= WebSocket Server

Jetty provides different implementations of the WebSocket protocol:

* A Jakarta EE 8 (`javax.websocket`) implementation, based on the https://jakarta.ee/specifications/websocket/1.1/[Jakarta WebSocket 1.1 Specification].
* A Jakarta EE 9 (`jakarta.websocket`) implementation, based on the https://jakarta.ee/specifications/websocket/2.0/[Jakarta WebSocket 2.0 Specification].
* A Jakarta EE 10 (`jakarta.websocket`) implementation, based on the https://jakarta.ee/specifications/websocket/2.1/[Jakarta WebSocket 2.1 Specification].
* A Jakarta EE 11 (`jakarta.websocket`) implementation, based on the https://jakarta.ee/specifications/websocket/2.2/[Jakarta WebSocket 2.2 Specification].
* A Jetty/Jakarta WebSocket API implementation, dependent on Jakarta EE, offering extended WebSocket APIs.
* A Jetty WebSocket API implementation independent of Jakarta EE, based on the Jetty Core APIs.

== Comparison

The Jakarta EE implementations and APIs are described in <<standard,this section>>.

Using the standard Jakarta EE WebSocket APIs allows your applications to depend only on standard APIs, and your applications may be deployed in any compliant WebSocket Container that supports Jakarta WebSocket.
The standard Jakarta EE WebSocket APIs provide these features that are not present in the Jetty WebSocket APIs

* Encoders and Decoders for automatic conversion of text or binary messages to objects.

Alternatively, the Jetty/Jakarta WebSocket API implementation, dependent on Jakarta EE, and offering extended WebSocket APIs, is described in <<jetty-jakarta,this section>>.

This implementation provides extended APIs with respect to the Jakarta EE APIs, but still integrates with Jakarta EE.
The use of these APIs is now obsoleted in favor of the Jetty specific WebSocket APIs based on the Jetty Core APIs, but maintained for backward compatibility.

Lastly, the Jetty WebSocket API implementation, independent of Jakarta EE, is described in <<jetty,this section>>.

Using the Jetty WebSocket APIs allows your applications to be more efficient and offer greater and more fine-grained control, and provide the following features that are not present in the Jakarta EE WebSocket APIs:

* A demand mechanism to control backpressure.
* Remote socket address (IP address and port) information.
* Advanced request URI matching with regular expressions, in addition to Servlet patterns and URI template patterns.
* More configuration options, for example the network buffer capacity.
* Programmatic WebSocket upgrade, in addition to WebSocket upgrade based on URI matching, for maximum flexibility.

If your application needs specific features that are not provided by the standard APIs, the Jetty WebSocket APIs may provide such features.

TIP: If the feature you are looking for is not present, you may ask for these features by https://github.com/jetty/jetty.project/issues[submitting an issue] to the Jetty Project.
In this way, you will avoid waiting for the standard Jakarta EE process to approve them and release a new version of the Jakarta EE WebSocket specification.

[[standard]]
== Standard APIs Implementation

When you write a WebSocket application using the Jakarta EE `jakarta.websocket` APIs, your code typically need to depend on just the Jakarta EE WebSocket APIs to compile your application.
However, at runtime you need to have an implementation of the Jakarta EE WebSocket APIs in your class-path/module-path.

The standard `jakarta.websocket` APIs, for example for Jakarta {ee-current-caps}, are provided by the following Maven artifact:

[,xml]
----
<dependency>
  <groupId>jakarta.websocket</groupId>
  <artifactId>jakarta.websocket-api</artifactId>
  <version>2.2.0</version>
</dependency>
----

At runtime, you also need an implementation of the standard Jakarta {ee-current-caps} WebSocket APIs, that Jetty provides with the following Maven artifact (and its transitive dependencies):

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty.{ee-current}.websocket</groupId>
  <artifactId>jetty-{ee-current}-websocket-jakarta-server</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

[NOTE]
====
The `jakarta.websocket-api` artifact and the `jetty-{ee-current}-websocket-jakarta-server` artifact (and their transitive dependencies) should be present in the server class-path/module-path, and never in the web application's `/WEB-INF/lib` directory.
====

To configure correctly your WebSocket application based on the standard Jakarta {ee-current-caps} WebSocket APIs, you need two steps:

. Make sure that Jetty sets up an instance of `jakarta.websocket.server.ServerContainer`, described in <<standard-container,this section>>.
. Configure the WebSocket endpoints that implement your application logic, either by annotating their classes with the standard `jakarta.websocket` annotations, or by using the `ServerContainer` APIs to register them in your code, described in <<standard-endpoints,this section>>.

[[standard-container]]
=== Setting Up `ServerContainer`

Jetty sets up a `jakarta.websocket.server.ServerContainer` instance using `JakartaWebSocketServletContainerInitializer`.

When you deploy web applications using xref:server/http.adoc#handler-use-webapp-context[`WebAppContext`], then  `JakartaWebSocketServletContainerInitializer` is automatically discovered (because it is present in the server class-path/module-path) and initialized by Jetty when the web application starts, so that it sets up the `ServerContainer`.
In this way, you do not need to write any additional code:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=standardContainerWebAppContext]
----

On the other hand, when you deploy web applications using xref:server/http.adoc#handler-use-servlet-context[`ServletContextHandler`], you have to write the code to ensure that the `JakartaWebSocketServletContainerInitializer` is initialized, so that it sets up the `ServerContainer`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=standardContainerServletContextHandler]
----

Calling `JakartaWebSocketServletContainerInitializer.configure(\...)` must be done _before_ the `ServletContextHandler` is started, and configures the Jakarta {ee-current-caps} WebSocket implementation for that web application context, making `ServerContainer` available to web applications.

[[standard-endpoints]]
=== Configuring Endpoints

Once you have <<standard-container,set up>> the `ServerContainer`, you can configure your WebSocket endpoints.

The WebSocket endpoints classes may be either annotated with the standard `jakarta.websocket` annotations, extend the `jakarta.websocket.Endpoint` abstract class, or implement the `jakarta.websocket.server.ServerApplicationConfig` interface.

When you deploy web applications using xref:server/http.adoc#handler-use-webapp-context[`WebAppContext`], then annotated WebSocket endpoint classes are automatically discovered and registered.
In this way, you do not need to write any additional code; you just need to ensure that your WebSocket endpoint classes are present in the web application's `/WEB-INF/classes` directory, or in a `*.jar` file in `/WEB-INF/lib`.

On the other hand, when you deploy web applications using xref:server/http.adoc#handler-use-webapp-context[`WebAppContext`] but you need to perform more advanced configuration of the `ServerContainer` or of the WebSocket endpoints, or when you deploy web applications using xref:server/http.adoc#handler-use-servlet-context[`ServletContextHandler`], you need to access the `ServerContainer` APIs.

The `ServerContainer` instance is stored as a `ServletContext` attribute, so it can be retrieved when the `ServletContext` is initialized, either from a `ServletContextListener`, or from a Servlet `Filter`, or from an `HttpServlet`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=standardEndpointsInitialization]
----

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=standardWebSocketInitializerServlet]
----

When you deploy web applications using xref:server/http.adoc#handler-use-servlet-context[`ServletContextHandler`], you can alternatively use the code below to set up the `ServerContainer` and configure the WebSocket endpoints in one step:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=standardContainerAndEndpoints]
----

When the `ServletContextHandler` is started, the `Configurator` lambda (the second parameter passed to `JakartaWebSocketServletContainerInitializer.configure(\...)`) is invoked and allows you to explicitly configure the WebSocket endpoints using the standard APIs provided by `ServerContainer`.

[[standard-upgrade]]
==== Upgrade to WebSocket

Under the hood, `JakartaWebSocketServletContainerInitializer` installs the `org.eclipse.jetty.{ee-current}.websocket.servlet.WebSocketUpgradeFilter`, which is the component that intercepts HTTP requests to upgrade to WebSocket, and performs the upgrade from the HTTP protocol to the WebSocket protocol.

[NOTE]
====
The `WebSocketUpgradeFilter` is installed under the filter name corresponding to its class name (that is, the string `"org.eclipse.jetty.{ee-current}.websocket.servlet.WebSocketUpgradeFilter"`) and with a filter mapping of `/*`.

Refer to the <<configure-filter,advanced `WebSocketUpgradeFilter` configuration section>> for more information.
====

With the default configuration, every HTTP request flows first through the `WebSocketUpgradeFilter`.

If the HTTP request is a valid upgrade to WebSocket, then `WebSocketUpgradeFilter` tries to find a matching WebSocket endpoint for the request URI path; if the match is found, `WebSocketUpgradeFilter` performs the upgrade and does not invoke any other Filter or Servlet.
From this point on, the communication happens with the WebSocket protocol, and HTTP components such as Filters and Servlets are not relevant anymore.

If the HTTP request is not an upgrade to WebSocket, or `WebSocketUpgradeFilter` did not find a matching WebSocket endpoint for the request URI path, then the request is passed to the Filter chain of your web application, and eventually the request arrives to a Servlet to be processed (otherwise a `404 Not Found` response is returned to client).

[[configure-filter]]
== Advanced `WebSocketUpgradeFilter` Configuration

The `WebSocketUpgradeFilter` that handles the HTTP requests that upgrade to WebSocket is installed by the `JakartaWebSocketServletContainerInitializer`, as described in <<standard,this section>>.

Typically, the `WebSocketUpgradeFilter` is not present in the `web.xml` configuration, and therefore the mechanisms above create a new `WebSocketUpgradeFilter` and install it _before_ any other Filter declared in `web.xml`, under the default name of `"org.eclipse.jetty.{ee-current}.websocket.servlet.WebSocketUpgradeFilter"` and with path mapping `/*`.

However, if the `WebSocketUpgradeFilter` is already present in `web.xml` under the default name, then the ``ServletContainerInitializer``s will use that declared in `web.xml` instead of creating a new one.

This allows you to customize:

* The filter order; for example, by configuring filters for increased security or authentication _before_ the `WebSocketUpgradeFilter`.
* The `WebSocketUpgradeFilter` configuration via ``init-param``s, that affects all `Session` instances created by this filter.
* The `WebSocketUpgradeFilter` path mapping. Rather than the default mapping of `+/*+`, you can map the `WebSocketUpgradeFilter` to a more specific path such as `+/ws/*+`.
* The possibility to have multiple ``WebSocketUpgradeFilter``s, mapped to different paths, each with its own configuration.

For example:

[,xml,subs=attributes+]
----
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
         version="5.0">
  <display-name>My WebSocket WebApp</display-name>

   
  <filter>
    <filter-name>security</filter-name>
    <filter-class>com.acme.SecurityFilter</filter-class>
    <async-supported>true</async-supported>
  </filter>
  <filter-mapping>
    <filter-name>security</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

   
  <filter>
    
    <filter-name>org.eclipse.jetty.{ee-current}.websocket.servlet.WebSocketUpgradeFilter</filter-name> 
    <filter-class>org.eclipse.jetty.{ee-current}.websocket.servlet.WebSocketUpgradeFilter</filter-class>
    
    <init-param> 
      <param-name>maxTextMessageSize</param-name>
      <param-value>1048576</param-value>
    </init-param>
    <async-supported>true</async-supported>
  </filter>
  <filter-mapping>
    <filter-name>org.eclipse.jetty.{ee-current}.websocket.servlet.WebSocketUpgradeFilter</filter-name>
    
    <url-pattern>/ws/*</url-pattern> 
  </filter-mapping>

</web-app>
----
<1> The custom `SecurityFilter` is the first, to apply custom security.
<2> The configuration for the _default_ `WebSocketUpgradeFilter`.
<3> Note the use of the _default_ `WebSocketUpgradeFilter` name.
<4> Specific configuration for `WebSocketUpgradeFilter` parameters.
<5> Use a more specific path mapping for `WebSocketUpgradeFilter`.

Note that using a more specific path mapping for WebSocket requests is also beneficial to the performance of normal HTTP requests: they do not go through the `WebSocketUpgradeFilter` (as they will not match its path mapping), saving the cost of analyzing them to see whether they are WebSocket upgrade requests or not.

[[jetty-jakarta]]
== Jetty/Jakarta WebSocket APIs Implementation

When you write a WebSocket application using the Jetty/Jakarta WebSocket APIs, your code needs to depend on both the Jetty WebSocket APIs and the Jakarta Servlet APIs to compile your application.
However, at runtime you need to have the _implementation_ of the Jetty/Jakarta WebSocket APIs in your class-path (or module-path).

Jetty/Jakarta WebSocket APIs are provided by the following Maven artifact for Jakarta EE 10 to Jakarta {ee-current-caps}

[,xml,subs=normal]
----
<dependency>
  <groupId>org.eclipse.jetty.websocket</groupId>
  <artifactId>jetty-websocket-jetty-api</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

For Java EE 8 and Jakarta EE 9 they are provided by the following Maven artifact (adjust the EE version number accordingly):

[,xml,subs=normal]
----
<dependency>
  <groupId>org.eclipse.jetty.websocket</groupId>
  <artifactId>jetty-ee9-websocket-jetty-api</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

The implementation is provided by the following Maven artifact and its transitive dependencies (adjust the EE version number accordingly):

[,xml,subs=normal]
----
<dependency>
  <groupId>org.eclipse.jetty.{ee-current}.websocket</groupId>
  <artifactId>jetty-{ee-current}-websocket-jetty-server</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

[NOTE]
====
The `jetty-websocket-jetty-api` artifact and the `jetty-{ee-current}-websocket-jetty-server` artifact (and its transitive dependencies) should be present in the server class-path (or module-path), and never in the web application's `/WEB-INF/lib` directory.
====

You can read more about the xref:client/websocket.adoc#architecture[Jetty WebSocket architecture], which is common to both client-side and server-side, to get familiar with the terminology used in the following sections.

The WebSocket upgrade can be performed by either:

- The `WebSocketUpgradeFilter` -- WebSocket mappings are configured through the `JettyWebSocketServerContainer`.
- The `JettyWebSocketServlet` -- WebSocket mappings are configured through the `JettyWebSocketServletFactory` provided in the `JettyWebSocketServlet.configure()` method.

[[jetty-jakarta-container]]
=== Setting up `JettyWebSocketServerContainer`

You need Jetty to set up a `JettyWebSocketServerContainer` instance, in order to use the Jetty WebSocket APIs through the `WebSocketUpgradeFilter`.

Jetty sets up a `JettyWebSocketServerContainer` instance using `JettyWebSocketServletContainerInitializer`.

When you deploy web applications using xref:server/http.adoc#handler-use-webapp-context[`WebAppContext`], then  `JettyWebSocketServletContainerInitializer` is automatically discovered and initialized by Jetty when the web application starts, so that it sets up the `JettyWebSocketServerContainer`.

In this way, you do not need to write any additional code:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=standardContainerWebAppContext]
----

On the other hand, when you deploy web applications using xref:server/http.adoc#handler-use-servlet-context[`ServletContextHandler`], you have to write the code to ensure that the `JettyWebSocketServletContainerInitializer` is initialized, so that it sets up the `JettyWebSocketServerContainer`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=jettyContainerServletContextHandler]
----

Calling `JettyWebSocketServletContainerInitializer.configure(\...)` must be done _before_ the `ServletContextHandler` is started, and configures the Jetty WebSocket implementation for that web application context.

[[jetty-jakarta-endpoints]]
=== Configuring Endpoints

Once you have <<jetty-container,set up>> the `JettyWebSocketServerContainer`, you can configure your xref:client/websocket.adoc#endpoints[WebSocket endpoints].

Differently from the <<standard-endpoints,configuration of standard WebSocket endpoints>>, WebSocket endpoint classes may be annotated with Jetty WebSocket API annotations, or extend the `org.eclipse.jetty.websocket.api.Session.Listener` interface, but they are not automatically discovered, not even when deploying web applications using xref:server/http.adoc#handler-use-webapp-context[`WebAppContext`].

[IMPORTANT]
====
When using the Jetty WebSocket APIs, WebSocket endpoints must always be explicitly configured.
====

There are two ways of configuring WebSocket endpoints when using the Jetty WebSocket APIs:

* <<jetty-jakarta-endpoints-container,Using `JettyWebSocketServerContainer`>>, which is very similar to how WebSocket endpoints are configured when using the <<standard-endpoints,standard `javax.websocket` APIs>>, but also provides APIs to perform a direct, programmatic, WebSocket upgrade.
* <<jetty-jakarta-endpoints-servlet,Using `JettyWebSocketServlet`>>, which may configured in `web.xml`, rather than in Java code.

[[jetty-jakarta-endpoints-container]]
==== Using `JettyWebSocketServerContainer`

To register WebSocket endpoints using the Jetty WebSocket APIs you need to access the `JettyWebSocketServerContainer` APIs.

The `JettyWebSocketServerContainer` instance is stored in the `ServletContext`, so it can be retrieved when the `ServletContext` is initialized, either from a `ServletContextListener` or from a `HttpServlet`:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=jettyEndpointsInitialization]
----

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=jettyWebSocketInitializerServlet]
----

You can also use this variant to set up the `JettyWebSocketServerContainer` and configure the WebSocket endpoints in one step:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=jettyContainerAndEndpoints]
----

In the call to `JettyWebSocketServerContainer.addMapping(\...)`, you can specify a _path spec_ (the first parameter) that can be configured as specified in <<jetty-pathspec,this section>>.

When the `ServletContextHandler` is started, the `Configurator` lambda (the second parameter passed to `JettyWebSocketServletContainerInitializer.configure(\...)`) is invoked and allows you to explicitly configure the WebSocket endpoints using the Jetty WebSocket APIs provided by `JettyWebSocketServerContainer`.

Under the hood, the call to `JettyWebSocketServerContainer.addMapping(\...)` installs the `org.eclipse.jetty.websocket.servlet.WebSocketUpgradeFilter`, which is the component that intercepts HTTP requests to upgrade to WebSocket, described in <<standard-upgrade,this section>>.
For more information about the configuration of `WebSocketUpgradeFilter` see also <<configure-filter,this section>>.

One last alternative to register your WebSocket endpoints is to use a programmatic WebSocket upgrade via `JettyWebSocketServerContainer.upgrade(\...)`, which allows you to use a standard `HttpServlet` subclass (rather than a `JettyWebSocketServlet` as explained in <<jetty-jakarta-endpoints-servlet,this section>>) to perform a direct WebSocket upgrade when your application logic demands so:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=jettyContainerServletContextHandler]
----

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=jettyContainerUpgrade]
----

When using `JettyWebSocketServerContainer.upgrade(\...)`, the `WebSocketUpgradeFilter` is not installed, since the WebSocket upgrade is performed programmatically.

[[jetty-jakarta-endpoints-servlet]]
==== Using `JettyWebSocketServlet`

An alternative way to register WebSocket endpoints using the Jetty WebSocket APIs is to use a `JettyWebSocketServlet` subclass (or even many different `JettyWebSocketServlet` subclasses).

This method has the advantage that it does not install the `WebSocketUpgradeFilter` under the hood, because the WebSocket upgrade is handled directly by your `JettyWebSocketServlet` subclass.
This may also have a performance benefit for non-WebSocket HTTP requests (as they will not pass through the `WebSocketUpgradeFilter`).

Your `JettyWebSocketServlet` subclass may be declared and configured either in code or in `web.xml`.

Declaring your `JettyWebSocketServlet` subclass explicitly in code or in `web.xml` also simplifies the declaration and configuration of other web components such as other Servlets and/or Filters (for example, it is easier to configure the `CrossOriginFilter`, see also <<configure-filter,this section>> for more information).

For example, your `JettyWebSocketServlet` subclass may be declared in code in this way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=jettyWebSocketServletMain]
----

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=jettyWebSocketServlet]
----

Note how in the call to `JettyWebSocketServletContainerInitializer.configure(\...)` the second parameter is `null`, because WebSocket endpoints are not created here, but instead by one (or more) `JettyWebSocketServlet` subclasses.
Yet the call is necessary to create other WebSocket implementation components that are used by `JettyWebSocketServlet`.

An HTTP upgrade request to WebSocket that matches your `JettyWebSocketServlet` subclass path mapping (specified above via `ServletContextHandler.addServlet(\...)`) arrives at the Servlet and is inspected to verify whether it is a valid upgrade to WebSocket.

If the HTTP request is a valid upgrade to WebSocket, `JettyWebSocketServlet` calls `configure(JettyWebSocketServletFactory factory)` that you have overridden in your subclass, so that your application can instantiate and return the WebSocket endpoint.

After having obtained the WebSocket endpoint, `JettyWebSocketServlet` performs the WebSocket upgrade.
From this point on, the communication happens with the WebSocket protocol, and HTTP components such as Filters and Servlets are not relevant anymore.

If the HTTP request is not an upgrade to WebSocket, `JettyWebSocketServlet` delegates the processing to the superclass, `javax.servlet.HttpServlet`, which in turn invokes methods such as `doGet(\...)` or `doPost(\...)` depending on the HTTP method.

If your `JettyWebSocketServlet` subclass does not override the `doXYZ(\...)` method corresponding to the HTTP request, a `405 Method Not Allowed` response is returned to the client, as per the standard `HttpServlet` class implementation.

[NOTE]
====
It is possible to use both `JettyWebSocketServerContainer` and `JettyWebSocketServlet`.

However, it is typically best to avoid mixing the use of `JettyWebSocketServerContainer` with the use of `JettyWebSocketServlet`, so that all your WebSocket endpoints are initialized by the same code in one place only.
====

Using `JettyWebSocketServerContainer.addMapping(\...)` will install the `WebSocketUpgradeFilter` under the hood, which by default will intercept all HTTP requests to upgrade to WebSocket.

However, as explained in <<standard-upgrade,this section>>, if `WebSocketUpgradeFilter` does not find a matching WebSocket endpoint for the request URI path, then the HTTP request is passed to the Filter chain of your web application and may arrive to your `JettyWebSocketServlet` subclass, where it would be processed and possibly result in a WebSocket upgrade.

[[jetty]]
== Jetty APIs Implementation using Jetty Core APIs

When you write a WebSocket application using the Jetty WebSocket APIs, your code typically needs to depend on just the Jetty WebSocket APIs to compile your application.
However, at runtime you need to have the _implementation_ of the Jetty WebSocket APIs in your class-path/module-path.

Jetty's WebSocket APIs are provided by the following Maven artifact:

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty.websocket</groupId>
  <artifactId>jetty-websocket-jetty-api</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

Jetty's implementation of the Jetty WebSocket APIs is provided by the following Maven artifact (and its transitive dependencies):

[,xml,subs=attributes+]
----
<dependency>
  <groupId>org.eclipse.jetty.websocket</groupId>
  <artifactId>jetty-websocket-jetty-server</artifactId>
  <version>{jetty-version}</version>
</dependency>
----

[NOTE]
====
The `jetty-websocket-jetty-api` artifact and the `jetty-websocket-jetty-server` artifact (and its transitive dependencies) should be present in the server class-path/module-path, and never in the web application's class-path.
====

To configure correctly your WebSocket application based on the Jetty WebSocket APIs, you need two steps:

. Make sure to <<jetty-container,set up>> an instance of `org.eclipse.jetty.websocket.server.ServerWebSocketContainer`.
. Use the `ServerWebSocketContainer` APIs in your applications to <<jetty-container-websocket-handler,register the WebSocket endpoints>> that implement your application logic.

You can read more about the xref:client/websocket.adoc#architecture[Jetty WebSocket architecture], which is common to both client-side and server-side, to get familiar with the terminology used in the following sections.

[[jetty-container]]
=== Setting up `ServerWebSocketContainer`

You need Jetty to set up a `ServerWebSocketContainer` instance to make your WebSocket applications based on the Jetty WebSocket APIs work.

Your WebSocket web application is represented by a `ContextHandler`.
The WebSocket upgrade is performed in a descendant (typically the only child) of the `ContextHandler`, either by the `org.eclipse.jetty.websocket.server.WebSocketUpgradeHandler`, or by a custom `Handler` that you write and is part of your web application.

In both cases, you need to set up a `ServerWebSocketContainer`, and this can be done <<jetty-container-websocket-handler,implicitly>> by using `WebSocketUpgradeHandler`, or <<jetty-container-websocket-container,explicitly>> by creating the `ServerWebSocketContainer` instance.

[[jetty-container-websocket-handler]]
==== Implicit setup using `WebSocketUpgradeHandler`

Using `WebSocketUpgradeHandler` is the most common way to set up your WebSocket applications.

You can use the `WebSocketUpgradeHandler` and the `ServerWebSocketContainer` APIs to map HTTP request URIs to WebSocket endpoints.

When an HTTP request arrives, `WebSocketUpgradeHandler` tests whether it is a WebSocket upgrade request, whether it matches a mapped URI, and if so upgrades the protocol to WebSocket.

From this point on, the communication on the upgraded connection happens with the WebSocket protocol.
This is very similar to what <<standard-upgrade,`WebSocketUpgradeFilter`>> does when using the Jakarta EE WebSocket APIs.

Once you have set up the `WebSocketUpgradeHandler`, you can use the `ServerWebSocketContainer` APIs to configure the WebSocket endpoints.
The example below shows how to set up the `WebSocketUpgradeHandler` and use the `ServerWebSocketContainer` APIs:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=jettyContainerWithUpgradeHandler]
----

The mapping of request URIs to WebSocket endpoints is further explained in <<jetty-pathspec,this section>>.

[[jetty-container-websocket-container]]
==== Explicit setup using `ServerWebSocketContainer`

A more advanced way to set up your WebSocket applications is to explicitly create the `ServerWebSocketContainer` instance programmatically.

This gives you more flexibility when deciding whether an HTTP request should be upgraded to WebSocket, because you do not need to match request URIs (although you can), nor you need to use `WebSocketUpgradeHandler` (although you can).

Once you have created the `ServerWebSocketContainer`, you can use its APIs to configure the WebSocket endpoints as shown in the example below.

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=jettyContainerWithContainer]
----

Note how the call to `ServerWebSocketContainer.upgrade(\...)` allows you to perform a direct WebSocket upgrade programmatically.

[[jetty-endpoints]]
=== WebSocket Endpoints

When using the Jetty WebSocket APIs, the WebSocket endpoint classes must be either annotated with the Jetty WebSocket annotations from the `org.eclipse.jetty.websocket.api.annotations` package, or implement the `org.eclipse.jetty.websocket.api.Session.Listener` interface.

In the case you want to implement the `Session.Listener` interface, remember that you have to explicitly demand to receive the next WebSocket event.
Use `Session.Listener.AutoDemanding` to automate the demand for simple use cases.

Refer to the Jetty WebSocket architecture xref:client/websocket.adoc#endpoints[section] for more information about Jetty WebSocket endpoints and how to correctly deal with the demand for WebSocket events.

There is no automatic discovery of WebSocket endpoints; all the WebSocket endpoints of your application must be returned by a `org.eclipse.jetty.websocket.server.WebSocketCreator` that is either mapped to a request URI via `ServerWebSocketContainer.addMapping(\...)`, or directly upgraded via `ServerWebSocketContainer.upgrade(\...)`.

In the call to `ServerWebSocketContainer.addMapping(\...)`, you can specify a _path spec_ (the first parameter) that can specified as discussed in <<jetty-pathspec,this section>>.

When the `Server` is started, the lambda passed to `ServerWebSocketContainer.configure(\...)`) is invoked and allows you to explicitly configure the WebSocket endpoints using the Jetty WebSocket APIs provided by `ServerWebSocketContainer`.

[[jetty-pathspec]]
==== Custom PathSpec Mappings

The `ServerWebSocketContainer.addMapping(\...)` API maps a _path spec_ to a `WebSocketCreator` instance (typically a lambda expression).
The path spec is matched against the WebSocket upgrade request URI to select the correspondent `WebSocketCreator` to invoke.

The path spec can have these forms:

* Servlet syntax, specified with `servlet|<path spec>`, where the `servlet|` prefix can be omitted if the path spec begins with `/` or `+*.+` (for example, `/ws`, `/ws/chat` or `+*.ws+`).
* Regex syntax, specified with `regex|<path spec>`, where the `regex|` prefix can be omitted if the path spec begins with `^` (for example, `+^/ws/[0-9]++`).
* URI template syntax, specified with `uri-template|<path spec>` (for example `+uri-template|/ws/chat/{room}+`).

Within the `WebSocketCreator`, it is possible to access the path spec and, for example in case of URI templates, extract additional information in the following way:

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=uriTemplatePathSpec]
----

---

### Documentation Jetty Modules Programming Guide Pages Troubleshooting Component Dump (`documentation/jetty/modules/programming-guide/pages/troubleshooting/component-dump.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

[[dump]]
= Jetty Component Dump

Jetty is based on components organized as a tree of objects (refer to xref:arch/bean.adoc[this section]), with the `Server` instance or the `HttpClient` instance typically at the root of the tree.

As explained in the xref:arch/jmx.adoc[JMX section], these components can be exported as JMX MBeans and therefore be accessible from JMX Consoles such as Java Missions Control (JMC).

Being able to take a snapshot of a Jetty component tree while it is running is the most useful information that can be attached when reporting an issue as it can contain both configuration and current state of Jetty.
A dump of the component tree for the Server includes:

* The thread pool configuration and its current state, including how many threads are in use, and their stack trace.
* The Connector configuration (including TLS configuration), state and statistics.
* The I/O configuration and its current state, including the ports Jetty listens to, how many connections are currently open, and he state of each connection, and the state of the request/response handling for each connection.
* The `Handler` structure and its configuration.
* The web applications deployed and their configurations, including the class loader information.

[[dump-start-stop]]
== Component Dump at Server Start/Stop

You can get the server dump at any time using these methods.

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ComponentDocs.java[tags=dump]
----

However, it is often useful to configure the server to dump the state of the freshly started server, and also dump the state just before the `Server` stops (which may be useful to log the state of server that is not working properly).

[,java,indent=0]
----
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ComponentDocs.java[tags=dumpStartStop]
----

[NOTE]
====
The format of the Jetty Server Dump output is subject to change at any time, as Jetty developers modify the Jetty code and decide to include more state, or remove state that is no longer relevant.

The Jetty Server Dump is organized in a tree whose structure is similar to the runtime Jetty component tree.

At the end of the dump output there is a legend that explains the type of tree node: whether it is a node that represent a _managed_ component, or an _array_ node (or a _map_ node) that represent some component state, etc.
====

[[dump-invoke]]
== Obtaining Dump via JMX

The Jetty Component Dump can be obtained by invoking, via JMX, the `Server.dump()` operation, as shown below using https://adoptium.net/jmc.html[Java Mission Control (JMC)]:

image::jmc-server-dump.png[]

Find the `Server` MBean in the MBean Tree, under `org.eclipse.jetty.server:type=server,id=0`.
Then click on the "Operations" tab, select the `dump()` operation, and then click the `Execute` button.
In the bottom panel you will see the result of the invocation, that you can copy into a text editor and save to your file system.

[CAUTION]
====
Taking a Jetty Component Dump is a relatively expensive operation, as it dumps the state of all connections (which can be thousands), and the state of all threads.

The result of the invocation may produce a large string, possibly few MiB, that may impact the server memory usage.

Furthermore, dumping the state of the I/O Jetty components takes a little CPU time off the handling of the actual I/O, possibly slowing it down temporarily.

The slow-down caused by taking the Jetty Component Dump may be noticeable on highly loaded systems.
However, it is typically a very small price to pay to obtain the information about the Jetty state that may be critical to the resolution of an issue.
====

[[dump-detailed]]
== Detailed ThreadPool Information

By default, the dump of the thread pool will only dump the topmost stack frame of each thread.
It is possible to configure the thread pool to dump the whole stack trace for each thread; while this may be a little more expensive, it provides complete information about the state of each thread, which may be important to diagnose the issue.

See the xref:operations-guide:modules/standard.adoc#threadpool[`threadpool` module] configuration for more information about how to configure the thread pool to dump detailed thread information.

Detailed thread pool information can also be turned on/off on-the-fly via JMX, by finding the `ThreadPool` MBean under `org.eclipse.jetty.util.thread:type=queuedthreadpool,id=0`, then selecting the `detailedDump` attribute and setting it to `true`. You can now perform the `Server.dump()` operation as explained above, and then set `detailedDump` back to `false`.

[[dump-example]]
== Dump Example

Below you can find a simple example of a Jetty Component Dump, with annotations for the principal components:

[jetty.small%nowrap]
....
[jetty]
setupArgs=--add-modules=http
args=jetty.http.selectors=1 jetty.http.acceptors=1 jetty.threadPool.minThreads=4 jetty.server.dumpAfterStart=true
delete=^[0-9]\{4}
callouts= <$N>,Server@,= oejut.QueuedThreadPool,ContextHandlerCollection@, oejs.ServerConnector,ManagedSelector@,keys @,startJarLoader@,unmanaged
....

ifdef::run-jetty[]
<1> The `Server` instance at the root of the tree
<2> The thread pool component
<3> The root of the `Handler` structure
<4> The connector listening on port `8080` for the HTTP/1.1 protocol
<5> A selector component that manages connections
<6> The connections currently managed by the selector component
<7> The server `ClassLoader` and its classpath
<8> The legend for the dump nodes
endif::[]

---

### Documentation Jetty Modules Programming Guide Pages Troubleshooting Debugging (`documentation/jetty/modules/programming-guide/pages/troubleshooting/debugging.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Remote Debugging

Sometimes, in order to figure out a problem, enabling xref:troubleshooting/logging.adoc[DEBUG logging] is not enough and you really need to debug the code with a debugger.

Debugging an embedded Jetty application is most easily done from your preferred IDE, so refer to your IDE instruction for how to debug Java applications.

Remote debugging can be enabled in a Jetty application via command line options:

[,screen]
----
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8000 --class-path ...
----

The example above enables remote debugging so that debuggers (for example, your preferred IDE) can connect to port `8000` on the host running the Jetty application to receive debugging events.

NOTE: More technically, remote debugging exchanges JVM Tools Interface (JVMTI) events and commands via the Java Debug Wire Protocol (JDWP).

---

### Documentation Jetty Modules Programming Guide Pages Troubleshooting Index (`documentation/jetty/modules/programming-guide/pages/troubleshooting/index.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Troubleshooting Jetty

TODO: introduction
// TODO: explain the process to troubleshoot Jetty:
// TODO: #1 enable JMX
// TODO: #2 enable GC logs
// TODO: #3 take jvm/component dumps
// TODO: #4 enable debug logging if you can

---

### Documentation Jetty Modules Programming Guide Pages Troubleshooting Logging (`documentation/jetty/modules/programming-guide/pages/troubleshooting/logging.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= Logging

The Jetty libraries (both client and server) use http://slf4j.org/[SLF4J] as logging APIs.
You can therefore plug in any SLF4J logging implementation, and configure the logging category `org.eclipse.jetty` at the desired level.

When you have problems with Jetty, the first thing that you want to do is to enable DEBUG logging.
This is helpful because by reading the DEBUG logs you get a better understanding of what is going on in the system (and that alone may give you the answers you need to fix the problem), and because Jetty developers will probably need the DEBUG logs to help you.

== Jetty SLF4J Binding

The Jetty artifact `jetty-slf4j-impl` is a SLF4J binding, that is the Jetty implementation of the SLF4J APIs, and provides a number of easy-to-use features to configure logging.

The Jetty SLF4J binding only provides an appender that writes to `System.err`.
For more advanced configurations (for example, logging to a file), use http://logback.qos.ch[LogBack], or https://logging.apache.org/log4j/2.x/[Log4j2], or your preferred SLF4J binding.

CAUTION: Only one binding can be present in the class-path or module-path. If you use the LogBack SLF4J binding or the Log4j2 SLF4J binding, remember to remove the Jetty SLF4J binding.

The Jetty SLF4J binding reads a file in the class-path (or module-path) called `jetty-logging.properties` that can be configured with the logging levels for various logger categories:

.jetty-logging.properties
[,screen]
----
# By default, log at INFO level all Jetty classes.
org.eclipse.jetty.LEVEL=INFO

# However, the Jetty client classes are logged at DEBUG level.
org.eclipse.jetty.client.LEVEL=DEBUG
----

Similarly to how you configure the `jetty-logging.properties` file, you can set the system property `org.eclipse.jetty[.<package_names>].LEVEL=DEBUG` to quickly change the logging level to DEBUG without editing any file.
The system property can be set on the command line, or in your IDE when you run your tests or your Jetty-based application and will override the `jetty-logging.properties` file configuration.
For example to enable DEBUG logging for all the Jetty classes (_very_ verbose):

[,screen]
----
java -Dorg.eclipse.jetty.LEVEL=DEBUG --class-path ...
----

If you want to enable DEBUG logging but only for the HTTP/2 classes:

[,screen]
----
java -Dorg.eclipse.jetty.http2.LEVEL=DEBUG --class-path ...
----

---

### Documentation Jetty Modules Programming Guide Pages Troubleshooting State Tracking (`documentation/jetty/modules/programming-guide/pages/troubleshooting/state-tracking.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= `StateTrackingHandler`

`StateTrackingHandler` (described xref:server/http.adoc#handler-use-state-tracking[here]) is a troubleshooting `Handler` that can be inserted in the `Handler` chain to track usages of `Handler`/`Request`/`Response` asynchronous APIs.

xref:troubleshooting/component-dump.adoc[Dumping the Jetty component tree] will dump the `StateTrackingHandler`, which will dump the state of the current requests.

This will help detecting whether requests are not completed due to callbacks not being completed, or whether callback code is stuck while invoking blocking APIs, etc.

Thread stack traces (including virtual threads) of operations that have been started but not completed, or are stuck in blocking code are provided in the component tree dump.

---

### Documentation Jetty Modules Programming Guide Pages Troubleshooting Thread Dump (`documentation/jetty/modules/programming-guide/pages/troubleshooting/thread-dump.adoc`)

//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

= JVM Thread Dump

TODO

---

--- METRICS ---
- Files Extracted: 75
- Estimated Token Budget: ~285839 tokens
- Canonical Reference: https://codewiki.google/github.com/jetty/jetty.project
