Home>

In order to do cache busting of static resources, I wrote the following in jsp, but it does not work.

<script>
    function createTime () {
        return new Date (). getTime ();
    }
</script>
<link rel = "stylesheet" href = "css/test.css? date = createTime ()" type = "text/css">
<link rel = "stylesheet" href = "css/test.css? date = javascript: createTime ()" type = "text/css">
<link rel = "stylesheet" href = "css/test.css? date =<% = createTime ()%>" type = "text/css">

How can I give the return value of createTime ()?

  • Answer # 1

    createTime ()should be implemented inJava.

    JSP should bedynamically generating HTML to respond withembedding various values ​​in server-sideJava.
    The processing instruction part (<%to%>) is the part embedded in Java.
    (By the way, the script element is javascript executed by the browser that displayed the page.)

    " type = "text/css">

    Addition)

      

    How can I give the return value of createTime ()?

    Cashbusting with JavaScript is limited to dynamic resources.

    When requesting and receiving a response with Ajax

    When receiving resources specified by src attribute etc. when deploying DOM dynamically


      

    Static resources

    Reconfirm client/server system basics:

    [Server] [Client (Browser)]
        | (1)
        |<== req (html) = |
       (2) |
        | = res (html) ==>|
        | (3)
        |<== req (css) == |
        |<== req (js) === |
        |<== req (img) == |
       (4) |
        | == res (css) ==>|
        | === res (js) ==>|
        | == res (img) ==>|
        | (5)
        | |

    The browser requests an HTML page

    The server receives a request from the browser and generatesHTML from JSPresponse.

    Browser also requests other resources needed for HTML

    The server conforms to 3.

    The received 4. resource is cached.
    Even after busting, it basically remains in the browser cache.
    (The URLquery character is different, so request againmechanism)

  • Answer # 2

    I get the source every time I load the screen.

    <% @ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core"%>