Core server component of Eclipse Jetty web server providing HTTP server functionality, request handling, and connection management
—
The core server APIs provide the foundation for creating and managing HTTP servers, including server lifecycle, connector management, and basic configuration.
The Server class is the central component that coordinates all server functionality.
public class Server extends Handler.Wrapper implements Attributes {
// Constructors
public Server();
public Server(int port);
public Server(InetSocketAddress addr);
public Server(ThreadPool threadPool);
public Server(ThreadPool threadPool, Scheduler scheduler, ByteBufferPool bufferPool);
// Version and identification
public static String getVersion();
public URI getURI();
public Context getContext();
// Lifecycle management
public void start() throws Exception;
public void stop() throws Exception;
public void join() throws InterruptedException;
public boolean getStopAtShutdown();
public void setStopAtShutdown(boolean stop);
public long getStopTimeout();
public void setStopTimeout(long stopTimeout);
// Component access
public ThreadPool getThreadPool();
public Scheduler getScheduler();
public ByteBufferPool getByteBufferPool();
// Connector management
public Connector[] getConnectors();
public void addConnector(Connector connector);
public void removeConnector(Connector connector);
public void setConnectors(Connector[] connectors);
// Handler management
public Handler getHandler();
public void setHandler(Handler handler);
public Handler getDefaultHandler();
public void setDefaultHandler(Handler defaultHandler);
public Request.Handler getErrorHandler();
public void setErrorHandler(Request.Handler errorHandler);
// Request logging
public RequestLog getRequestLog();
public void setRequestLog(RequestLog requestLog);
// Configuration
public File getTempDirectory();
public void setTempDirectory(File temp);
public MimeTypes.Mutable getMimeTypes();
}// Create server with default configuration
Server server = new Server();
// Create server with specific port
Server server = new Server(8080);
// Create server with address binding
InetSocketAddress addr = new InetSocketAddress("localhost", 8080);
Server server = new Server(addr);
// Create server with custom thread pool
QueuedThreadPool threadPool = new QueuedThreadPool(200);
Server server = new Server(threadPool);Server server = new Server(8080);
// Configure handlers and connectors before starting
server.setHandler(myHandler);
try {
// Start the server
server.start();
System.out.println("Server started on: " + server.getURI());
// Wait for server to finish (blocks until stopped)
server.join();
} catch (Exception e) {
System.err.println("Server failed to start: " + e.getMessage());
} finally {
// Ensure cleanup
server.stop();
}Server server = new Server(8080);
// Configure shutdown behavior
server.setStopAtShutdown(true); // Stop server on JVM shutdown
server.setStopTimeout(30000); // 30 second timeout for graceful stop
// Add shutdown hook for clean shutdown
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
server.stop();
} catch (Exception e) {
System.err.println("Error during shutdown: " + e.getMessage());
}
}));The ServerConnector is the default implementation for HTTP network connections.
public class ServerConnector extends AbstractNetworkConnector {
// Constructors
public ServerConnector(Server server);
public ServerConnector(Server server, ConnectionFactory... factories);
public ServerConnector(Server server, Executor executor, Scheduler scheduler,
ByteBufferPool bufferPool, int acceptors, int selectors,
ConnectionFactory... factories);
// Network configuration
public void setHost(String host);
public String getHost();
public void setPort(int port);
public int getPort();
public int getLocalPort();
// Connection handling
public int getAcceptors();
public void setAcceptors(int acceptors);
public SelectorManager getSelectorManager();
// Socket configuration
public void setReuseAddress(boolean reuseAddress);
public boolean getReuseAddress();
public void setInheritChannel(boolean inheritChannel);
public boolean isInheritChannel();
public void setAcceptedTcpNoDelay(boolean tcpNoDelay);
public boolean getAcceptedTcpNoDelay();
public void setAcceptedReceiveBufferSize(int size);
public int getAcceptedReceiveBufferSize();
public void setAcceptedSendBufferSize(int size);
public int getAcceptedSendBufferSize();
}Base interface for all server connectors.
public interface Connector extends LifeCycle, Container, Graceful {
// Server association
Server getServer();
void setServer(Server server);
// Core components
Executor getExecutor();
Scheduler getScheduler();
ByteBufferPool getByteBufferPool();
// Protocol factories
ConnectionFactory getConnectionFactory(String protocol);
ConnectionFactory getConnectionFactory(Class<? extends Connection> factoryType);
Collection<ConnectionFactory> getConnectionFactories();
void addConnectionFactory(ConnectionFactory factory);
void removeConnectionFactory(String protocol);
void setDefaultProtocol(String protocol);
String getDefaultProtocol();
List<String> getProtocols();
// Statistics and monitoring
long getBytesIn();
long getBytesOut();
long getConnections();
long getMessagesIn();
long getMessagesOut();
long getConnectionsOpen();
long getConnectionsOpenMax();
Duration getConnectionsOpenMax();
}Server server = new Server();
// Create HTTP connector
ServerConnector httpConnector = new ServerConnector(server);
httpConnector.setHost("localhost");
httpConnector.setPort(8080);
httpConnector.setAcceptors(2); // Number of acceptor threads
// Create HTTPS connector
HttpConfiguration httpsConfig = new HttpConfiguration();
httpsConfig.addCustomizer(new SecureRequestCustomizer());
SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
sslContextFactory.setKeyStorePath("keystore.jks");
sslContextFactory.setKeyStorePassword("password");
ServerConnector httpsConnector = new ServerConnector(server,
new SslConnectionFactory(sslContextFactory, "http/1.1"),
new HttpConnectionFactory(httpsConfig));
httpsConnector.setPort(8443);
// Add connectors to server
server.setConnectors(new Connector[]{httpConnector, httpsConnector});Server server = new Server();
HttpConfiguration config = new HttpConfiguration();
// HTTP/1.1 and HTTP/2 connector
ServerConnector connector = new ServerConnector(server,
new HttpConnectionFactory(config),
new HTTP2ServerConnectionFactory(config));
connector.setPort(8080);
server.addConnector(connector);Base class for network-based connectors.
public abstract class AbstractNetworkConnector extends AbstractConnector implements NetworkConnector {
// Network interface configuration
public void setHost(String host);
public String getHost();
public void setPort(int port);
public int getPort();
public int getLocalPort();
// Lifecycle
public void open() throws IOException;
public void close() throws IOException;
public boolean isOpen();
}public interface NetworkConnector extends Connector, Closeable {
void open() throws IOException;
void close() throws IOException;
boolean isOpen();
int getLocalPort();
String getHost();
int getPort();
void setHost(String host);
void setPort(int port);
}In-memory connector for testing and embedded usage.
public class LocalConnector extends AbstractConnector {
public LocalConnector(Server server);
public LocalConnector(Server server, ConnectionFactory... factories);
// Request execution
public LocalEndPoint executeRequest(String rawRequest);
public String getResponse(String rawRequest);
public String getResponse(String rawRequest, boolean head, long time, TimeUnit unit);
}// Create server with local connector for testing
Server server = new Server();
LocalConnector localConnector = new LocalConnector(server);
server.addConnector(localConnector);
server.setHandler(new Handler.Abstract() {
@Override
public boolean handle(Request request, Response response, Callback callback) throws Exception {
response.setStatus(200);
response.getHeaders().put("Content-Type", "text/plain");
response.write(true, "Hello Test!".getBytes(), callback);
return true;
}
});
server.start();
// Execute request directly
String response = localConnector.getResponse("GET / HTTP/1.1\r\nHost: localhost\r\n\r\n");
System.out.println("Response: " + response);Provides access to core server components.
public interface Components {
ByteBufferPool getByteBufferPool();
Scheduler getScheduler();
Executor getExecutor();
InvocationType getInvocationType();
}Install with Tessl CLI
npx tessl i tessl/maven-org-eclipse-jetty--jetty-server