or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

http-client.mdindex.mdreactive-web-server.mdserver-configuration.mdservlet-web-server.mdwebsocket-support.md

servlet-web-server.mddocs/

0

# Servlet Web Server

1

2

Core servlet container functionality using Jetty as the embedded server. Provides complete servlet container capabilities with Spring Boot integration, supporting traditional servlet-based web applications with blocking I/O.

3

4

## Capabilities

5

6

### JettyServletWebServerFactory

7

8

Creates and configures Jetty servlet web server instances with comprehensive customization options.

9

10

```java { .api }

11

/**

12

* Factory for creating JettyWebServer instances configured for servlet applications

13

*/

14

public class JettyServletWebServerFactory

15

extends AbstractServletWebServerFactory

16

implements ConfigurableJettyWebServerFactory, ResourceLoaderAware {

17

18

/** Create factory with default configuration */

19

public JettyServletWebServerFactory();

20

21

/** Create factory with specified port */

22

public JettyServletWebServerFactory(int port);

23

24

/** Create factory with context path and port */

25

public JettyServletWebServerFactory(String contextPath, int port);

26

27

/** Create web server with servlet context initializers */

28

public WebServer getWebServer(ServletContextInitializer... initializers);

29

30

/** Add server customizers for additional configuration */

31

public void addServerCustomizers(JettyServerCustomizer... customizers);

32

33

/** Get collection of configured server customizers */

34

public Collection<JettyServerCustomizer> getServerCustomizers();

35

36

/** Set number of acceptor threads */

37

public void setAcceptors(int acceptors);

38

39

/** Set number of selector threads */

40

public void setSelectors(int selectors);

41

42

/** Configure forward headers handling */

43

public void setUseForwardHeaders(boolean useForwardHeaders);

44

45

/** Set thread pool configuration */

46

public void setThreadPool(ThreadPool threadPool);

47

48

/** Get the configured thread pool */

49

public ThreadPool getThreadPool();

50

51

/** Set maximum number of connections */

52

public void setMaxConnections(int maxConnections);

53

54

/** Set resource loader for loading resources */

55

public void setResourceLoader(ResourceLoader resourceLoader);

56

57

/** Set collection of server customizers */

58

public void setServerCustomizers(Collection<? extends JettyServerCustomizer> customizers);

59

60

/** Add Jetty configurations */

61

public void addConfigurations(Configuration... configurations);

62

63

/** Set Jetty configurations */

64

public void setConfigurations(Collection<? extends Configuration> configurations);

65

66

/** Get configured Jetty configurations */

67

public Collection<Configuration> getConfigurations();

68

}

69

```

70

71

**Usage Examples:**

72

73

```java

74

@Configuration

75

public class JettyServletConfig {

76

77

@Bean

78

public JettyServletWebServerFactory jettyServletWebServerFactory() {

79

JettyServletWebServerFactory factory = new JettyServletWebServerFactory(8080);

80

81

// Configure acceptor and selector threads

82

factory.setAcceptors(2);

83

factory.setSelectors(4);

84

85

// Enable forward headers

86

factory.setUseForwardHeaders(true);

87

88

// Add custom server configuration

89

factory.addServerCustomizers(server -> {

90

// Configure server connectors

91

ServerConnector connector = new ServerConnector(server);

92

connector.setPort(8080);

93

connector.setIdleTimeout(30000);

94

server.addConnector(connector);

95

});

96

97

return factory;

98

}

99

}

100

```

101

102

### JettyWebServer

103

104

The actual web server implementation that manages the Jetty server lifecycle.

105

106

```java { .api }

107

/**

108

* Jetty-based implementation of WebServer interface

109

*/

110

public class JettyWebServer implements WebServer {

111

112

/** Create Jetty web server with given server instance */

113

public JettyWebServer(Server server);

114

115

/** Create Jetty web server with server and auto-start option */

116

public JettyWebServer(Server server, boolean autoStart);

117

118

/** Start the Jetty server */

119

public void start() throws WebServerException;

120

121

/** Stop the Jetty server gracefully */

122

public void stop() throws WebServerException;

123

124

/** Get the port the server is running on */

125

public int getPort();

126

127

/** Get the underlying Jetty Server instance */

128

public Server getServer();

129

130

/** Shutdown the server gracefully with timeout */

131

public void shutDownGracefully(GracefulShutdownCallback callback);

132

133

/** Destroy the server */

134

public void destroy();

135

}

136

```

137

138

### Servlet Context Configuration

139

140

Configuration classes for servlet-specific Jetty setup.

141

142

```java { .api }

143

/**

144

* Jetty-specific embedded web application context

145

*/

146

public class JettyEmbeddedWebAppContext extends WebAppContext {

147

148

/** Create context with default configuration */

149

public JettyEmbeddedWebAppContext();

150

151

/** Create context with web app and context path */

152

public JettyEmbeddedWebAppContext(String webApp, String contextPath);

153

154

/** Set temporary directory for web app */

155

public void setTempDirectory(File tempDirectory);

156

157

/** Configure class loading behavior */

158

public void setParentLoaderPriority(boolean parentLoaderPriority);

159

160

/** Set session timeout in minutes */

161

public void setSessionTimeout(int sessionTimeout);

162

}

163

164

/**

165

* Configuration for servlet context initializers

166

*/

167

public class ServletContextInitializerConfiguration {

168

169

/** Apply initializers to servlet context */

170

public static void apply(ServletContextInitializer[] initializers,

171

ServletContext servletContext);

172

}

173

```

174

175

### Error Handling

176

177

Jetty-specific error handling configuration.

178

179

```java { .api }

180

/**

181

* Custom error handler for Jetty embedded applications

182

*/

183

public class JettyEmbeddedErrorHandler extends ErrorHandler {

184

185

/** Create error handler with default configuration */

186

public JettyEmbeddedErrorHandler();

187

188

/** Handle errors and generate appropriate responses */

189

public void handle(String target, Request baseRequest,

190

HttpServletRequest request, HttpServletResponse response);

191

192

/** Set whether to show server information in error pages */

193

public void setShowServlet(boolean showServlet);

194

195

/** Set whether to show stack traces in error pages */

196

public void setShowStacks(boolean showStacks);

197

}

198

```

199

200

**Configuration Examples:**

201

202

```java

203

@Configuration

204

public class JettyErrorConfig {

205

206

@Bean

207

public JettyServletWebServerFactory jettyFactory() {

208

JettyServletWebServerFactory factory = new JettyServletWebServerFactory();

209

210

factory.addServerCustomizers(server -> {

211

// Configure custom error handler

212

JettyEmbeddedErrorHandler errorHandler = new JettyEmbeddedErrorHandler();

213

errorHandler.setShowStacks(false);

214

errorHandler.setShowServlet(false);

215

216

// Apply to all contexts

217

Handler.Collection handlers = new Handler.Collection();

218

handlers.setHandlers(new Handler[]{

219

new DefaultHandler(),

220

errorHandler

221

});

222

server.setHandler(handlers);

223

});

224

225

return factory;

226

}

227

}

228

```

229

230

### Graceful Shutdown

231

232

Support for graceful server shutdown with connection draining.

233

234

```java { .api }

235

/**

236

* Handles graceful shutdown of Jetty server

237

*/

238

public class GracefulShutdown implements TomcatConnectorCustomizer {

239

240

/** Create graceful shutdown handler */

241

public GracefulShutdown();

242

243

/** Initiate graceful shutdown with callback */

244

public void shutDownGracefully(GracefulShutdownCallback callback);

245

246

/** Set shutdown timeout period */

247

public void setTimeout(Duration timeout);

248

}

249

```