This tutorial will find the read request header name and values in the servlet.
There are many requests headers present like:
host, connection, user-agent, cookie, etc.
In the below project, we read the request project header names and values.
Technology User
apache-tomcat-9.0.71
javaee-web-api 7.0
Java 1.8
Steps to develop this project
- Create a Java with Maven -> Web application project
- Give Project Name
- Create package
com.ebhor.servletheaderes
- Create a Servlet with name
Header.java
- Build and run the project
Creating a project
Assign project name ServletHeader
Project Explorer
Following is the project explorer window for this project.
Inside the source package, all packages and files are kept.
Web Pages are used to store jsp and other files
pom.xml is available in Project Files.

Servlet Header Request page-index.jsp
1 2 3 4 5 6 7 8 9 10 11 | <!DOCTYPE html> <html> <head> <title>Index Page</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <h1>Servlet Header Example</h1> <a href="header">Get Headers</a> </body> </html> |
Servlet header processing -Header.java
On clicking on the header
URL it will call header.java servlet.
This will be called by HTTP get request and doGet() method of servlet is get called.
To access all header information request.getHeaderNames()
method is used.
This method will return the Enumeration of all header names.
By iterating header enumeration we can get the values of each header.
Here a single request header host is also read in the program, then after all request headers are read.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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; @WebServlet(name = "Header", urlPatterns = {"/header"}) public class Header extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String hostValue = request.getHeader("host"); out.println("<h3>Printing a single header</h3>"); out.println("Header name <b>host </b> value <b>" + hostValue + "</b>"); Enumeration<String> headerNames = request.getHeaderNames(); out.println("<h3>Printing all headers</h3>"); while (headerNames.hasMoreElements()) { String headerName = headerNames.nextElement(); String headerValue = request.getHeader(headerName); out.println("Header name <b>" + headerName + "</b> value <b>" + headerValue + "</b><br/>"); } } } |
Project Configuration file -pom.xml
This is a Maven configuration file used to configure dependencies and used to build projects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ebhor</groupId> <artifactId>ServletHeaders</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>ServletHeaders</name> <properties> <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> <compilerArguments> <endorseddirs>${endorsed.dir}</endorseddirs> </compilerArguments> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.6</version> <executions> <execution> <phase>validate</phase> <goals> <goal>copy</goal> </goals> <configuration> <outputDirectory>${endorsed.dir}</outputDirectory> <silent>true</silent> <artifactItems> <artifactItem> <groupId>javax</groupId> <artifactId>javaee-endorsed-api</artifactId> <version>7.0</version> <type>jar</type> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> </plugins> </build> </project> |
Result
Index page requesting to get all request header.
First, a host request header is read by its value.
consequently, all request headers are read with values.

Showing all request headers in servlet
for this we have created a JSP file that is not shown in the above project explorer.
You can use any jsp page for this.
code is as below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | <%@page import="java.util.Enumeration"%> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <title>Servlet Request Headers</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet"> <script </head> <body> <div class="container mt-3"> <h2>Printing All Request Header</h2> <table class="table table-striped"> <thead> <tr> <th>Sr No</th> <th>Header Name</th> <th>Header Value</th> </tr> </thead> <tbody> <% int i = 1; Enumeration<String> headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String headerName = headerNames.nextElement(); String headerValue = request.getHeader(headerName); %> <tr> <td><%=i++%></td> <td><%=headerName%></td> <td><%=headerValue%></td> </tr> <% } %> </tbody> </table> </div> </body> </html> </body> </html> |
Result

Read More
Sending data to servlet using http get method
Submitting data to servlet using http post
Getting parameter values in Servlet getParameterNames
Servlet Annotation WebServlet Example
Servlet url and class mapping using web.xml
Employee Management System Project JSP Servlet