May 14, 2021 Servlet
When a browser requests a Web page, it sends specific information to the Web server that cannot be read directly because the information is transmitted as part of the header of the HTTP request. You can check the HTTP protocol for more information.
Here's important header information from the browser side that you can use frequently in web programming:
Head information | describe |
---|---|
Accept | This header specifies the MIME type that the browser or other client can process.value image/png or image/jpeg It is the most common possible value. |
Accept-Charset | This header information specifies that the browser can be used to display the character set of the information.For example, ISO-8859-1. |
Accept-Encoding | This header information specifies how the browser knows how to process the encoding type.value gzip or compress It is the most common possible value. |
Accept-Language | This header information specifies the preferred language of the client, in which case servlet produces a variety of languages.For example, En, EN-US, RU, etc. |
Authorization | This header is used to identify your identity when the client is accessing the password-protected web page. |
Connection | This header information indicates whether the client can handle a persistent HTTP connection.The persistent connection allows clients or other browsers to retrieve multiple files through a single request.value Keep-Alive It means that continuous connection is used. |
Content-Length | This header is only available for POST requests and gives the size of Post data (in bytes). |
Cookie | This header returns the cookies sent to the browser to the server. |
Host | This header information specifies the host and port in the original URL. |
If-Modified-Since | This header means that the client wants only when the page has changed after the page has been changed.If there is no new result, the server will send a 304 code, indicating Not Modified Head information. |
If-Unmodified-Since | This header information is the opposite of if-modified-since, which specifies that only the operation will succeed when the document is earlier than the specified date. |
Referer | This header information indicates the URL of the web page pointed to.For example, if you are on page 1, click on a link to the web page 2, when the browser requests page 2, the URL of the web page 1 will be included in the Referr header information. |
User-Agent | This header information identifies a request for browser or other client and can return different content to different types of browsers. |
The following method can be used to read the HTTP header in the servlet program. These methods are available through the HttpServletRequest object.
Serial number | Method & Description |
---|---|
1 |
Cookie[] getCookies()
Returns an array that contains all the cookie objects that the client sends the request. |
2 |
Enumeration getAttributeNames()
Returns an enumeration that contains the name of the attribute available to the request. |
3 |
Enumeration getHeaderNames()
Returns an enumeration that contains all the headers contained in the request. |
4 |
Enumeration getParameterNames()
Returns an enumeration of a String object, which is included in the name of the parameters included in the request. |
5 |
HttpSession getSession()
Returns the current session session associated with the request, or if you don't have a session session, you create one. |
6 |
HttpSession getSession(boolean create)
Returns the current httpsession associated with the request, or if there is no current session, it is true, then returns a new session session. |
7 |
Locale getLocale()
Based on the Accept-Language header, return the preferred area setting for the client accepts the content. |
8 |
Object getAttribute(String name)
Returns NULL if there is no given name attributes in an object. |
9 |
ServletInputStream getInputStream()
Use ServletInputStream to retrieve the requested subject in binary data. |
10 |
String getAuthType()
Returns the name of the authentication scheme for protecting the servlet, for example, "Basic" or "SSL", returns NULL if the JSP is not protected. |
11 |
String getCharacterEncoding()
Returns the name of the character encoding used in the request body. |
12 |
String getContentType()
Returns the MIME type of the requested main body, if you don't know the type, return null. |
13 |
String getContextPath()
Returns the request URI section indicating the request context. |
14 |
String getHeader(String name)
Returns the value of the specified request head in a string. |
15 |
String getMethod()
Returns the name of the requested HTTP method, for example, GET, POST, or PUT. |
16 |
String getParameter(String name)
Returns the value of the request parameter in the form of a string, or returns NULL if the parameter does not exist. |
17 |
String getPathInfo()
When the request is issued, return any additional path information related to the URL sent by the client. |
18 |
String getProtocol()
Returns the name and version of the request protocol. |
19 |
String getQueryString()
Returns the query string included in the request URL after the path. |
20 |
String getRemoteAddr()
Returns the Internet Protocol (IP) address of the client that sends a request. |
21 |
String getRemoteHost()
Returns the fully qualified name of the client that sends a request. |
22 |
String getRemoteUser()
If the user has passed authentication, return a request to the login user, or if the user does not pass authentication, return NULL. |
23 |
String getRequestURI()
From the protocol name until the query string of the first line of the HTTP request, part of the URL of the request is returned. |
24 |
String getRequestedSessionId()
Returns the session session ID specified by the client. |
25 |
String getServletPath()
Returns part of the URL that calls the request for JSP. |
26 |
String[] getParameterValues(String name)
Returns an array of string objects that contain all the values of the given request parameters, return null if the parameters do not exist. |
27 |
boolean isSecure()
Returns a Boolean value indicating whether the request is secure channel, such as HTTPS. |
28 |
int getContentLength()
Returns the length of the request body with bytes, and supplies the input stream, or returns -1 if the length is unknown. |
29 |
int getIntHeader(String name)
Returns the value of the specified request head as an int value. |
30 |
int getServerPort()
Returns the port number that receives this request. |
The following example reads http header information using the getHeaderNames() method of HttpServletRequest. The method returns an enumerity that contains header information related to the current HTTP request.
Once we have an enumeration, we can loop the enumeration in a standard way, using the hasMoreElements() method to determine when to stop, and the nextElement() method to get the name of each parameter.
// 导入必需的 java 库
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
// 扩展 HttpServlet 类
public class DisplayHeader extends HttpServlet {
// 处理 GET 方法请求的方法
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// 设置响应内容类型
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "HTTP Header 请求实例";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n"+
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n" +
"<table width=\"100%\" border=\"1\" align=\"center\">\n" +
"<tr bgcolor=\"#949494\">\n" +
"<th>Header 名称</th><th>Header 值</th>\n"+
"</tr>\n");
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");
}
out.println("</table>\n</body></html>");
}
// 处理 POST 方法请求的方法
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Calling the servlet above now produces the following results:
Header name | Header value |
---|---|
accept | */* |
accept-language | en-us |
user-agent | Mozilla/4.0 (compatible; M SIE 7.0; W indows NT 5.1; T rident/4.0; I nfoPath.2; MS-RTC LM 8) |
accept-encoding | gzip, deflate |
host | localhost:8080 |
connection | Keep-Alive |
cache-control | no-cache |