Servlet client HTTP request
When a browser requests a web page, it sends specific information to the Webserver that cannot be read directly because it is transmitted as part of the header of the HTTP request. You can check the HTTP protocol for more information.
The following is important header information from the browser side, which you can use frequently in Web programming:
Header information |
Description |
---|---|
|
This header information specifies the type of MIME that the browser or otherclient can handle. The value image/png or image/jpeg are the two most common possible values. |
|
This header information specifies the character set that the browser can useto display the information. For example, ISO-8859-1. |
|
This header information specifies the type of encoding that the browser knows how to handle. The value gzip or compress are the two most common possible values. |
|
This header information specifies the preferred language of the client, in which case Servlet produces results in multiple languages. For example, en, en-us, ru, and so on. |
|
This header information is used for clients to identify themselves when visiting password-protected web pages. |
|
This header indicates whether the client can handle persistent HTTP connections. Persistent connections allow clients or other browsers to retrieve multiple files through a single request. A value of Keep-Alive means that a persistent connection is used. |
|
This header information applies only to POST requests and gives the size of the POST data in bytes. |
|
This header returns the cookies that was previously sent to the browser to the server. |
|
This header information specifies the host and port in the original URL. |
|
This header information represents the page that the client wants only if the page has changed after the specified date. If no new results are available, the server sends a 304code indicating the Not Modified header information. |
|
This header information is the opposite of If-Modified-Since, which specifies that the operation will succeed only if the document is earlier than the specified date. |
|
This header information indicates the URL of the Web page you are pointing to. For example, if you click a link to page 2 on page 1, the URL of page 1 will be included in the Referer header information when the browser requestspage 2. |
|
This header information identifies the browser or other client that made therequest and can return different content to different types of browsers. |
The method of reading HTTP header
The following method can be used to read the HTTP header in a Servlet program. These methods are passed through HttpServletRequest
object is available.
Serial number |
Method & description |
---|---|
1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
HTTP Header request instance
The following example uses the HttpServletRequest
of getHeaderNames()
method to read the HTTP header information. This methodreturns an enumeration containing header information related to the currentHTTP request.
Once we have an enumeration, we can loop through it in a standard way, usingthe hasMoreElements()
method to determine when to stop, using the nextElement()
method to get the name of each parameter.
//Import necessary Java libraries
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/DisplayHeader")
//Extend HttpServlet Class
public class DisplayHeader extends HttpServlet {
// Method for handling GET method requests
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
// Set response content type
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String title = "HTTP Header Request Example - Rookie Tutorial Example";
String docType =
"<!DOCTYPE html> \n";
out.println(docType +
"<html>\n" +
"<head><meta charset=\"utf-8\"><title>" + title + "</title></head>\n"+
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n" +
"<table width=\"100%\" border=\"1\" align=\"center\">\n" +
"<tr bgcolor=\"#949494\">\n" +
"<th>Header name</th><th>Header value</th>\n"+
"</tr>\n");
Enumeration headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
String paramName = (String)headerNames.nextElement();
out.print("<tr><td>" + paramName + "</td>\n");
String paramValue = request.getHeader(paramName);
out.println("<td> " + paramValue + "</td></tr>\n");
}
out.println("</table>\n</body></html>");
}
// Method for handling POST method requests
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
The above test examples are located under the TomcatTest project, and the corresponding web.xml
configured to:
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<!-- Class name -->
<servlet-name>DisplayHeader</servlet-name>
<!-- The package it is in -->
<servlet-class>com.runoob.test.DisplayHeader</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayHeader</servlet-name>
<!-- Visited website -->
<url-pattern>/TomcatTest/DisplayHeader</url-pattern>
</servlet-mapping>
</web-app>
Now, call the Servlet above to access http://localhost:8080/TomcatTest/DisplayHeader, the following results will be generated: