JSP client request
When the browser requests a web page, it sends a series of information to the network server that cannot be read directly because the information is transmitted as part of the HTTP header. 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 | Description |
---|---|
Specifies the MIME types that the browser or other client can handle. Its value is usually | image/png or image/jpeg |
Specifies the character set to be used by the browser. For example, ISO-8859-1 | ## Accept-Encoding |
gzip | orcompress## Accept-Language |
Authorization | |
Connection | |
Content-Length | Only applicable to POST requests, indicating the number of bytes of POST data |
Cookie | Return cookies previously sent to the browser to the server |
Host | Indicate the host name 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 after the specified date |
Marks the URL of the referenced page. For example, if you are on page 1 and then click on a link to page 2, then the URL of page 1 will be included in the header of the browser request for page 2 | |
Used to distinguish requests sent by different browsers or clients and return different content to different types of browsers |
Serial Number | Method& describe |
---|---|
1 | Cookie[] getCookies() |
2 | Enumeration getAttributeNames() |
3 | Enumeration getHeaderNames() |
4 | Enumeration getParameterNames() |
5 | HttpSession getSession() |
6 | HttpSession getSession(boolean create) |
7 | Locale getLocale() |
8 | Object getAttribute(String name) |
9 | ServletInputStream getInputStream() |
10 | String getAuthType() |
11 | String getCharacterEncoding() |
12 | String getContentType() |
13 | String getContextPath() |
14 | String getHeader(String name) |
15 | String getMethod() |
16 | String getParameter(String name) |
17 | String getPathInfo() |
18 | String getProtocol() |
19 | String getQueryString() |
contained in this request URL 20 | String getRemoteAddr() |
21 | String getRemoteHost() |
22 | String getRemoteUser() |
23 | String getRequestURI() |
24 | String getRequestedSessionId() |
25 | String getServletPath() |
26 | String[] getParameterValues(String name) |
27 | boolean isSecure() |
28 | int getContentLength() |
29 | int getIntHeader(String name) |
30 | int getServerPort() |
HTTP header example
In this example, we will use the getHeaderNames() method of the HttpServletRequest class to read the HTTP header. 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, 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>php中文网(php.cn)</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>\n"); } %> </table> </body> </html>
Access main.jsp, you will get the following results:
You can try other methods of the HttpServletRequest class in the above code.