English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JSP Client Request

When a browser requests a webpage, it sends a series of information that cannot be directly read to the network server, as this information is transmitted as part of the HTTP header. You can refer to the HTTP protocol for more information.

The table below lists some important contents of browser-side information headers, which will be frequently encountered in future network programming:

InformationDescription
Accept Specifies the MIME types that the browser or other client can handle. Its value is usually image/png or image/jpeg
Accept-Charset Specifies the character set to be used by the browser. For example, ISO-8859-1
Accept-Encoding Specifies the encoding type. Its value is usually gzip orcompress
Accept-Language Specifies the preferred language of the client. The servlet will return the result set formed by the current language in priority if the servlet supports this language. For example, en, en-us, ru, etc.
Authorization Identifies different users when accessing password-protected web pages
Connection Indicates whether the client can handle HTTP persistent connections. Persistent connections allow the client or browser to retrieve multiple files in a single request.Keep-Alive Indicates that a persistent connection is enabled
Content-Length Only applicable to POST requests, indicating the number of bytes of POST data
Cookie Returns the cookies sent to the browser previously to the server
Host Indicates the hostname and port number in the original URL
If-Modified-Since Indicates that the client needs the web page only when the web page is modified on the specified date. The server sends304Code to the client, indicating that there are no updated resources
If-Unmodified-Since With If-Modified-Since conversely, the operation will only be successful if the document has not been modified after the specified date
Referer Indicates the URL of the referenced page. For example, if you are on the page1Clicked on a link to the page2If so, the page1The URL will be included in the browser's request for the page2The information headers
User-Agent Used to distinguish requests sent by different browsers or clients and return different content for different types of browsers

HttpServletRequest class

The request object is an instance of the javax.servlet.http.HttpServletRequest class. Whenever a client requests a page, the JSP engine generates a new object to represent this request.

The request object provides a series of methods to obtain HTTP header information, including form data, cookies, HTTP methods, and so on.

The following will introduce some commonly used methods to obtain HTTP header information in JSP programming. Detailed content can be found in the table below:

Serial numberMethod& Description
1Cookie[] getCookies() Returns the array of all cookies from the client
2Enumeration getAttributeNames() Returns the collection of all attribute names of the request object
3Enumeration getHeaderNames() Returns the collection of all HTTP header names
4Enumeration getParameterNames() Returns the collection of all parameters in the request
5HttpSession getSession() Returns the session object corresponding to the request; if there is none, it creates one
6HttpSession getSession(boolean create) Returns the session object corresponding to the request; if there is none and the parameter create is true, it returns a new session object
7Locale getLocale() Returns the Locale object of the current page, which can be set in the response
8Object getAttribute(String name) Returns the value of the attribute named name, or null if it does not exist.
9ServletInputStream getInputStream() Returns the input stream of the request
10String getAuthType() Returns the name of the authentication scheme used to protect the servlet, such as "BASIC" or "SSL" or null if the JSP has not set protection measures
11String getCharacterEncoding() Returns the name of the character encoding set of the request
12String getContentType() Returns the MIME type of the request body, or null if unknown
13String getContextPath() Returns the context path specified in the request URI
14String getHeader(String name) Returns the information header specified by name
15String getMethod() Returns the HTTP method of this request, such as GET, POST, or PUT
16String getParameter(String name) Returns the parameter specified by name in this request, or null if it does not exist
17String getPathInfo() Returns any additional path related to this request URL
18String getProtocol() Return the protocol name and version used by this request
19String getQueryString() Return the query string included in this request URL
20String getRemoteAddr() Return the IP address of the client
21String getRemoteHost() Return the full name of the client
22String getRemoteUser() Return the user authenticated by the client through login, or return null if the user is not authenticated
23String getRequestURI() Return the URI of the request
24String getRequestedSessionId() Return the session ID specified by the request
25String getServletPath() Return the requested servlet path
26String[] getParameterValues(String name) Return all values of the parameter with the specified name, or return null if it does not exist
27boolean isSecure() Return whether the request uses an encrypted channel, such as HTTPS
28int getContentLength() Return the number of bytes contained in the request body, or return null if unknown-1
29int getIntHeader(String name) Return the value of the request information header with the specified name
30int getServerPort() Return the server port number

Example of HTTP information headers

In this example, we will use the getHeaderNames() method of the HttpServletRequest class to read HTTP information headers. This method returns the current HTTP request headers in the form of an enumeration.

After obtaining the Enumeration object, use the standard way to traverse the Enumeration object, use the hasMoreElements() method to determine when to stop, and use the nextElement() method to obtain the name of each parameter.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Basic Tutorial Website(oldtoolbag.com)</title>
</head>
<body>
<h2>HTTP 头部请求示例</h2>
<table width="100%" border="1" align="center">
<tr bgcolor="#949494">
<th>Header Name</th><th>Header Value(s)</th>
</tr>
<%
   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>
");
   }
%>
</table>
</body>
</html>

Accessing main.jsp will result in the following:

You can try other methods of HttpServletRequest class in the above code.