title: Setting Expires cache in webapplication for JBoss application server not working right
body: <p>I have set up a cache-filter for my web application in JBoss Application Server 7.1.1 </p>
  
  <p>by using the setHeader and setDateHeader functions of the response.</p>
  
  <p>The Expires date is always set to 1 month in the future since
  the server has been restarted - The month is also depending on the current month:
  So if the server has been restarted on e.g.:</p>
  
  <p>Wed, 21 Mar 2018 10:34 GMT 
  i will set the expire date to:</p>
  
  <p>Wed, 21 <strong>Apr</strong> 2018 10:34 GMT or depending on the current date:</p>
  
  <p>But in the Google-Chrome-Tools i will always see a wrong Expires-Date:</p>
  
  <p><strong>e.g. theme.css :</strong></p>
  
  <ul>
  <li>Status        : 200</li>
  <li>Type          : stylesheet</li>
  <li>Cache-Control : public, must-revalidate <strong><em>(This is as i set in my filter)</em></strong></li>
  <li>Expires       : Wed, 28 Mar 2018 12:04:30 GMT <strong><em>(This is wrong)</em></strong></li>
  </ul>
  
  <p>instead of using my Expires-Date which i have sent:</p>
  
  <ul>
  <li>Expires       : Wed, 21 Apr 2018 10:34 GMT </li>
  </ul>
  
  <p>I don't know where the problem is and i am using currently this functions in my filter:</p>
  
  <pre><code>@Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
          throws IOException, ServletException
  {
      // Get request datas:
      HttpServletResponse httpResponse = (HttpServletResponse)response;
      HttpServletRequest reqt = (HttpServletRequest) request;
      // Ignore ajax-request's:
      String reqURI       = reqt.getRequestURI().toLowerCase().trim();
      String reqURIContext    = reqt.getContextPath().toLowerCase()+ '/';
      String reqURIPart = reqURI.substring(reqURIContext.trim().length()).toLowerCase().trim();
      String CACHE_MEDIA  =       ";.js;.css;.svg;.png;.jpg;.gif;.pdf;.ico;.woff2;.woff;.ttf;.otf;.eot;"
                                                  +ResourceHandler.RESOURCE_IDENTIFIER+";";
  
              Boolean useCache = false;
              // Set Browser cache for all resources including all media files :
              String[] medias2cache = CACHE_MEDIA.split(";");
              for (int idx=1;idx&lt;medias2cache.length;idx++)
              {
                  if (reqURI.contains(medias2cache[idx]))
                  {
                      useCache = true;
                      break;
                  }
              }
              // Set browser-cache:
              if (useCache)
              {
                  httpResponse.setHeader("Cache-Control", "public, must-revalidate");
                  httpResponse.setDateHeader("Expires", InitBean.getStartupDateTime4CacheAsTimeStamp());
                  logger.info("*** Setting cache for static resource '"+reqURIPart+ '\'');
              }
              else
              {
                  httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
                  httpResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0.
                  httpResponse.setDateHeader("Expires", 0); // Proxies.
                  logger.info("*** No cache for '"+reqURIPart+ '\'');
              }
      // Continue with next filter:
      chain.doFilter(request, response);
  }
  </code></pre>
  
  <p>and the InitBean-Part which is called on each startup of the Server:</p>
  
  <pre><code>@Singleton
  @Startup
      public class InitBean 
      {
          // Time on which server has been restarted:
          private static Date startUpDateTime = new Date();
          @PostConstruct
          private void StartUp() 
          {
              // Set startup date/time
              InitBean.startUpDateTime = new Date(System.currentTimeMillis());
          }
          public static Long getStartupDateTime4CacheAsTimeStamp()
          {
              // Calculate date difference in month since server has been started up:
              Calendar startCalendar = new GregorianCalendar();
              startCalendar.setTime(startUpDateTime);
              Calendar endCalendar = new GregorianCalendar();
              endCalendar.setTime(new Date());
              //
              int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
              int diffMonth = diffYear * 12 + endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
              // And set expiration date to 1 month in the future from server startup :
              diffMonth++;
              // Get date in future:
              startCalendar.add(Calendar.MONTH, diffMonth);
              return startCalendar.getTimeInMillis();
          }
      }
  </code></pre>