In this section you will studied how to display the header information in servlet.
When a HTTP client sends a request, generally GET or POST method is specified. It can send a number of headers. Here are some headers:
Accept - MIME type, the browser prefers.
Accept-Charset - The Character set the browser expects.
Accept-Language - The language which the browser is accepting.
Connection - If a servlet gets a keep-Alive value or gets a request line indicating HTTP 1.1, it may be able to take advantage of connection.
Host (host and port as defined in url).
Content-Length (for POST messages).
User-Agent (type of browser, if servlet is returning browser-specific content).
In the given example, method getHeaderNames() is called to get an Enumeration of all header names received on this particular request. The getMethod() returns the main request method (GET or POST, but HEAD, PUT, DELETE are also possible). The getRequestURI() method returns the URI (the part of url that came after the host and port , but before the form data). The getRequestProtocol() returns the protocol which is generally HTTP/1.0 or HTTP/1.1. Using while loop, all the headers are passed into the header. Now if header is not equal to null, all the header information will be displayed by using the method req.getHeader().
Here is the code of HeaderInformation.java
After compiling the servlet, place it to the appropriate place in the Tomcat. Run the tomcat. Open the browser and type the url http://localhost:8080/Myexamples/HeaderInformation
Following output will be displayed.
If you enjoyed this post then why not add us on Google+? Add us to your Circles
Liked it! Share this Tutorial
Discuss: Header Information available from the client in Servlet
Post your Comment