Skip to content

Return value from getLastModified is not truncated to seconds #884

@rodcheater

Description

@rodcheater

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:

  1. Run curl -v http://localhost:8080/test.
  2. Observe that the response includes the header Last-Modified: Tue, 24 Jun 2025 06:33:32 GMT.
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions