Pr.Pg Next Pg

Methods to read HTTP Header tutorials

  • There are following methods which can be used to read HTTP header in your servlet program. These method are available with HttpServletRequest object.

 

S.N.

Method

Description

1

Cookie[] getCookies()

 

The getCookies method returns the contents of the Cookie
header, parsed and stored in an array of Cookie objects.

2

Enumeration getAttributeNames()

 

Returns an Enumeration containing the names of the attributes available to this request.

3

Enumeration getHeaderNames()

 

Rather than looking up one particular header, you can use the getHeaderNames method to get an Enumeration of all header names received on this particular request.

4

Enumerationget ParameterNames()

 

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

5

HttpSession getSession()

 

Returns the current session associated with this request, or if the request does not have a session, creates one.

6

HttpSession getSession(boolean create)

 

Returns the current HttpSession associated with this request or, if if there is no current session and create is true, returns a new session.

7

Locale getLocale()

 

Returns the preferred Locale that the client will accept content in, based on the Accept-Language header

8

Object getAttribute(String name)

 

Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.

9

ServletInputStream getInputStream()

 

Retrieves the body of the request as binary data using a ServletInputStream.

10

String getAuthType()

The getAuthType methods break the Authorization header into its component pieces.

11

String getCharacterEncoding()

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

12

String getContentType()

Returns the MIME type of the body of the request, or null if the type is not known.

13

String getContextPath()

Returns the portion of the request URI that indicates the context of the request.

14

String getHeader(String name)

Returns the value of the specified request header as a String.

15

String getMethod()

 

Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT.

16

String getParameter(String name)

 

Returns the value of a request parameter as a String, or null if the parameter does not exist.

17

String getPathInfo()

Returns any extra path information associated with the URL the client sent when it made this request.

18

String getProtocol()

Returns the name and version of the protocol the request.

19

String getQueryString()

 

Returns the query string that is contained in the request URL after the path.

20

String getRemoteAddr()

 

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

21

String getRemoteHost()

 

Returns the fully qualified name of the client that sent the request.

22

String getRemoteUser()

 

Returns the login of the user making this request, if the user has been authenticated, or null if the user has not been authenticated.

23

String getRequestURI()

 

Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request.

24

String getRequestedSessionId()

Returns the session ID specified by the client.

25

String getServletPath()

Returns the part of this request's URL that calls the JSP.

26

String[] getParameterValues(String

name)

 

Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist.

27

boolean isSecure()

 

Returns a boolean indicating whether this request was made using a secure channel, such as HTTPS.

28

int getContentLength()

 

The getContentLength method returns the value of the
Content-Length header (as an int).

29

int getIntHeader(String name)

Returns the value of the specified request header as an int.

30

int getServerPort()

Returns the port number on which this request was received.


 

Example: Servlet program to show HTTP Header Request

Sam Sir

//Program name ClientRequestHeaderInfo.java

// Program to show HTTP Header Request

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.util.*;

public class ClientRequestHeaderInfo extends HttpServlet

{

public void doGet(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException

{

response.setContentType("text/html");

PrintWriter out = response.getWriter();

String title = "Servlet Example: Showing Request Headers";

out.println("<HTML>\n" +

"<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +

"<BODY BGCOLOR=\"#FDF5E6\">\n" +

"<H1 ALIGN=\"CENTER\">" + title + "</H1>\n" +

"<B>Request Method: </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()) {

String headerName = (String)headerNames.nextElement();

out.println("<TR><TD>" + headerName);

out.println(" <TD>" + request.getHeader(headerName));

}

out.println("</TABLE>\n</BODY></HTML>");

}

}

Output

HTTP request header

Pr.Pg border                                              Next Pg