如何在 GlassFish 中为 auto-delivered 文件设置 Content-Type?
How to set Content-Type for auto-delivered file in GlassFish?
将 .war 文件部署到 GlassFish Server(当前为 4.1)时,GF 服务器会自动从 WEB-INF/
文件夹传送文件。 (除非我通过指定一个 Servlet 来覆盖它们的地址。)
实际上在部署.war文件到GF服务器时,它会将WEB-INF/
下的那些文件提取到{gf-home}/glassfish/domains/domain1/applications/{app-name}
。
然后它在访问 http://{hostname}:8080/{app-name}/{path}
.
时传送它们
现在访问 .json 文件时,服务器不会发送 HTTP Content-Type: application/json
header。
这导致页面无法正确加载,FireFox 控制台显示 XML Parsing Error: not well-formed
异常,即使文件内容完全相同。
所以我的猜测是缺少 Content-Type 标签。
如何为 app/project 本身更改此 mime-mapping?
从我目前看到的页面来看,可以在 {gf-home}/glassfish/domains/domain1/default-web.xml
文件中重新定义此行为,定义 mime-mapping
。但假设我无法访问该文件,只能上传 .war 文件。 有什么解决办法吗? 是否可以将 default-web.xml
的某处打包到 .war 文件中?
目前我能想到的另一个解决方案是用 servlet 覆盖特定的 .json 文件地址,并在 [=44= 中添加 Content-Type
header ].但我不确定是否有一种万无一失的方法可以在运行时访问和读取 .json 文件,但不会将它们移动到 Java 源代码中的任何位置,而是将它们留在 WEB-INF/文件夹? 有什么建议吗?
How can I change this mime-mapping for the app/project itself?
通过在 webapp 自己的 /WEB-INF/web.xml
中声明 <mime-mapping>
条目。
从 Servlet 版本 3.0 开始,web.xml
文件成为可选的。这也许可以解释为什么你找不到人。你可以在 webapp 中提供你自己的。 GlassFish 4.1 是一个支持 Servlet 3.1 的容器,因此下面的 Servlet 3.1 兼容 web.xml
应该可以帮助您入门:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- Config here. -->
</web-app>
在你的具体情况下,你需要下面的 mime 映射:
<mime-mapping>
<extension>json</extension>
<mime-type>application/json</mime-type>
</mime-mapping>
另请参阅:
- What is conf/web.xml used for in Tomcat as oppsed to the one in WEB-INF?
- How to set the content type on the servlet
- Servlet 3.1 specification (PDF) chapter 10.13
将 .war 文件部署到 GlassFish Server(当前为 4.1)时,GF 服务器会自动从 WEB-INF/
文件夹传送文件。 (除非我通过指定一个 Servlet 来覆盖它们的地址。)
实际上在部署.war文件到GF服务器时,它会将WEB-INF/
下的那些文件提取到{gf-home}/glassfish/domains/domain1/applications/{app-name}
。
然后它在访问 http://{hostname}:8080/{app-name}/{path}
.
现在访问 .json 文件时,服务器不会发送 HTTP Content-Type: application/json
header。
这导致页面无法正确加载,FireFox 控制台显示 XML Parsing Error: not well-formed
异常,即使文件内容完全相同。
所以我的猜测是缺少 Content-Type 标签。
如何为 app/project 本身更改此 mime-mapping?
从我目前看到的页面来看,可以在 {gf-home}/glassfish/domains/domain1/default-web.xml
文件中重新定义此行为,定义 mime-mapping
。但假设我无法访问该文件,只能上传 .war 文件。 有什么解决办法吗? 是否可以将 default-web.xml
的某处打包到 .war 文件中?
目前我能想到的另一个解决方案是用 servlet 覆盖特定的 .json 文件地址,并在 [=44= 中添加 Content-Type
header ].但我不确定是否有一种万无一失的方法可以在运行时访问和读取 .json 文件,但不会将它们移动到 Java 源代码中的任何位置,而是将它们留在 WEB-INF/文件夹? 有什么建议吗?
How can I change this mime-mapping for the app/project itself?
通过在 webapp 自己的 /WEB-INF/web.xml
中声明 <mime-mapping>
条目。
从 Servlet 版本 3.0 开始,web.xml
文件成为可选的。这也许可以解释为什么你找不到人。你可以在 webapp 中提供你自己的。 GlassFish 4.1 是一个支持 Servlet 3.1 的容器,因此下面的 Servlet 3.1 兼容 web.xml
应该可以帮助您入门:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- Config here. -->
</web-app>
在你的具体情况下,你需要下面的 mime 映射:
<mime-mapping>
<extension>json</extension>
<mime-type>application/json</mime-type>
</mime-mapping>
另请参阅:
- What is conf/web.xml used for in Tomcat as oppsed to the one in WEB-INF?
- How to set the content type on the servlet
- Servlet 3.1 specification (PDF) chapter 10.13