I learned when a request comes in from a client, the Tomcat generates a new request instance for each request.
So I print out HttpServletRequest's hashcode but it was always same
Here is my code,
package hello.servlet.basic.request;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Slf4j
@WebServlet(name = "tomcatCreateNewRequestInstance", value = "/check-request-address")
public class TomcatCreateNewRequestInstance extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
log.info("req = {}", req);
log.info("req class = {}", req.getClass());
}
}
But same hashcode like this:
2024-05-03 15:40:28.725 INFO 14920 --- [nio-9090-exec-6] h.s.b.r.TomcatCreateNewRequestInstance : req = org.apache.catalina.connector.RequestFacade@1f8212ca
2024-05-03 15:40:28.725 INFO 14920 --- [nio-9090-exec-6] h.s.b.r.TomcatCreateNewRequestInstance : req class = class org.apache.catalina.connector.RequestFacade
2024-05-03 15:40:37.232 INFO 14920 --- [nio-9090-exec-4] h.s.b.r.TomcatCreateNewRequestInstance : req = org.apache.catalina.connector.RequestFacade@1f8212ca
2024-05-03 15:40:37.232 INFO 14920 --- [nio-9090-exec-4] h.s.b.r.TomcatCreateNewRequestInstance : req class = class org.apache.catalina.connector.RequestFacade
2024-05-03 15:40:37.370 INFO 14920 --- [nio-9090-exec-7] h.s.b.r.TomcatCreateNewRequestInstance : req = org.apache.catalina.connector.RequestFacade@1f8212ca
2024-05-03 15:40:37.370 INFO 14920 --- [nio-9090-exec-7] h.s.b.r.TomcatCreateNewRequestInstance : req class = class org.apache.catalina.connector.RequestFacade
2024-05-03 15:40:42.507 INFO 14920 --- [nio-9090-exec-9] h.s.b.r.TomcatCreateNewRequestInstance : req = org.apache.catalina.connector.RequestFacade@1f8212ca
2024-05-03 15:40:42.507 INFO 14920 --- [nio-9090-exec-9] h.s.b.r.TomcatCreateNewRequestInstance : req class = class org.apache.catalina.connector.RequestFacade
2024-05-03 15:40:42.695 INFO 14920 --- [nio-9090-exec-8] h.s.b.r.TomcatCreateNewRequestInstance : req = org.apache.catalina.connector.RequestFacade@1f8212ca
2024-05-03 15:40:42.695 INFO 14920 --- [nio-9090-exec-8] h.s.b.r.TomcatCreateNewRequestInstance : req class = class org.apache.catalina.connector.RequestFacade
Did I get it wrong? Or is Tomcat performing a series of optimisations? If so where could I find related reference?
Those objects are "recycled" - but each request is served with the proper data.
There's nothing to see here, and you're not supposed to hold a request object for longer than the request is handled.
You might find a reference in the servlet spec: How the container (tomcat) handles requests is up to it. It owns the lifecycle of those objects and is free to invalidate them after a request is handled - even if the object was not recycled, you'd not be guaranteed for them to have the correct state that they used to have while the request was handled.
If this makes a difference to your program, then you're doing it wrong. You must not care about the request's identity - just act according to its state and don't hold on to a request or response object for any longer than the request is handled.