servlet

Get all Request Headers in Servlet

Photo of Byron KiourtzoglouByron KiourtzoglouNovember 11th, 2012Last Updated: March 7th, 2019
2 3,083 1 minute read

This is an example on how to get all the Request Headers in a Servlet. This is to make it easy for the programmer to parse an HTTP request object and decide on the response you will provide.

In short in order to get all request headers in Servlet, on should follow these steps:

  • Create a handleRequest method so you can use it both in doGet and doPost methods.
  • UseHttpServletRequest.getHeaderNames() to get anEnumeration of header names.
  • UseHttpServletRequest.getHeaders(headerName) to get the value of a specific header.

 
Here is the code:

package com.javacodegeeks.snippets.enterprise;import java.io.IOException;import java.io.PrintWriter;import java.util.Enumeration;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class GetAllRequestHeadersInServlet extends HttpServlet {private static final long serialVersionUID = -2128122335811219481L;public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {handleRequest(req, res);}public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {handleRequest(req, res);}public void handleRequest(HttpServletRequest req, HttpServletResponse res) throws IOException {PrintWriter out = res.getWriter();res.setContentType("text/plain");Enumeration<String> headerNames = req.getHeaderNames();while (headerNames.hasMoreElements()) {String headerName = headerNames.nextElement();out.write(headerName);out.write("n");Enumeration<String> headers = req.getHeaders(headerName);while (headers.hasMoreElements()) {String headerValue = headers.nextElement();out.write("t" + headerValue);out.write("n");}}out.close();}}

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  version="2.5"><display-name>JCG Snippets Web Project</display-name><servlet><servlet-name>JCG Snippets Application</servlet-name><servlet-class>com.javacodegeeks.snippets.enterprise.GetAllRequestHeadersInServlet</servlet-class></servlet><servlet-mapping><servlet-name>JCG Snippets Application</servlet-name><url-pattern>/jcgservlet</url-pattern></servlet-mapping></web-app>

URL:

Want to be a Servlets Master ?
Subscribe to our newsletter and download the Java ServletUltimateGuideright now!
In order to help you master programming with Java Servlets, we have compiled a kick-ass guide with all the major servlet API uses and showcases! Besides studying them online you may download the eBook in PDF format!

Thank you!

We will contact you soon.

http://myhost:8080/jcgsnippets/jcgservlet

Output:

hostmyhost:8080user-agentMozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0accepttext/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8accept-languageen-us,en;q=0.5accept-encodinggzip, deflateaccept-charsetISO-8859-1,utf-8;q=0.7,*;q=0.7connectionkeep-alivecache-controlmax-age=0

This was an example on how to get all Request Headers in Servlet.

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Byron KiourtzoglouByron KiourtzoglouNovember 11th, 2012Last Updated: March 7th, 2019
2 3,083 1 minute read
Photo of Byron Kiourtzoglou

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor atJava Code Geeks.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.