Request headers provide configuration information of the browser.It may include accepting media types,accepting encoding types of browser,types of browser.

The following are some common request headers.

  1. Accept:
    Accepting media type of browser.
  2. Accept-encoding:
    By using this request header server can send compressed data as response,So that we can reduce download time.
  3. User Agent:
    By using User-Agent request header we can send compatible response for every browser.
  4. Cookie
    By using Cookie request header we can send session information to the server.So that session management is possible.

HttpServletRequest contains the following methods to retrieve request headers at server side.

  1. String getHeader(String name)
  2. Enumeration getHeaders(string hname)
  3. Enumeration getHeaderNames()

String getHeader(String name)

  1. Returns the value associated with specified header.
  2. If the header associated with multiple values then first value will be returned.
  3. If the specified header is not available this method returns null.
  4. Argument is not case sensitive

Enumeration getHeader(string hname)

  1. Returns all values associated with the header.
  2. If the request doesn’t contain specified headers this method returns empty enumeration object but not null.
  3. Argument is not case sensitive

Enumeration getHeaderNames()

Returns all header names associated with the request

Some request header as can be associated with int and date values.

To access these headers HttpServletRequest defines the more convenient methods.

  1. public int getIntHeader(string hname)
    int length =req.getIntHeader(“content-length”);
    If the specified header not associated with int value this method raises NumberFormatException.
  2. public long getDateHeader(string hname)
    Returns the date value as the number of milliseconds since 1970 jan 1st mid night. If we are passing this long value to the date constructor we will get exact date.
    Date d= new Date(long l)
    If the specified header not associated with date ,then this method returns IllegalArgumentException.


  1. Create servlet that extends HttpServlet
  2. Create web.xml
  3. Create war file and deploy web application
  4. Output of web application
  5. Download web application

Create servlet that extends HttpServlet

package com.java2learn;

import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestHeaderDemo extends HttpServlet {

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		PrintWriter out = resp.getWriter();

		Enumeration e = req.getHeaderNames();
		while (e.hasMoreElements()) {
			String hname = (String) e.nextElement();
			String hvalue = req.getHeader(hname);
			out.println(hname + "....." + hvalue);



Create web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
	xmlns="" xmlns:web=""
	id="WebApp_ID" version="2.5">



Final application structure


Create war file and deploy the application


Download Project: WebApp

Retrieving cookies from request

HttpservletRequest defines getCookies() method to retrive cookies at server sie.

Cookie[] c= req.getCookies();