• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Request Headers In Servlet

February 8, 2014 //  by Krishna Srinivasan//  Leave a Comment

  • Java EE Tutorials
  • Servlets Tutorials
  • Servlets Interview Questions

Request Header

Request header is used to pass additional information about the request or itself to the server. Request header can be used by client to pass useful information. getHeaderNames() and getHeader() methods of javax.servlet.http.HttpServletRequest interface can be used to get the header information. Following important request header information which comes from browser side can be frequently used while programming:

  1. Accept: This specifies the certain media types that are acceptable in the response.
  2. Accept-Charset: This indicates the character sets that are acceptable in the response.
  3. Accept-Encoding: This restricts the content-coding values that are acceptable in the response
  4. Accept-Language: This restricts the set of language that are preferred in the response.
  5. Authorization : This type indicates that user agent is attempting to authenticate itself with a server.
  6. From: This type contains internet email address for the user who controls the requesting user agent.
  7. Host: This type indicates internet host and port number of the resource being requested.
  8. If-Modified-Since: This type makes GET method condition. Do not return the requested information if it is not modified since the specified date.
  9. Range: This type request one or more sub-range of the entity, instead of the entire entity.
  10. Referer: This type enables client to specify, for the servers benefit, the address(URL) of the resources from which the Request-URL was obtained.
  11. User-Agent: This type contains information about the user agent originating the request.

Request Header Example

Following example displays the HTTP header information. Here we have used method getHeaderNames() to get the information.

package javabeat.net.servlets;

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 RequestHeaderDemo extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)

	throws IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("Headers<hr/>");
		Enumeration<String> headerNames = request.getHeaderNames();

		while (headerNames.hasMoreElements()) {
			String headerName = headerNames.nextElement();
			out.print("Header Name: <em>" + headerName);
			String headerValue = request.getHeader(headerName);
			out.print("</em>, Header Value: <em>" + headerValue);
			out.println("</em><br/>");
		}

	}
}

Execute the above program in Eclipse. Right mouse click on the class RequestHeaderDemoOutput, select Run > Run As and an output as below would be seen:
servlet_requheader_demo

Previous Tutorial : How To Refresh Servlet  || Next Tutorial : Session Tracking Using Servlet

Category: Java EETag: Servlets Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « How To Refresh Servlet
Next Post: Servlets Tutorials »

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact