How to Add Custom Header to HTTP Request in Filter

Add Custom Header to HTTP Request

In this post, we will show you a Java servlet simple example program about, how to add custom header to HTTP header in filter. This program has been tested and shared in the same post.

Request Wrapper Class

package com.dineshkrish;

import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class MyRequestWrapper extends HttpServletRequestWrapper {

	private Map<String, String> headerMap = new HashMap<String, String>();

	public MyRequestWrapper(HttpServletRequest request) {
		super(request);
	}

	public void addHeader(String name, String value) {
		headerMap.put(name, value);
	}

	@Override
	public String getHeader(String name) {
		String headerValue = super.getHeader(name);
		if (headerMap.containsKey(name)) {
			headerValue = headerMap.get(name);
		}
		return headerValue;
	}

	@Override
	public Enumeration<String> getHeaderNames() {
		List<String> names = Collections.list(super.getHeaderNames());
		for (String name : headerMap.keySet()) {
			names.add(name);
		}
		return Collections.enumeration(names);
	}

	@Override
	public Enumeration<String> getHeaders(String name) {
		List<String> values = Collections.list(super.getHeaders(name));
		if (headerMap.containsKey(name)) {
			values.add(headerMap.get(name));
		}
		return Collections.enumeration(values);
	}

}

Filter Class

package com.dineshkrish;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

@WebFilter(urlPatterns = "/*")
public class MyFilter implements Filter {

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

		HttpServletRequest httpServletRequest = (HttpServletRequest) request;

		MyRequestWrapper requestWrapper = new MyRequestWrapper(httpServletRequest);

		requestWrapper.addHeader("first.name", "Dinesh");
		requestWrapper.addHeader("last.name", "Krishnan");

		chain.doFilter(requestWrapper, response);
	}

}

Servlet Class

package com.dineshkrish;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

@WebServlet(urlPatterns = "/headers")
public class MyServlet extends HttpServlet {

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		PrintWriter out = resp.getWriter();

		resp.setContentType("text/html");

		Enumeration<String> headerNames = req.getHeaderNames();

		out.println("<ul>");

		while (headerNames.hasMoreElements()) {
			String header = headerNames.nextElement();
			out.println("<li>" + header + "-" + req.getHeader(header) + "</li>");
		}

		out.println("</ul>");

		out.close();
	}

}

Output

Add Custom Header to HTTP Request

References

https://stackoverflow.com/questions/2811769/adding-an-http-header-to-the-request-in-a-servlet-filter

Hello, folks, I am a founder of dineshkrish.com. I love Java and Open source technologies, If you find my tutorials are useful, please consider making donations to these charities.