java 将 HTTP 标头添加到 index.html 的响应中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12283543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:18:25  来源:igfitidea点击:

Add HTTP header into response for index.html

javajakarta-eehttp-headers

提问by bugs_

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

在 JavaEE 应用程序中。
我在 web.xml 中有 index.html 页面作为“欢迎文件”

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

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

我想将 Http Header 添加到 index.html 的响应中

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

一种方法是使用 index.jsp 而不是 index.html 并在 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.

因为我不想使用 index.jsp 而不是 index.html。

采纳答案by jagamot

You can definitely add a filter,

您绝对可以添加过滤器,

try - responseheaderfilter

尝试 -响应头过滤器

回答by Jér?me Radix

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.

您可以要求您的 Web 服务器/servlet 容器为您添加这些标头。它将在服务器配置文件中而不是在 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.

或者您可以创建一个过滤器,为您添加标题。您必须在 web.xml 中配置过滤器。

This stackoverflow answershow you how to configure jetty to add headers. This other stackoverflow answershows you how to code a Filter that add headers.

stackoverflow 答案向您展示了如何配置 jetty 以添加标头。这个其他stackoverflow 答案向您展示了如何编写添加标题的过滤器。