When a browser requests a web page, it sends a series of information to the web server that cannot be read directly because the information is sent as part of the HTTP headers. You can check out the HTTP protocol for more information.
The following table lists some important contents of the browser-side information header. You will often see this information in future network programming:
information | describe |
---|---|
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. Such as ISO-8859-1 |
Accept-Encoding | Specify the encoding type. Its value is usually gzip or compress |
Accept-Language | Specify the client's preferred language. The servlet will give priority to returning a result set in the current language, if the servlet supports this language. Such as en, en-us, ru, etc. |
Authorization | Identify 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 fetch multiple files in one request. Keep-Alive means enabling persistent connections |
Content-Length | Applies to POST requests only, indicating the number of bytes of POST data |
Cookies | Returns cookies previously sent to the browser to the server |
Host | Indicate the hostname and port number in the original URL |
If-Modified-Since | Indicates that the client only needs this web page if it has been modified on the specified date. The server sends a 304 code to the client, indicating that there are no updated resources. |
If-Unmodified-Since | Contrary to If-Modified-Since, the operation will succeed only if the document has not been modified since the specified date |
Referer | Marks the URL of the referenced page. For example, if you are on page 1 and then click a link to page 2, the URL of page 1 will be included in the header of the browser request for page 2. |
User-Agent | Used to distinguish requests sent by different browsers or clients and return different content to different types of browsers |
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 the request.
The request object provides a series of methods to obtain HTTP information headers, including form data, cookies, HTTP methods, etc.
Next, we will introduce some methods commonly used in JSP programming to obtain HTTP information headers. Please see the table below for details:
serial number | Method & Description |
---|---|
1 | Cookie[] getCookies() returns an array of all cookies on the client |
2 | Enumeration getAttributeNames() returns a collection of all attribute names of the request object |
3 | Enumeration getHeaderNames() returns a collection of 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 not, create one. |
6 | HttpSession getSession(boolean create) returns the session object corresponding to the request. If there is none and the parameter create is true, a new session object is returned. |
7 | Locale getLocale() returns the Locale object of the current page, which can be set in response |
8 | Object getAttribute(String name) returns the attribute value named name, or null if it does not exist. |
9 | ServletInputStream getInputStream() returns the requested input stream |
10 | String getAuthType() returns the name of the authentication scheme used to protect the servlet, such as "BASIC" or "SSL" or null if JSP does not set protection measures |
11 | String getCharacterEncoding() returns the character encoding set name of request |
12 | String getContentType() returns the MIME type of the request body, or null if it is unknown. |
13 | String getContextPath() returns the context path specified in the request URI |
14 | String getHeader(String name) returns the information header specified by name |
15 | String getMethod() returns the HTTP method in 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 additional paths associated with this request URL |
18 | String getProtocol() returns the protocol name and version used by this request |
19 | String getQueryString() returns the query string contained in this request URL |
20 | String getRemoteAddr() returns the client's IP address |
twenty one | String getRemoteHost() returns the full name of the client |
twenty two | String getRemoteUser() returns the user authenticated by the client through login, or null if the user is not authenticated. |
twenty three | String getRequestURI() returns the URI of the request |
twenty four | String getRequestedSessionId() returns the session ID specified by request |
25 | String getServletPath() returns the requested servlet path |
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 an encrypted channel, such as HTTPS |
28 | int getContentLength() returns the number of bytes contained in the request body. If it is unknown, it returns -1 |
29 | int getIntHeader(String name) returns the value of the request header with the specified name |
30 | int getServerPort() returns the server port number |
In this example, we will use the getHeaderNames() method of the HttpServletRequest class to read the HTTP headers. This method returns the header information of the current HTTP request in the form of an enumeration.
After obtaining the Enumeration object, use the standard method to traverse the Enumeration object, using the hasMoreElements() method to determine when to stop, and the nextElement() method to obtain the name of each parameter.
<%@ page import="java.io.*,java.util.*" %><html><head><title>HTTP Header Request Example</title></head><body><center><h2 >HTTP Header Request Example</h2><table 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>n"); }%></table></center></body></html>
Access main.jsp, you will get the following results:
Header Name | Header Value(s) |
---|---|
accept | */* |
accept-language | en-us |
user-agent | Mozilla/4.0 (compatible; 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 |
You can try other methods of the HttpServletRequest class in the above code.