JSP Client Request
When a browser requests a web page, it sends a series of pieces of information to the Web server that cannot be read directly, because this information is transmitted as part of the HTTP header. You can consult the HTTP protocol for more information.
The following table lists some important contents of the browser-side header, which you will often encounter in future web programming:
| Information | Description |
|---|---|
| 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 the browser is to use. 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 preferentially return results in that language if it supports it. For example, en, en-us, ru, and so on |
| Authorization | Identifies different users when accessing password-protected web pages |
| Connection | Indicates whether the client can handle HTTP persistent connections. A persistent connection allows a client or browser to obtain multiple files in a single request.Keep-Alive Indicates that persistent connections are enabled |
| Content-Length | Applies only to POST requests; indicates the number of bytes of POST data |
| Cookie | Returns the cookies previously sent to the browser back to the server |
| Host | Indicates the host name and port number in the original URL |
| If-Modified-Since | Indicates that the client only needs the page if it has been modified since the specified date. The server sends a 304 code to the client to indicate that there is no newer resource. |
| If-Unmodified-Since | The opposite of If-Modified-Since; the operation succeeds only if the document has not been modified since the specified date |
| Referer | Indicates the URL of the referring page. For example, if you are on page 1 and click a link to page 2, the URL of page 1 will be included in the header of the browser's request for page 2. |
| User-Agent | Used to distinguish requests sent by different browsers or clients, and to return different content to different types of browsers |
The HttpServletRequest class
The request object is an instance of the javax.servlet.http.HttpServletRequest class. Whenever a client requests a page, the JSP engine creates a new object to represent that request.
The request object provides a series of methods for obtaining HTTP headers, including form data, cookies, HTTP methods, and so on.
Next we will introduce some methods commonly used in JSP programming for obtaining HTTP headers. See the table below for details:
| No. | Method**& **Description |
|---|---|
| 1 | Cookie[] getCookies() Returns an array of all Cookies from the client |
| 2 | Enumeration getAttributeNames() Returns a collection of all attribute names of the request object |
| 3 | Enumeration getHeaderNames() Returns a collection of the names of all HTTP headers |
| 4 | Enumeration getParameterNames() Returns a collection of all parameters in the request |
| 5 | HttpSession getSession() Returns the session object corresponding to the request; if there is none, creates one |
| 6 | HttpSession getSession(boolean create) Returns the session object corresponding to the request; if there is none and the parameter create is true, returns a new session object |
| 7 | Locale getLocale() Returns the Locale object of the current page, which can be set in the response |
| 8 | Object getAttribute(String name) Returns the value of the attribute named name, or null if it does not exist. |
| 9 | ServletInputStream getInputStream() Returns the input stream of the request |
| 10 | String getAuthType() Returns the name of the authentication scheme used to protect the servlet, such as "BASIC" or "SSL", or null if the JSP has no protection set |
| 11 | String getCharacterEncoding() Returns the name of the character encoding of the request |
| 12 | String getContentType() Returns the MIME type of the request body, or null if unknown |
| 13 | String getContextPath() Returns the context path indicated in the request URI |
| 14 | String getHeader(String name) Returns the header specified by name |
| 15 | String getMethod() Returns the HTTP method of this request, such as GET, POST, or PUT |
| 16 | String getParameter(String name) Returns the parameter specified by name in this request, or null if it does not exist |
| 17 | String getPathInfo() Returns any extra path associated with this request URL |
| 18 | String getProtocol() Returns the name and version of the protocol used by this request |
| 19 | String getQueryString() Returns the query string contained in this request URL |
| 20 | String getRemoteAddr() Returns the IP address of the client |
| 21 | String getRemoteHost() Returns the fully qualified name of the client |
| 22 | String getRemoteUser() Returns the user authenticated through login on the client, or null if the user is not authenticated |
| 23 | String getRequestURI() Returns the URI of the request |
| 24 | String getRequestedSessionId() Returns the session ID specified by the request |
| 25 | String getServletPath() Returns the path of the requested servlet |
| 26 | String[] getParameterValues(String name) Returns all values of the parameter with the specified name, or null if it does not exist |
| 27 | boolean isSecure() Returns whether the request uses a secure channel, such as HTTPS |
| 28 | int getContentLength() Returns the number of bytes contained in the request body, or -1 if unknown |
| 29 | int getIntHeader(String name) Returns the value of the request header with the specified name |
| 30 | int getServerPort() Returns the server port number |
HTTP Header Example
In this example, we will use the getHeaderNames() method of the HttpServletRequest class to read HTTP headers. This method returns the header information of the current HTTP request as an enumeration.
After obtaining the Enumeration object, traverse it in the standard way: use the hasMoreElements() method to determine when to stop, and 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.*" %>
Runoob Tutorial(runoob.com)
HTTP 头部请求实例
Header Name Header Value(s)
<%
Enumeration headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
String paramName = (String)headerNames.nextElement();
out.print("" + paramName + " \n");
String paramValue = request.getHeader(paramName);
out.println(" " + paramValue + " \n");
}
%>
Accessing main.jsp will produce the following result:

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