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 6051987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:52:40+00:00 2026-05-23T07:52:40+00:00

How could I add a response header – say X-Time that would look something

  • 0

How could I add a response header – say X-Time that would look something like:

X-Time: 112

Where the value given would be the time in milliseconds that the response took to process? Is there a really simple way to add this to an Grails app? Not something I want to leave on permanently but would be nice to have while developing my app.

  • 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-05-23T07:52:40+00:00Added an answer on May 23, 2026 at 7:52 am

    To simply add a header to the response, you can use an after Filter.

    // grails-app/conf/MyFilters.groovy
    class MyFilters {
        def filters = {
            addHeader(uri: '/*') {
                after = {
                    response.setHeader('X-Time', value)
                }
            }
        }
    }
    

    Edit:

    To actually compute the time, it’d probably be more proper to use a javax.servlet.Filter instead of a Grails filter.

    src/groovy/com/example/myproject/MyFilter.groovy

    package com.example.myproject
    
    import javax.servlet.*
    
    class MyFilter implements Filter {
    
        void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
    
            def start = System.currentTimeMillis()
            chain.doFilter(request, response)
    
            def elapsed = System.currentTimeMillis() - start
            response.setHeader('X-Time', elapsed as String)
        }
    
        void init(FilterConfig config) { }
        void destroy() { }
    }
    

    src/templates/war/web.xml
    (run grails install-templates if src/templates isn’t already in your source tree)

    <filter>
      <filter-name>timer</filter-name>
      <filter-class>com.example.myproject.MyFilter</filter-class>
    </filter>
    <filter-mapping>
      <filter-name>timer</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
    

    The reason for using the javax.servlet.Filter is so you don’t have to separate out your “before” and “after” actions, and can therefore hold onto the start time throughout the entire filter chain & servlet execution.

    Supplementary note:

    To me, it seems strange to want to return the server elapsed execution time as a response header. Perhaps you have a decent reason for doing it, but in most cases I’d A) either be more concerned about total round-trip time (as observed by the client), or B) be logging elapsed execution times on the server for my own system administration/metrics purposes.

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

Sidebar

Related Questions

If you could add anything to Cocoa, what would it be? Are there any
Im wondering how i could add a success or fail message to return at
how we could add a special class for labels and errors for a zend-form-element
I'm wondering how you could add flir on the captions of a lightbox. I
When answering another question I started to wonder how I could Add new properties
How could I add to a plot an OHLCSeriesCollection and a TimeSeriesCollection , in
How could I add the values of a matrix int[,] diagonally from bottomleft, to
How could I add a own stoplist to MySQL 5?
If I wanted to badly enough, could I add additional LINQ constructs to LINQ-to-SQL
Could somebody give me a pointer on why I need to add my project

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.