I would like to obtain a unique identifier for every connection established to Tomcat. I am not talking about sessions or uniqueness of users (clients) but every connection. Say, client A sends one GET, then another. In my scenario these are two separate and unique connections.
Is there any variable or something that can play identifier role for such a connection in Tomcat ?
One option is to use a ServletFilter:
public class UniqueRequestFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
String requestID = UUID.randomUUID().toString()
//save to ThreadLocal...
try {
chain.doFilter(req, res);
}
finally {
//remove from ThreadLocal
}
}
public void init(FilterConfig config) throws ServletException {
}
public void destroy() {
}
}
You will be able to get the request value from the ThreadLocal at any point in your application.
Jakarta Servlet 6.0 (c. 2022) adds a new method to the ServletRequest interface: getRequestId.
While you may have had to write your own request-id-generator in the past, you should now be able to rely on a container-agnostic feature in your web applications.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With