Request Headers In Servlet

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

Comments

comments

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.

Speak Your Mind

*