Whenever an http client sends a request, 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.
In this example we are going to retrieve some of the headers available in the request object. We are retrieving it by using the expression tag.
The code of the program is given below:
<html> <head> <title>To retrieve a specific header</title> </head> <body> <font size = 8>The request method is <%= request.getMethod() %></font><br> <font size = 8>The browser is <%= request.getHeader("user-agent") %></font><br> <font size = 8>The request URI is <%= request.getRequestURI() %></font><br> <font size = 8>The request protocol is <%= request.getProtocol() %></font> </body> <html>
The output of the program is given below:
Posted on: February 8, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles