question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suppport setting own ServletContextHandler on embedded Server

See original GitHub issue

I have some existing servlets that I want to use in combination with Javalin. When I set the server’s handler to use the servletContextHandler it seems to complete overwrite any Javalin endpoints. Is it possible to do this?

embeddedServer(new EmbeddedJettyFactory(() -> {
    Server server = new Server();
    ServletContextHandler servletContextHandler =
		new ServletContextHandler(ServletContextHandler.SESSIONS);
    servletContextHandler.setContextPath("/someServletContextPath");
    servletContextHandler.addServlet(someServlet.class, "path");
    server.setHandler(servletContextHandler);
    return server;
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
havenotfearcommented, Jun 24, 2018

I have not started on it yet. On the back burner probably for another week or so. I do plan on working on it.

1reaction
tipsycommented, Jun 18, 2018

Let’s see. The handlers are added like this:

server.apply {
    handler = attachHandlersToTail(server.handler, HandlerList(httpHandler, webSocketHandler, notFoundHandler))
    connectors = connectors.takeIf { it.isNotEmpty() } ?: arrayOf(ServerConnector(server).apply {
        this.port = port
    })
}.start()

I wrote a tutorial a while back for custom handlers: https://javalin.io/tutorials/prometheus-example

.embeddedServer(new EmbeddedJettyFactory(() -> {
    Server server = new Server(queuedThreadPool);
    server.setHandler(statisticsHandler);
    return server;
}))

But that looks a lot like what you’re doing. I’m guessing your ServletContextHandler takes it upon itself to handle 404s? Ideally the request should just move to the next handler in the list, but I’m not all that familiar with how ServletContextHandler behaves in a HandlerList.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Embedded jetty and guice Handler servlet upon request ...
AContext.getHandler(server) returns the ServletContextHandler defined in the first. isn't it enough? The HOST is a String formatted like a.test.
Read more >
Setup Swagger with an embedded Jetty Server - gists · GitHub
Update your ServletContextHandler's resource base path to point to the static resources. Finally, add a DefaultServlet to serve these static resources. This ...
Read more >
73. Embedded servlet containers - Spring
There are two ways to add Servlet , Filter , ServletContextListener and the other listeners supported by the Servlet spec to your application....
Read more >
Jetty11 Programming Guide | The Eclipse Foundation
The Eclipse 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, ...
Read more >
Application servers and environments supported by Weld
No further configuration is needed when starting Jetty as an embedded webapp server from within another Java program. However, if you're using a ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found