Open In App

Servlet – Client HTTP Request

When the user wants some information, he/she will request the information through the browser. Then the browser will put a request for a web page to the webserver. It sends the request information to the webserver which cannot be read directly because this information will be part of the header of the HTTP request.

Header and Descriptions

Header Name

Description

Accept

Specifies the MIME types (also called Internet Media types or Content types which describe the media type of content served by web servers or applications) that the browser or other clients can handle.

Accept-Charset Specifies the character sets that the browser can use to display the information
Accept-Encoding Specifies the types of encodings that the browser supports.
Accept-Language Specifies the client’s preferred languages in case the servlet can produce results in more than one language
Authorization This is used by clients to identify themselves when accessing password-protected web pages
Connection This indicates whether the client can handle HTTP connections or not. 
Content-Length This header is applicable only for POST requests and gives the size of the POST data
Cookie This returns cookies to servers that are previously sent to the browser
Host This specifies the host and port which are given in the original URL

Methods which are used to read HTTP Header

There are several other methods that are used to read the HTTP Header.

Example




package com.headers;
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;
// servlet implementation;
@WebServlet("/Headers")
public class Headers extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public Headers() { super(); }
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response)
        throws ServletException, IOException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String str = "Display Header Request";
        out.println(
            "<BODY BGCOLOR=\"#FF5732\">\n"
            + "<H1 ALIGN=CENTER>" + str + "</H1>\n"
            + "<B><center>Request Method: <center></B>"
            + request.getMethod() + "<BR>\n"
            + "<B>Request URI: </B>"
            + request.getRequestURI() + "<BR>\n"
            + "<B>Request Protocol: </B>"
            + request.getProtocol() + "<BR><BR>\n"
            + "<TABLE BORDER=1 ALIGN=CENTER>\n"
            + "<TR BGCOLOR=\"#FFAD00\">\n"
            + "<TH>Header Name<TH>Header Value");
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            Sting headerName
                = (String)headerNames.nextElement();
            out.println("<TR><TD>" + headerName);
            out.println("<TD>"
                        + request.getHeader(headerName));
        }
        out.println("<TABLE>\n</BODY></HTML>");
    }
}


Article Tags :