title: Auto-versioning of static content with JBoss
body: <p>As per the Q&amp;A <a href="https://stackoverflow.com/questions/118884/what-is-an-elegant-way-to-force-browsers-to-reload-cached-css-js-files">here</a>, I'd like to implement a similar auto-versioning system for a web app running in JBoss 5. Is there anything already out there to do this sort of thing, or will I need to write something myself? To be clear: <strong>I am not using PHP.</strong></p>
  
  <p>Not knowing much about PHP, I'm not sure what the Tomcat/JBoss analogs of PHP's <code>.htaccess</code>, etc. are. If I do have to write my own auto-versioning, where would I start? The principle is clear to me - rewriting the URL using the file's timestamp, but I don't know much about URL rewriting with JBoss/Tomcat.</p>
  
  <hr>
  
  <h2>Update:</h2>
  
  <p>Combining the approaches recommended by <a href="https://stackoverflow.com/questions/2630885/auto-versioning-of-static-content-with-jboss/2667786#2667786">Pascal</a> and <a href="https://stackoverflow.com/questions/2630885/auto-versioning-of-static-content-with-jboss/3719518#3719518">novice</a>, here's what I ended up with:</p>
  
  <p><strong>1.</strong> Custom <code>&lt;my:script/&gt;</code> and <code>&lt;my:style/&gt;</code> tags, so I wouldn't have to see <code>&lt;c:url/&gt;</code> tags everywhere.</p>
  
  <pre><code>&lt;%@ tag body-content="empty" %&gt;
  &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;
  &lt;%@ attribute name="src" required="true" rtexprvalue="true" %&gt;
  &lt;script src="&lt;c:url value="${src}" /&gt;"&gt;&lt;/script&gt;
  </code></pre>
  
  <p><strong>2.</strong> Following fairly closely to novice's steps, but mapping <code>UrlRewriteFilter</code> to <code>/*</code> in web.xml:</p>
  
  <pre><code>&lt;filter-mapping&gt;
      &lt;filter-name&gt;UrlRewriteFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  </code></pre>
  
  <p><strong>3.</strong> Injecting a CACHE_BUST variable to every new session (more or less...), an application deploy  <code></code> <code></code> <code></code>timestamp:</p>
  
  <pre><code>// On application deploy:
  long CACHE_BUST = System.currentTimeMillis() / 1000;
  
  // later...
  session.setAttribute("cacheBust", CACHE_BUST);
  </code></pre>
  
  <p><strong>4.</strong> ...so that I can use these rules in <code>urlrewrite.xml</code>:</p>
  
  <pre><code>&lt;outbound-rule&gt;
      &lt;from&gt;^/static/(css|js|images)/(.*)$&lt;/from&gt;
      &lt;to&gt;%{context-path}/static/%{session-attribute:cacheBust}/$1/$2&lt;/to&gt;
  &lt;/outbound-rule&gt;
  
  &lt;rule&gt;
      &lt;from&gt;^/static/\d{10}/(css|js|images)/(.*)$&lt;/from&gt;
      &lt;to&gt;/static/$1/$2&lt;/to&gt;
  &lt;/rule&gt;
  </code></pre>
  
  <hr>
  
  <p>Many thanks to <a href="https://stackoverflow.com/users/70604/pascal-thivent">Pascal</a> and <a href="https://stackoverflow.com/users/136913/novice">novice</a> for their help.</p>