Obtaining HTTP Request Headers from HttpServletRequest

In this article we will learn how to get the HTTP Request Headers via HttpServletRequest, The HTTP request which a client browser sends to the server includes HTTP request headers with  some important information, such as cookies and the referer. You can access these headers from the HttpServletRequest object passed to a doxxx method.In this example we will get all the header information using the getHeaderNames() method of the HttpServletRequest interface which will return Enumeration of the all the header information.

RequestHeaderExample.java

package com.javainterviewpoint;

import java.io.IOException;
import java.io.PrintWriter;
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 RequestHeaderExample  extends HttpServlet 
{
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException 
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        Enumeration enumeration = request.getHeaderNames();
        while (enumeration.hasMoreElements()) 
        {
             String header = (String) enumeration.nextElement();
             out.println(header + ": " + request.getHeader(header) + "
");
        }
    }
}

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" id="WebApp_ID" version="2.5">
 <display-name>ServletsTutorial</display-name>
 <servlet>
     <servlet-name>RequestHeaderExample</servlet-name>
     <servlet-class>com.javainterviewpoint.RequestHeaderExample</servlet-class>
 </servlet>
 <servlet-mapping>
     <servlet-name>RequestHeaderExample</servlet-name>
     <url-pattern>/RequestHeaderExample</url-pattern>
 </servlet-mapping>
</web-app>

When we run the above code we will get the below output

URL : http://localhost:8080/ServletsTutorial/RequestHeaderExample

Output:

accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
accept-language: en-US
cache-control: no-cache
user-agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
accept-encoding: gzip, deflate
host: localhost:8080
connection: Keep-Alive

Instead of getting all the header, we can get the value of a particular header by using getHeader() method. We will modify the above code a bit.

package com.javainterviewpoint;

import java.io.IOException;
import java.io.PrintWriter;
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 RequestHeaderExample  extends HttpServlet 
{
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException 
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("User Agent : "+request.getHeader("user-agent"));
    }
}

Hit on the URL : http://localhost:8080/ServletsTutorial/RequestHeaderExample

Output:

User Agent : Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)

Leave a Reply

Your email address will not be published. Required fields are marked *