Connect Servlet applications to Bugsink
When a Servlet request fails, Bugsink can show the original exception, the application frame that raised it, and the HTTP request which reached the servlet.
This guide adds Bugsink to an existing Jakarta Servlet application. You’ll initialize the SDK, retain request details through the Servlet lifecycle, and report an exception from the application’s error page before triggering one failed checkout request.
To follow along, create a Bugsink project and copy its DSN. The quickstart guide shows where to find it.
Install the Servlet integration
Add the open-source Jakarta Servlet integration to your Maven project. Bugsink accepts the Sentry event protocol, so the SDK can send events directly to your Bugsink project:
<dependency> <groupId>io.sentry</groupId> <artifactId>sentry-servlet-jakarta</artifactId> <version>${sentry.version}</version> </dependency><dependency> <groupId>io.sentry</groupId> <artifactId>sentry-servlet-jakarta</artifactId> <version>${sentry.version}</version> </dependency>
Initialize the SDK
Initialize the SDK once when the application starts. Reading the DSN from the
environment keeps it out of source control, while addInAppInclude marks your
own package as application code in the stack trace:
Sentry.init(options -> { options.setDsn(System.getenv("SENTRY_DSN")); options.addInAppInclude("com.example.inventory"); });Sentry.init(options -> { options.setDsn(System.getenv("SENTRY_DSN")); options.addInAppInclude("com.example.inventory"); });
Set the DSN in the environment where the Servlet container runs:
export SENTRY_DSN=https://key@bugsink.example/1export SENTRY_DSN=https://key@bugsink.example/1
The integration normally registers its request listener through the Servlet container initializer included in the dependency. This embedded Jetty application creates its context in code, so it registers the same listener directly:
context.addEventListener(new SentryServletRequestListener());context.addEventListener(new SentryServletRequestListener());
The listener keeps each request in its own SDK scope. Events captured before that request ends receive its URL, method, safe headers, and an HTTP breadcrumb.
Report failures from the error page
The Servlet integration adds request details to an event; it does not decide which application failures should become events. Capture the exception in the error page your container already uses for failures which escape a servlet:
package com.example.inventory; import io.sentry.Sentry; import io.sentry.protocol.SentryId; import jakarta.servlet.RequestDispatcher; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; public final class ErrorReportingServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { Throwable failure = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION); SentryId eventId = failure == null ? SentryId.EMPTY_ID : Sentry.captureException( failure, scope -> scope.setTransaction("GET /checkout/{sku}")); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.setContentType("application/json"); response.getWriter().printf( "{\"error\":\"request failed\",\"eventId\":\"%s\"}%n", eventId); } }package com.example.inventory; import io.sentry.Sentry; import io.sentry.protocol.SentryId; import jakarta.servlet.RequestDispatcher; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; public final class ErrorReportingServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { Throwable failure = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION); SentryId eventId = failure == null ? SentryId.EMPTY_ID : Sentry.captureException( failure, scope -> scope.setTransaction("GET /checkout/{sku}")); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.setContentType("application/json"); response.getWriter().printf( "{\"error\":\"request failed\",\"eventId\":\"%s\"}%n", eventId); } }
The scope callback gives the event a stable route name instead of grouping the concrete product URL as a different transaction. The returned event ID can be included in your normal 500 response or server log when that helps support staff find the corresponding Bugsink event.
Map escaped exceptions to that error servlet using your container’s usual error-page configuration. In embedded Jetty, the equivalent setup is:
context.addServlet(new ServletHolder(new ErrorReportingServlet()), "/error"); ErrorPageErrorHandler errorPages = new ErrorPageErrorHandler(); errorPages.addErrorPage(Throwable.class, "/error"); context.setErrorHandler(errorPages);context.addServlet(new ServletHolder(new ErrorReportingServlet()), "/error"); ErrorPageErrorHandler errorPages = new ErrorPageErrorHandler(); errorPages.addErrorPage(Throwable.class, "/error"); context.setErrorHandler(errorPages);
Send a test error
Temporarily let one servlet request raise an exception. There is no reporting call in the servlet itself; the exception leaves the application handler and reaches the configured error page:
String sku = request.getPathInfo().substring(1); if (!"coffee-beans".equals(sku)) { throw new ProductNotFound("Product " + sku + " is not in inventory"); }String sku = request.getPathInfo().substring(1); if (!"coffee-beans".equals(sku)) { throw new ProductNotFound("Product " + sku + " is not in inventory"); }
Build and start the application, then request the failing checkout route:
mvn package java -jar target/inventory-servlet-1.0.0.jar :::bash curl http://127.0.0.1:8080/checkout/servlet-missingmvn package java -jar target/inventory-servlet-1.0.0.jar :::bash curl http://127.0.0.1:8080/checkout/servlet-missing
The request still follows the application’s normal error path and returns HTTP 500. The error page also sends the original exception while the Servlet request scope is active.
See the error in Bugsink
Open your Bugsink project and select the new ProductNotFound issue. Its event
shows the failing application frame and concrete request, while the transaction
uses the parameterized checkout route.
ProductNotFound
That’s it: failures dispatched to your Servlet error page will now reach Bugsink with their request details. Remove the deliberate missing-product request; the listener and error page remain active for future failures.
For handled exceptions, messages, logging integrations, tags, structured context, and breadcrumbs, continue with the Java guide.
