Setting HTTP Headers in Java Server Faces

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
.

稿源:Rich Hewlett (源链) | 来自的投递 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合技术 » Setting HTTP Headers in Java Server Faces

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录