Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Home Jsp Simple-jsp-example Request Headers in JSP

Related Tutorials


Share on Google+Share on Google+

Request Headers in JSP

Whenever an http client sends a request, it sends the request in the form of get or post method or any other HttpRequest methhods.

Request Headers in JSP


Whenever an http client sends a request, it sends the request in the form of get or post method or any other HttpRequest  methhods. It can also sends the headers with it. All the headers are optional except Content-length, which is required only for POST requests. Here is the list of most commonly used headers are:

1) Accept : The MIME types the browser prefers.

2) Accept- Encoding: The types of data encodings the browser knows how to decode.

3) Accept- Charset: The character set the browser expects.

4) Content-Length: It is mostly used for POST messages, it tells how much data is attached.

5) Cookie: It is one of the most frequently used headers, it returns the cookies.

6) Accept- Language: The language the browser is expecting.

There are many more request headers availables. These headers you can see in the output of the program.

In this example we are going to retrieve all the headers available in the request object. We can retrieve these by using the Enumeration interface of the java.util package. Use while condition to check whether there are more headers or not. And at last print the headers on the browsers by using the out implicit object.

The code of the program is given below:

<%@ page import = " java.util.* " %>
		<title>Getting a header in Jsp</title>
		Enumeration enumeration = request.getHeaderNames();
		while (enumeration.hasMoreElements()) {
		String string = (String)enumeration.nextElement();
		out.println("<font size = 6
                >" +string +": " + request.getHeader(string)+ "</font><br>");

Output of the program is given below:

Download this example:


If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 8, 2008

Related Tutorials

Discuss: Request Headers in JSP  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image