top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

how to retrieve headers in Servlet?Get all Request Headers in Servlet with example?

+4 votes
638 views
how to retrieve headers in Servlet?Get all Request Headers in Servlet with example?
posted Mar 12, 2015 by Roshni

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
 
Best answer

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.

Use HttpServletRequest.getHeaderNames() to get an Enumeration of header names.

Use HttpServletRequest.getHeaders(headerName) to get the value of a specific header.

Here 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:

http://myhost:8080/jcgsnippets/jcgservlet

Output:

host
    myhost:8080
user-agent
    Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0
accept
    text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
accept-language
    en-us,en;q=0.5
accept-encoding
    gzip, deflate
accept-charset
    ISO-8859-1,utf-8;q=0.7,*;q=0.7
connection
    keep-alive
cache-control
    max-age=0

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

answer Mar 16, 2015 by Karthick.c
+2 votes

Get the list of all header names using servletRequest.getHeaderNames(), run a loop on all header names and get value for individual header using servletRequest.getHeader(headername)

Sample code :

  Enumeration names = servletRequest.getHeaderNames();
    while (names.hasMoreElements()) {
      String name = (String) servletRequest.nextElement();
      String value = servletRequest.getHeader(name);
    }
answer Mar 15, 2015 by Rahul Jha
...