,可以通过以下步骤实现:
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
public class CustomHeaderFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// 初始化操作
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
httpRequest.addHeader("Custom-Header", "Custom-Value");
chain.doFilter(request, response);
}
@Override
public void destroy() {
// 销毁操作
}
}
在上述示例中,通过HttpServletRequest的addHeader()方法,在dofilter中为请求添加了一个名为"Custom-Header",值为"Custom-Value"的自定义标头。
应用场景:
腾讯云相关产品推荐:
更多腾讯云产品信息,请参考腾讯云官方网站:腾讯云
领取专属 10元无门槛券
手把手带您无忧上云