Whenever we pass data from the client to the Server, then many additional information will be passed from the client to the Server. This additional information is called HTTP Request Headers and will not be visible directly for the users.
The purpose of the HttpRequest object is to represent the HTTP request a browser sends to your web application. Thus, anything the browser may send, is accessible via the HttpRequest.

This Request Headers information helps developers in developing application more efficiently.
HttpServletRequest object provides various methods to read HTTP Request Header information.
Some of the HTTP Request Header methods :

Parameters
The request parameters are parameters that are sent from the browser along with the request. Request parameters are typically sent as part of the URL (in the “query string”) – in case of GET, or as part of the body of an HTTP request – in case of POST. For instance:
http://example.com/somePage.html?param1=hello¶m2=world
Notice the “query string” part of the URL: ?param1=hello¶m2=world This part contains two parameters with parameter values:
param1=hello param2=world
You can access these parameters from the HttpRequest object like this:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String param1 = request.getParameter("param1");
String param2 = request.getParameter("param2");
}
You would use the same code, if the request parameters were sent in the body part of the HTTP request. If no parameter exists with the given name, null is returned.
In general, if the browser sends an HTTP GET request, the parameters are included in the query string in the URL. If the browser sends an HTTP POST request, the parameters are included in the body part of the HTTP request.
Headers
The request headers are name-value pairs sent by the browser along with the HTTP request. The request headers contain information about e.g. what browser software is being used, what file types the browser is capable of receiving etc. In short, at lot of meta data around the HTTP request.
You can access the request headers from the HttpRequest object like this:
String contentLength = request.getHeader("Content-Length");
This example reads the Content-Length header sent by the browser.
The Content-Length header contains the number of bytes sent in the HTTP request body, in case the browser sends an HTTP POST request. If the browser sends an HTTP GET request, the Content-Lengthheader is not used, and the above code will return null.
In general, If no header exists with the name passed to getHeader(), null is returned.
Example : Reading HTTP Request Headers
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!Doctype HTML>");
out.println("<html>");
out.println("<head>");
out.println("<title>Showing all Request Headers</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>All Request Headers </h1>");
out.println("<p><b>Request Method : </b>" + request.getMethod()); // GET or POST, etc
out.println("<p><b>Request URI :</b>" + request.getRequestURI());
out.println("<p><b>Request Protocol : <b>" + request.getProtocol()); // HTTP/1.1
Enumeration<String> headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
out.println("<p><b>" + headerName + ":</b>" + request.getHeader(headerName) + "</p>");
}
out.println("</body>");
out.println("</html>");
out.close();
}
InputStream
If the browser sends an HTTP POST request, request parameters and other potential data is sent to the server in the HTTP request body. It doesn’t have to be request parameters that is sent in the HTTP request body. It could be pretty much any data, like a file or a SOAP request (web service request).
To give you access to the request body of an HTTP POST request, you can obtain an InputStreampointing to the HTTP request body. Here is how it is done:
InputStream requestBodyInput = request.getInputStream();
NOTE: You will have to call this method before calling any getParameter() method, because calling the getParameter() method on an HTTP POST request will cause the servlet engine to parse the HTTP request body for parameters. Once parsed, you cannot access the body as a raw stream of bytes anymore.
What you do with the data read from the InputStream is up to you. The Servlet engine does not help you parse or interpret that data. You just get it raw.
HTTPResponse
The purpose of the HttpResponse object is to represent the HTTP response your web application sends back to the browser, in response to the HTTP request the browser send to your web application.

Some of the HTTP Response Header methods :

Writing HTML
To send HTML back to the browser, you have to obtain the a PrintWriter from the HttpResponse object. Here is how:
PrintWriter out = response.getWriter();
out.println("<html><body>GET/POST response</body></html>");
Headers
Just like the request object, the HttpRequest can contain HTTP headers. Headers must be set before any data is written to the response. You set a header on the response object like this:
response.setHeader("Header-Name", "Header Value");
Ex:
response.setHeader("Content-Encoding", "gzip");
As you can see, a response header is a name-value pair.
Content-Type
The Content-Type header is a response header that tells the browser the type of the content you are sending back to it. For instance, the content type for HTML is text/html. Similarly, if what you send back to the browser is plain text, you use the content type text/plain.
Here is how you set the Content-Type header on the HttpResponse object:
response.setHeader("Content-Type", "text/html");
response.setContentType("text/html");
response.setContentType("application/vnd.ms-excel");
Writing Text
You can write text back to the browser instead of HTML, like this:
response.setHeader("Content-Type", "text/plain");
PrintWriter writer = response.getWriter();
writer.write("This is just plain text");
First the Content-Type header is set to text/plain. Then a plain text string is written to the writer obtained from the response object.
Content-Length
The Content-Length header tells the browser how many bytes your Servlet is sending back. If you are sending binary data back you need to set the content length header. Here is how:
response.setHeader("Content-Length", "31642");
Writing Binary Data
You can also write binary data back to the browser instead of text. For instance, you can send an image back, a PDF file or a Flash file or something like that.
Again, you will first have to set the Content-Type header to the type matching the data you are sending back. For instance, the content type for a PNG image is image/png.
You can search for “mime types” in your favourite search engine to find a list of mime types (content types), so you can find the mime type for the content you are sending back.
In order to write binary data back to the browser you cannot use the Writer obtained from response.getWriter(). Afterall, Writer‘s are intended for text.
Instead you have to use the OutputStream obtained from the response.getOutputStream() method. Here is how:
OutputStream outputStream = response.getOutputStream(); outputStream.write(...);
Redirecting to a Different URL
You can redirect the browser to a different URL from your servlet. You cannot send any data back to the browser when redirecting. Here is how you redirect:
response.sendRedirect("http://google.com");
Refresh page every 3 seconds :
response.setIntHeader("Refresh", 3);
Setting HTTP Status Code :
HTTP Status codes are the standard response codes given by the web server on the internet. These codes help to identify the cause of the problem when a web page or other resources do not load properly.

Whenever the client sends a request, and if the server successfully received, understood, and accepted, web server will return the status code in 200 series along with the response.
Whenever the client side error occurs, the web server will return the status code in the 400 series.
Whenever the client sends a request to the server and while processing that request, if any exceptions are raised at the Server side, then the web server will return status code in the 500 series.

Example :
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String siteName = "http://www.pluralsight.com";
response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
response.setHeader("Location", siteName);
// response.sendRedirect(siteName);
// response.sendError(407,"This Page Requires Authentication.");
}