gpt4 book ai didi

java - 如何使用 ServeletFilter 完成 Servlet 处理后更新响应 Content-Type

转载 作者:行者123 更新时间:2023-11-30 07:04:00 28 4
gpt4 key购买 nike

我尝试使用 ServletFilter 更改 ServletResponse Content-Type。但是,当 chain.doFilter() 调用时,servlet(在我的内容中为 AxisServlet)会更新该 Content-Type。但其他响应 header 已正确更新。

我尝试在调用 doFilter 后设置响应 header 。当时没有更新响应 header 。

有谁知道Servlet完成后更新ServletResponse的Content-Type的方法吗?

代码:

public class HeaderFilter implements Filter {

private HashMap<String,String> rsCustomHeaders = new HashMap<String,String>();

public void init(FilterConfig config) throws ServletException {

Enumeration<String> initParameterNames = config.getInitParameterNames();

while (initParameterNames.hasMoreElements()) {

String initParameterName = initParameterNames.nextElement();

rsCustomHeaders.put(initParameterName, config.getInitParameter(initParameterName));

}

System.out.println("init().rsCustomHeaders : " + rsCustomHeaders);
}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {

HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;

for (Map.Entry<String, String> rsCustomHeaderEntry : rsCustomHeaders.entrySet()) {
httpServletResponse.addHeader(rsCustomHeaderEntry.getKey(), rsCustomHeaderEntry.getValue());
}

System.out.println("doFilter().encoding :Set Response Headers Done");

chain.doFilter(httpServletRequest, httpServletResponse);

System.out.println("doFilter().HeaderFilter is Done!!!");


}

public void destroy() {
System.out.println("destroy(). : Destroy is called!");
}
}

web.xml

  <filter>
<filter-name>HeaderFilter</filter-name>
<filter-class>filters.HeaderFilter</filter-class>
<init-param>
<param-name>content-type</param-name>
<param-value>application/xml; charset=utf-8</param-value>
</init-param>
<init-param>
<param-name>Content_type</param-name>
<param-value>text/xml; charset=utf-8</param-value>
</init-param>
<init-param>
<param-name>rq_content-type</param-name>
<param-value>text/xml; charset=utf-8</param-value>
</init-param>
<init-param>
<param-name>Header-X</param-name>
<param-value>Value-X</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>HeaderFilter</filter-name>
<url-pattern>/services/GradeThreeMath</url-pattern>
</filter-mapping>

最佳答案

设置原始响应的内容类型。使用HttpServletResponseWrapper使用覆盖的 setContentType 来包装传递给 child.doFilterresponse。重写的 setContentType 应该忽略任何更改内容类型的尝试。

代码片段:

// Setting content type
httpServletResponse.setContentType("you-content-type");
chain.doFilter(httpServletRequest, new HttpServletResponseWrapper(httpServletResponse) {
@Override
public void setContentType(final String type) {
// Ignore any further attempts to change content type
}
});

关于java - 如何使用 ServeletFilter 完成 Servlet 处理后更新响应 Content-Type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40446439/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com