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. You can check HTTP Protocol for more data on this.

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
1

Accept

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.

2

Accept-Charset

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

3

Accept-Encoding

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.

4

Accept-Language

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

5

Authorization

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

6

Connection

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.

7

Content-Length

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

8

Cookie

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

9

Host

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

10

If-Modified-Since

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.

11

If-Unmodified-Since

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.

12

Referer

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.

13

User-Agent

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
1

Cookie[] getCookies()

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

2

Enumeration getAttributeNames()

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

3

Enumeration getHeaderNames()

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

4

Enumeration getParameterNames()

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

5

HttpSession getSession()

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

6

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.

7

Locale getLocale()

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

8

Object getAttribute(String name)

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

9

ServletInputStream getInputStream()

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

10

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.

11

String getCharacterEncoding()

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

12

String getContentType()

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

13

String getContextPath()

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

14

String getHeader(String name)

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

15

String getMethod()

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

16

String getParameter(String name)

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

17

String getPathInfo()

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

18

String getProtocol()

Returns the name and form of the convention the request.

19

String getQueryString()

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

20

String getRemoteAddr()

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

21

String getRemoteHost()

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

22

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.

23

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.

24

String getRequestedSessionId()

Returns the session ID indicated by the client.

25

String getServletPath()

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

26

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.

27

boolean isSecure()

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

28

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.

29

int getIntHeader(String name)

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

30

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 java.io.*; 

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 

response.setContentType("text/html"); 

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"+ 

"</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 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