Open In App

Servlet – Client HTTP Request

Last Updated : 30 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

  • Cookie[] getCookies(): Returns all the cookie objects that the client has sent with the request
  • Enumeration getAttributeNames(): Returns an Enumeration containing the names of the attributes available to this request.
  • Enumeration getHeaderNames(): Returns an Enumeration of all the header names this request contains.
  • Enumeration getParameterNames(): Returns an Enumeration of String Objects which contains the names of the parameters of the current request.
  • HTTPSession getSession(): Returns the session which is associated with this request, if the request does not have a session, then it creates a session for that request.
  • Object getAttribute(String name): returns the value of the named attribute as an object, or null if no attribute with the given name exists.
  • String getContentType(): returns the MIME type of the request, or null if the type is not known.
  • String getMethod(): returns the name of the HTTP method with which the request has been made. The HTTP methods that are used are GET, POST.

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

Example

Java




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>");
    }
}




Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads