JSP Client Request
When a browser requests a web page, it sends a series of messages that cannot be read directly because they are transmitted as part of the HTTP header. You can refer to the HTTP protocol for more information.
The following table lists some important contents of the browser's request headers, which will be frequently encountered in future web programming:
Information | Description |
---|---|
Accept | Specifies the MIME types that the browser or client can handle. Its value is usually image/png or image/jpeg |
Accept-Charset | Specifies the character set that the browser wants to use, such as ISO-8859-1 |
Accept-Encoding | Specifies the encoding type. Its value is usually gzip or compress |
Accept-Language | Specifies the client's preferred language. The servlet will prioritize returning results in this language if the servlet supports it. 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 fetch multiple files in one request. Keep-Alive indicates enabling persistent connections |
Content-Length | Applies only to POST requests, indicating the number of bytes of POST data |
Cookie | Returns cookies previously sent to the browser back to the server |
Host | Specifies the host name and port number from the original URL |
If-Modified-Since | Indicates that the client only needs the page if it has been modified after the specified date. The server sends a 304 code to the client, indicating no updated resources |
If-Unmodified-Since | Opposite to If-Modified-Since, the operation will only succeed if the document has not been modified since the specified date |
Referer | Marks 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 request header for Page 2 |
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 headers, including form data, cookies, HTTP methods, etc.
The following table introduces some commonly used methods for obtaining HTTP headers in JSP programming. For detailed content, see the table below:
Number | Method & Description |
---|---|
1 | Cookie[] getCookies() Returns an array of all cookies for the client |
2 | Enumeration getAttributeNames() Returns a collection of all attribute names for the request object |
3 | Enumeration getHeaderNames() Returns a collection of all HTTP header names |
4 | Enumeration getParameterNames() Returns a collection of all parameters in the request |
5 | HttpSession getSession() Returns the session object corresponding to the request, or creates one if it does not exist |
6 | HttpSession getSession(boolean create) Returns the session object corresponding to the request, or creates a new one if it does not exist and the create parameter is true |
7 | Locale getLocale() Returns the Locale object for the current page, which can be set in the response |
8 | Object getAttribute(String name) Returns the value of the attribute with the name, or null if it does not exist |
9 | ServletInputStream getInputStream() Returns the input stream for 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 is not protected |
11 | String getCharacterEncoding() Returns the name of the character encoding set for 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 information 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 path related to this request URL |
18 | String getProtocol() returns the protocol name and version used in this request |
19 | String getQueryString() returns the query string included in this request URL |
20 | String getRemoteAddr() returns the client's IP address |
21 | String getRemoteHost() returns the client's fully qualified name |
22 | String getRemoteUser() returns the user authenticated by the client, or null if the user is not authenticated |
23 | String getRequestURI() returns the request URI |
24 | String getRequestedSessionId() returns the session ID specified by the 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 is using an encrypted channel, such as HTTPS |
28 | int getContentLength() returns the number of bytes 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 headers of the current HTTP request as an enumeration.
After obtaining the Enumeration object, iterate through it using standard methods: hasMoreElements() determines when to stop, and nextElement() gets each parameter name.
<%@ 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>tutorialpro.org(tutorialpro.org)</title>
</head>
<body>
<h2>HTTP Header Request Example</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>
Accessing main.jsp will yield the following result:
You can try other methods of the HttpServletRequest class in the above code.