Showing all request header in servlet

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

  1. Create a Java with Maven -> Web application project
  2. Give Project Name
  3. Create package com.ebhor.servletheaderes
  4. Create a Servlet with name Header.java
  5. Build and run the project

Creating a project

Java and Maven project creation

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.

1328_project_explorer
Servlet Header Request page-index.jsp

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.

Project Configuration file -pom.xml

This is a Maven configuration file used to configure dependencies and used to build projects.

Result

Index page requesting to get all request header.

Servlet Header Request

First, a host request header is read by its value.

consequently, all request headers are read with values.

Reading all Servlet request Headers

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

Result

Showing all request headers in servlet

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