Consider this servlet:
package org.acme
import jakarta.servlet.annotation.WebServlet
import jakarta.servlet.http.HttpServlet
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
@WebServlet(urlPatterns = ["/test"])
class TestServlet : HttpServlet() {
override fun doGet(req: HttpServletRequest, resp: HttpServletResponse) {
resp.contentType = "text/plain"
resp.writer.write("Hello")
}
override fun getLastModified(req: HttpServletRequest): Long {
return 1750746812123
}
}
Run up an application with this servlet then follow these steps:
- Run
curl -v http://localhost:8080/test.
- Observe that the response includes the header
Last-Modified: Tue, 24 Jun 2025 06:33:32 GMT.
- Run
curl -v http://localhost:8080/test -H 'If-Modified-Since: Tue, 24 Jun 2025 06:33:32 GMT'.
Expected result:
A 304 not modified response is returned.
Actual result:
A 200 response is returned.
If you then modify the servlet to return 1750746812000 from getLastModified then the last curl command returns the 304 as expected.
Compare your implementation to Tomcat’s for example; they truncate the value returned by getLastModified to the second before comparing it with the If-Modified-Since timestamp because that’s the precision of HTTP dates.
Consider this servlet:
Run up an application with this servlet then follow these steps:
curl -v http://localhost:8080/test.Last-Modified: Tue, 24 Jun 2025 06:33:32 GMT.curl -v http://localhost:8080/test -H 'If-Modified-Since: Tue, 24 Jun 2025 06:33:32 GMT'.Expected result:
A 304 not modified response is returned.
Actual result:
A 200 response is returned.
If you then modify the servlet to return
1750746812000fromgetLastModifiedthen the last curl command returns the 304 as expected.Compare your implementation to Tomcat’s for example; they truncate the value returned by
getLastModifiedto the second before comparing it with theIf-Modified-Sincetimestamp because that’s the precision of HTTP dates.