Setting HTTP Headers in Java Server Faces

综合技术 2015-03-03

In mylast postI discussed using HTTP headers to control browser caching of sensitive data. The post can befound here. The examples provided in that post were all ASP.Net and so I thought I’d cover how to explicitly set your HTTP Response headers when you are using the Java JSF framework.

Adding Headers via Code

You can set HTTP response headers directly in your code via the HTTPServletResponse
object, as below:

import javax.servlet.http.HttpServletResponse;

ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
HttpServletResponse response =  (HttpServletResponse)context.getResponse();
response.setHeader("TestHeader", "hello");

This results in the addition of this header in the HTTP Response: TestHeader: hello
.

Adding Headers in your XHTML Markup

Alternatively you can set it directly on each XHTML page via an event tag, as shown in the example below:

  
    
      
      
      
      
    
    

This again results in the addition of this header in the HTTP Response: TestHeader: hello
.

Adding Headers via a custom Web Filter

In order to implement a solution across your whole web application and for the ability to set headers for different resource types (not just facelets), a web filter may be what you need. Filters intercept your requests and responses to dynamically transform them or use the information contained in them. A good guide to Filters is this official Oracle one – The Essentials of Filters
, or check this one out – Servlet Filter to Set Response Headers

.

Here is the source code for a simple filter that sets a custom header and can be used to explicitly set HTTP response headers as required:

package Filters; 
import java.io.IOException; 
import javax.servlet.Filter; 
import javax.servlet.FilterChain; 
import javax.servlet.FilterConfig; 
import javax.servlet.ServletException; 
import javax.servlet.ServletRequest; 
import javax.servlet.ServletResponse; 
import javax.servlet.http.HttpServletResponse;
public class HeaderFilter implements Filter 
{ 
	public void init(FilterConfig fc) throws ServletException {} 
	public void doFilter(ServletRequest req, ServletResponse res, 
			FilterChain fc) throws IOException, ServletException 
	{ 
		HttpServletResponse response = (HttpServletResponse) res; 
		response.setHeader("TestHeader", "hello"); 
		fc.doFilter(req, res); 
	} 
	public void destroy() {} 
}

Once you’ve coded your filter you need to update your Web.xml file to tell the framework that you want to use this filter. Do this by adding a filter element containing the name and class details. You also need to set a filter-mapping element to map the filter with the request for resources. In the below example which configures the Filter above I have mapped “/*” meaning all requests will go through the filter but you can configure this to only impact certain resources or file types.

  MapThisCustomHeaderFilter
  Filters.HeaderFilter
  
    value
    test
  


  MapThisCustomHeaderFilter
   /*

Once configured via the web.xml file the custom header sets this header on all respones: TestHeader : hello
.

您可能感兴趣的

Wuzz 0.4.0 发布,HTTP 命令行调试工具 Wuzz 0.4.0 已发布,Wuzz 是一个用于 HTTP 调试的命令行工具,其命令行参数与 cURL 的参数相似。 更新内容: 保存/加载请求 ( -f / --file flags for loading) 多表格处理 ( -F / --form f...
200G视频自学视频免费领取 从09年兴起的移动互联网,到16年兴起的AI和大数据,近年来,技术的发展可谓日新月异。这里,小编给大家收集了一些热门的最新视频教程,有兴趣的可以学习下,当然有问题还可以加群哦278792776。 视频区 机器学习链接: https://pan.baidu.com/s/1o9hlR5o...
从antlr扯淡到一点点编译原理 迁移一批老文章到掘金 网上我们看到很多看起来很神奇的,能做到各种语言之间转换的东西 比如JSPatchConvertor,他可以做到把OC代码输入后直接转化成JSPatch代码, 比如objc2swift,他可以做到把OC代码输入后直接转化为Swift代码 ...
微信小程序中使用Fly 发起http请求 fly.png 微信小程序的 javascript运行环境和浏览器不同,页面的脚本逻辑是在 JsCore 中运行, JsCore 是一个没有窗口对象的环境,所以不能在脚本中使用 window ,也无法在脚本中操作组件,JsCore中也没有 XmlhttpRequest对象,所以j...
《阿里巴巴Java开发手册v1.4.0(详尽版)》发布,新增16条设计规约... 阿里巴巴Java开发手册v1.4.0(详尽版)发布,新增16条设计规约。设计规约是根据阿里巴巴实际项目架构经验提炼而成,主要从UML图和架构设计原则来规定比较基础的软件设计理念,并且明确了超过什么样的阈值需要以什么样的方式来呈现设计思维。 点击下载《阿里巴巴Java开发手册》v1.4.0(...
0

责编内容来自:Rich Hewlett (本文源链)
阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。