1

I want to modify every HTML page I have in my Spring application. The reason behind this is I want to include a piece of JavaScript code on each page.

Unfortunately, Sitemesh can't help me out here (or at least I think it can't) because Ajax requests would also have to be affected by this JavaScript, and my Ajax requests are explicitly excluded from being decorated.

So my idea was to tamper with HttpServletResponse using a Filter in Spring, but I'm stuck at actually accessing the generated content and modifying it.

eagerMoose
  • 1,123
  • 5
  • 13
  • 35

1 Answers1

2

You access the ServletResponse which is part of the doFilter doFilter(ServletRequest request, ServletResponse response, FilterChain chain) and then add your additional content to it.

Sample example code: http://myjavaswtech.blogspot.com/2012/05/modifying-response-using-filters.html

Ram G
  • 4,829
  • 1
  • 16
  • 26
  • 1
    FYI the sample code link is no longer valid (the blog it pointed to is gone). This other question does have some sample code: http://stackoverflow.com/questions/14736328/looking-for-an-example-for-inserting-content-into-the-response-using-a-servlet-f – kenj0418 Jan 29 '16 at 16:35