3

In JavaEE application.
I have index.html page as "welcome-file" in web.xml

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

I want to add Http Header into response for index.html

One way is use index.jsp instead of index.html and add inside an scriptlet:

<% response.addHeader("X-Frame-Options", "DENY");  %>

Is there any other way? Is there an possiblility to add some kind of filter
For example something like:

WelcomeFileFilter {
  void filter(HttpServletResponse response) {
    response.addHeader("X-Frame-Options", "DENY");
  }
} 

Because I don't want to use index.jsp instead of index.html.

bugs_
  • 3,544
  • 4
  • 34
  • 39

2 Answers2

1

You can definitely add a filter,

try - responseheaderfilter

jagamot
  • 5,348
  • 18
  • 59
  • 96
  • Unfortunatelly it seems, that filter do not apply, when welcome-file is index.html. It is applyed only if welcome-file is index.jsp :-( – bugs_ Sep 05 '12 at 14:32
  • Sorry filter works fine. I had wrong mapping "/*". I changed it into "*" and now it works fine. – bugs_ Sep 05 '12 at 14:37
  • responseheaderfilter is old and not maintained anymore (last code evol in Jun. 2009) and has some serious bugs like this one : http://code.google.com/p/responseheaderfilter/issues/detail?id=2 Why do you want to use a lib when the standard JavaEE covers all your needs ? – Jérôme Radix Sep 05 '12 at 14:43
  • 1
    Don't use this solution, use this one instead: http://stackoverflow.com/a/26875465/1583422 – Frank Orellana Aug 05 '16 at 18:17
1

You can ask your web server/servlet container to add those headers for you. It will be configured in the server configuration files not in web.xml.

Or you can create a filter that will add headers for you. You'll have to configure the filter in your web.xml.

This stackoverflow answer show you how to configure jetty to add headers. This other stackoverflow answer shows you how to code a Filter that add headers.

Community
  • 1
  • 1
Jérôme Radix
  • 10,285
  • 4
  • 34
  • 40