Servlets - Client HTTP Request

When a program demands for a site page, it sends parcel of data to the web server which can't be perused straightforwardly in light of the fact that this data travel as a piece of header of HTTP ask.

Following is the vital header data which originates from program side and you would utilize all around much of the time in web programming −

Sr.No. Header & Description


This header indicates the MIME types that the program or different customers can deal with. Estimations of image/png or image/jpeg are the two most regular possibilities.



This header determines the character sets the program can use to show the data. For instance ISO-8859-1.



This header indicates the kinds of encodings that the program realizes how to deal with. Estimations of gzip or compress are the two most basic possibilities.



This header indicates the customer's favored dialects in the event that the servlet can create results in more than one language. For instance en, en-us, ru, etc



This header is utilized by customers to distinguish themselves while getting to secret phrase secured Web pages.



This header shows whether the customer can deal with constant HTTP associations. Persevering associations license the customer or other program to recover different documents with a solitary demand. An estimation of Keep-Alive implies that relentless associations ought to be used.



This header is appropriate just to POST asks for and gives the span of the POST information in bytes.



This header returns treats to servers that recently sent them to the browser.



This header indicates the host and port as given in the first URL.



This header demonstrates that the customer needs the page just on the off chance that it has been changed after the predetermined date. The server sends a code, 304 which implies Not Modified header if no more up to date result is available.



This header is the invert of If-Modified-Since; it indicates that the activity ought to succeed just if the report is more seasoned than the predefined date.



This header shows the URL of the alluding Web page. For instance, in the event that you are at Web page 1 and snap on a connection to Web page 2, the URL of Web page 1 is incorporated into the Referrer header when the program demands Web page 2.



This header recognizes the program or other customer making the demand and can be utilized to return diverse substance to various kinds of browsers.

Methods to peruse HTTP Header

There are following strategies which can be utilized to peruse HTTP header in your servlet program. These techniques are accessible with HttpServletRequest object

Sr.No. Method & Description

Cookie[] getCookies()

Returns a cluster containing the majority of the Cookie questions the customer sent with this request.


Enumeration getAttributeNames()

Returns an Enumeration containing the names of the ascribes accessible to this request.


Enumeration getHeaderNames()

Returns a list of all the header names this demand contains.


Enumeration getParameterNames()

Returns an Enumeration of String objects containing the names of the parameters contained in this request


HttpSession getSession()

Returns the present session related with this demand, or if the demand does not have a session, makes one.


HttpSession getSession(boolean create)

Returns the present HttpSession related with this demand or, if there is no present session and estimation of make is valid, restores another session.


Locale getLocale()

Returns the favored Locale that the customer will acknowledge content in, in light of the Accept-Language header.


Object getAttribute(String name)

Returns the estimation of the named property as an Object, or invalid if no characteristic of the given name exists.


ServletInputStream getInputStream()

Retrieves the body of the demand as double information utilizing a ServletInputStream.


String getAuthType()

Returns the name of the confirmation conspire used to secure the servlet, for instance, "Essential" or "SSL," or invalid if the JSP was not protected.


String getCharacterEncoding()

Returns the name of the character encoding utilized in the body of this request.


String getContentType()

Returns the MIME sort of the body of the demand, or invalid if the sort isn't known.


String getContextPath()

Returns the part of the demand URI that demonstrates the setting of the request.


String getHeader(String name)

Returns the estimation of the predefined ask for header as a String.


String getMethod()

Returns the name of the HTTP strategy with which this demand was made, for instance, GET, POST, or PUT.


String getParameter(String name)

Returns the estimation of a demand parameter as a String, or invalid if the parameter does not exist.


String getPathInfo()

Returns any additional way data related with the URL the customer sent when it made this request


String getProtocol()

Returns the name and form of the convention the request.


String getQueryString()

Returns the question string that is contained in the demand URL after the path.


String getRemoteAddr()

Returns the Internet Protocol (IP) address of the customer that sent the request.


String getRemoteHost()

Returns the completely qualified name of the customer that sent the request.


String getRemoteUser()

Returns the login of the client making this demand, if the client has been validated, or invalid if the client has not been authenticated.


String getRequestURI()

Returns the piece of this current demand's URL from the convention name up to the inquiry string in the principal line of the HTTP request.


String getRequestedSessionId()

Returns the session ID indicated by the client.


String getServletPath()

Returns the piece of this present demand's URL that calls the JSP.


String[] getParameterValues(String name)

Returns a variety of String objects containing the majority of the qualities the given demand parameter has, or invalid if the parameter does not exist.


boolean isSecure()

Returns a Boolean demonstrating whether this demand was made utilizing a safe channel, for example, HTTPS.


int getContentLength()

Returns the length, in bytes, of the demand body and made accessible by the information stream, or - 1 if the length isn't known.


int getIntHeader(String name)

Returns the estimation of the predetermined demand header as an int.


int getServerPort()

Returns the port number on which this demand was received.

HTTP Header Request Example

Following is the precedent which utilizes getHeaderNames() strategy for HttpServletRequest to peruse the HTTP header data. This strategy restores an Enumeration that contains the header data related with the present HTTP request.

Once we have an Enumeration, we can circle down the Enumeration in the standard way, utilizing hasMoreElements() technique to decide when to stop and utilizing nextElement() strategy to get every parameter name

//Import required java libraries 


import javax.servlet.*; 

import javax.servlet.http.*; 

import java.util.*; 

//Extend HttpServlet class 

public class DisplayHeader extends HttpServlet { 

//Method to deal with GET strategy request. 

public void doGet(HttpServletRequest request, HttpServletResponse response) 

throws ServletException, IOException { 

//Set reaction content type 


PrintWriter out = response.getWriter(); 

String title = "HTTP Header Request Example"; 

String docType = 

"<!doctype html open \"-/w3c//dtd html 4.0 " + "transitional//en\">\n"; 

out.println(docType + 

"<html>\n" + 

"<head><title>" + title + "</title></head>\n"+ 

"<body bgcolor = \"#f0f0f0\">\n" + 

"<h1 adjust = \"center\">" + title + "</h1>\n" + 

"<table width = \"100%\" fringe = \"1\" adjust = \"center\">\n" + 

"<tr bgcolor = \"#949494\">\n" + 

"<th>Header Name</th><th>Header Value(s)</th>\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"); 




//Method to deal with POST strategy request. 

public void doPost(HttpServletRequest request, HttpServletResponse response) 

throws ServletException, IOException { 

doGet(request, response); 



Now calling the above servlet would create the accompanying outcome −

HTTP Header Request Example

Header Name Header Value(s)
accept */*
accept-language en-us
user-agent Mozilla/4.0 (perfect; MSIE 7.0; Windows NT 5.1; Trident/4.0; InfoPath.2; MS-RTC LM 8)
accept-encoding gzip, deflate
host localhost:8080
connection Keep-Alive
cache-control no-cache

© Javacodegeeks 2018 -
All Right Reserved and you agree to have read and accepted our term and condition.

Python 3