Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9255041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:37:22+00:00 2026-06-18T11:37:22+00:00

I have a Struts2 (version 2.2.6) web application where I want to set the

  • 0

I have a Struts2 (version 2.2.6) web application where I want to set the Expires response header for static resources that are not present in the traditional static-related folders for struts2. E.g. they are placed under /images/, /css/, /js/ at the root. I want all the resources to have a custom Expires response header.

How to do this in Struts2?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:37:24+00:00Added an answer on June 18, 2026 at 11:37 am

    So based on the hint provided by @Dave I have come up with a following approach.

    In web.xml setup following filter & it’s mapping.

    <filter>
        <filter-name>StaticContentCacheHeaderFilter</filter-name>
        <filter-class>management.filters.StaticContentCacheHeaderFilter</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>StaticContentCacheHeaderFilter</filter-name>
        <url-pattern>/images/*</url-pattern>
    </filter-mapping>
    

    And the StaticContentCacheHeader which implements Filter has the following method –

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpServletRequest request = (HttpServletRequest) servletRequest;
    
        long ifModifiedSince = 0;
        try {
            ifModifiedSince = request.getDateHeader("If-Modified-Since");
        } catch (Exception e) {
            LOG.warning("Invalid If-Modified-Since header value: '"
                    + request.getHeader("If-Modified-Since") + "', ignoring");
        }
    
        long now = DateTime.now().getMillis();
    
        long lastModifiedMillis = now;
    
        DateTime dateTime = new DateTime();
    
        //1 month seems to be the minimum recommended period for static resources acc to          
        //https://developers.google.com/speed/docs/best-practices/caching#LeverageBrowserCaching
        long expires = dateTime.plusMonths(1).getMillis();
    
        if (ifModifiedSince > 0 && ifModifiedSince <= lastModifiedMillis) {
            // not modified, content is not sent - only basic
            // headers and status SC_NOT_MODIFIED
            response.setDateHeader("Expires", expires);
            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            return;
        }
    
        // set heading information for caching static content
        response.setDateHeader("Date", now);
        response.setDateHeader("Expires", expires);
        response.setDateHeader("Retry-After", expires);
        response.setHeader("Cache-Control", "public");
        response.setDateHeader("Last-Modified", lastModifiedMillis);
    
        filterChain.doFilter(servletRequest, servletResponse);
    }
    

    This will set the cache headers to expire in one month from now.

    Let me know if there is a better way to deal with this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with a Struts2 + dojo web application that I've inherited.
I have a struts2 web application which accepts both POST and GET requests in
I have one legacy web application based on struts2 (primarily using annotation). While debugging
I have a web application developed with Struts2, JSP, JPA, Spring and MySql. I
We have a Struts 2 web application that's used by pretty much every employee
I have struts2 application with struts2 version 2.0, There is no error on server
I have a web application using struts2 and spring 3. for configuring spring I
I have a struts2 web application developed in eclipse IDE and exported it as
We have a web application that periodically sends out e-mails to users. At the
I have a Java web application that has a 'disconnected' Java Swing desktop app.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.