JSP/Servlet | HTTPServletRequest Methods

The request object is used to get values, cookies, headers, and client information sent with an HTTP request.

This page summarizes the methods of the Request object.

Before You Start

The return value examples assume that the following URL was received.

http://www.devkuma.com:8080/app/request.jsp?name=devkuma

Main Information

Method Example return value Description
request.getRequestURL() http://www.devkuma.com:8080/app/request.jsp
request.getRequestURI() /app/request.jsp The remaining address and parameters after excluding the scheme, server name, and port number from the URL
request.getScheme() http Protocol such as http, https, or ftp
request.getServerName() www.devkuma.com Server name
request.getServerPort() 8080 Server port
request.getContextPath() /app Context path
request.getMethod() GET HTTP method such as GET or POST
request.isSecure() false Whether SSL security is enabled. Returns true/false depending on whether a secure channel such as https is used
request.getLocale() ko_KR Locale information
request.getProtocol() HTTP/1.1 Protocol in use. Protocol/major version.minor version

Basic Local Information (Server Information)

Method Example return value Description
request.getLocalAddr() 127.0.0.1
request.getLocalName() localhost
request.getLocalPort() 8080

Remote Information (Client Information)

Method Example return value Description
request.getRemoteAddr() 127.0.0.1
request.getRemoteHost() 127.0.0.1
request.getRemotePort() 60361

Session ID

Method Example return value Description
request.getRequestedSessionId() 39AE7AC19BC174D803C2A7BD8638E382 Session ID
request.getSession().getId() 39AE7AC19BC174D803C2A7BD8638E382 Session ID
request.isRequestedSessionIdFromCookie() true Whether the session ID was provided by a cookie
request.isRequestedSessionIdFromURL() false Whether the session ID was provided as part of the URL
request.isRequestedSessionIdValid() true Whether the session is still valid

Parameter Information

Method Example return value Description
request.getParameterNames() java.util.Enumeration Returns all parameter names.
request.getParameter(name) devkuma Returns the parameter value by parameter name.

Display All Parameter Values

Enumeration params = request.getParameterNames();
while (params.hasMoreElements()) {
    String name = (String) params.nextElement();
    String value = request.getParameter(name);
    logger.debug(name + "=" + value);
}

The displayed return value is as follows.

name=devkuma

The return value is displayed as above because the request was made as /app/request.jsp?name=devkuma.

Method Example return value Description
request.getCookies() Array Returns all cookie values.
Cookie cookies[] = request.getCookies();
for(int i = 0; i < cookies.length; i++) {
    String name = cookies[i].getName();
    String value = cookies[i].getValue();
    logger.debug(name + "=" + value);
}

The displayed return value is as follows.

JSESSIONID=39AE7AC19BC174D803C2A7BD8638E382

In this return example, only JSESSIONID is displayed because no additional cookie value was set.

Attribute

Method Example return value Description
request.getAttributeNames() Array Returns all attribute names.
request.getAttribute(name) Returns the value of the attribute.

Display All Attribute Values

Enumeration<String> attrs = request.getAttributeNames();
while (attrs.hasMoreElements()) {
    String name = (String)attrs.nextElement();
    String value = (String)request.getAttribute(name);
    logger.debug(name + " : " + value);
}

Header Information

Method Example return value Description
request.getHeaderNames() java.util.Enumeration Returns all header names.
request.getHeader(“user-agent”) Mozilla/5.0 …. Returns a header value by header name.

Display All Header Values

Enumeration<String> headers = request.getHeaderNames();
while (headers.hasMoreElements()) {
    String name = (String) headers.nextElement();
    String value = request.getHeader(name);
    logger.debug(name + "=" + value);
}

The displayed return values are as follows.

host=www.devkuma.com:8080
connection=keep-alive
cache-control=max-age=0
upgrade-insecure-requests=1
user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
accept=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
accept-encoding=gzip, deflate
accept-language=ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7,ja;q=0.6,es;q=0.5
cookie=JSESSIONID=26356CFACA2FFA1B4AA25C75D291E028

Request Body

You can get data passed in the request body as follows.

DataInputStream dis = new DataInputStream(request.getInputStream());
String str = null
while ((str = dis.readLine()) != null) {
    logger.debug(new String(str.getBytes("ISO-8859-1"), "utf-8") + "/n");
    // Korean text sent as euc-kr is broken.
}