How do I get servlet request headers information?

package org.kodejava.example.servlet;

import java.util.Enumeration;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 public class ServletRequestHeader extends HttpServlet implements Servlet {
    protected void doGet(HttpServletRequest request, 
            HttpServletResponse response) throws ServletException, IOException {
        PrintWriter writer = response.getWriter(); 

        Enumeration enumeration = request.getHeaderNames();
        while (enumeration.hasMoreElements()) {
            // Get request header name
            String name = (String) enumeration.nextElement();

            // Get request header value
            String value = request.getHeader(name);
            writer.println("Header [" + name + " = " + value + "<br/>");            

            // Read request values, for header information that have multiple
            // values.
            Enumeration values = request.getHeaders(name);
            while (values.hasMoreElements()) {
                value = (String) values.nextElement();
                writer.println("   Header [" + name + " = " + value + "<br/>");         

    protected void doPost(HttpServletRequest request, 
            HttpServletResponse response) throws ServletException, IOException {
        // Do Nothing!

Wayan Saryada

Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. If these posts help, you can support me, buy me a cup of coffee or tea. Thank you 🥳

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.