Header Information available from the client in Servlet
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
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HeaderInformation extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
printHeader(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
printHeader(req, res);
}
public void printHeader(HttpServletRequest req,
HttpServletResponse res) throws IOException, ServletException {
String header = null;
String head = "<html><head><title> Header Information</title>
</head><body>";
String foot = "</body></html>";
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
Enumeration enumeration = req.getHeaderNames();
pw.println(head);
pw.println("<h2> Header Information </h2>");
pw.println("<B>Request Method: </B>" +
req.getMethod() + "<BR>\n" +
"<B>Request URI: </B>" +
req.getRequestURI() + "<BR>\n" +
"<B>Request Protocol: </B>" +
req.getProtocol() + "<BR><BR>\n" );
pw.println("<table>");
while (enumeration.hasMoreElements()) {
header = (String) enumeration.nextElement();
if (header != null) {
pw.println("<tr><td><b>" + header + "</td>");
pw.println("<td >" + req.getHeader(header)
+ "</td></tr>");
}
}
pw.println("</table><br>");
pw.println(foot);
}
}
|
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.

Download Source Code
|