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 transmitted as part of the HTTP message header. You can check the HTTP protocol for more information.
The following table lists some of the important aspects of the browser-side information header, which you will see frequently in future network 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 to be used by the browser. Like Iso-8859-1. |
Accept-encoding |
Specifies the encoding type. It is usually a value of gzip or compress |
Accept-language |
Specifies the client preferred language, and the servlet takes precedence over the result set in the current language, if the servlet supports that language. Like En,en-us,ru and so on. |
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 obtain multiple files in a single request. keep-alive indicates that persistent connections are enabled |
Content-length |
Applies only to post requests, indicating the number of bytes of post data |
Cookies |
Returns the cookies previously sent to the browser to the server |
Host |
Indicates the host name and port number in the original URL |
If-modified-since |
Indicates that the client will need this page only if the page has been modified after the specified date. The server sends 304 yards to the client, indicating that there are no updated resources |
If-unmodified-since |
In contrast to If-modified-since, the operation succeeds only if the document has not been modified since the specified date |
Referer |
Indicates the URL of the referenced page. For example, if you are on page 1 and then click on a link to page 2, then page 1 URL will be included in the browser request page 2 of the information header |
User-agent |
Used to distinguish between requests sent by different browsers or clients, and to 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 the request.
The Request object provides a series of methods to get HTTP headers, including form data, cookies,http methods, and so on.
The next step is to introduce some of the methods that are commonly used in JSP programming to get HTTP information headers. Please see the table below for details:
Serial Number |
method & Description |
1 |
Cookie[] GetCookies () Returns an array of all cookies for the client |
2 |
Enumeration Getattributenames () Returns a collection of all property names for the request object |
3 |
Enumeration Getheadernames () Returns a collection of names for 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, and if not, creates a |
6 |
HttpSession getsession (Boolean Create) Returns a Session object corresponding to request, and returns a new Session object if none and the parameter create is true |
7 |
Locale GetLocale () Returns the locale object for the current page, which you can set in response |
8 |
Object getattribute (String name) Returns a property 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 that is used to protect the servlet, such as "BASIC" or "SSL" or null if the JSP does not have a protective action set |
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 unknown |
13 |
String Getcontextpath () Returns the context path indicated 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 and returns 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 IP address of the client |
21st |
String Getremotehost () Returns the full name of the client |
22 |
String Getremoteuser () Returns the user who is authenticated by the login and returns 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 request |
25 |
String Getservletpath () Returns the requested Servlet path |
26 |
String[] Getparametervalues (String name) Returns all values of a parameter of 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 by the request body if an unknown return-1 |
29 |
int Getintheader (String name) Returns the value of the request information header for the specified name |
30 |
int Getserverport () Return Server port number |
HTTP Information 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 for the current HTTP request in the form of an enumeration.
After getting the enumeration object, use the standard way to traverse the enumeration object, use the hasMoreElements () method to determine when to stop, and use the Nextelement () method to get 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><Tablewidth= "100%"Border= "1"Align= "Center"><TRbgcolor= "#949494"><th>Header Name</th><th>Header Value (s)</th></TR><%Enumeration Headernames=Request.getheadernames (); while(Headernames.hasmoreelements ()) {Stringparamname= (String) headernames.nextelement (); Out.print ("<tr><td>" +paramname+ "</td>\n"); Stringparamvalue=Request.getheader (paramname); Out.println ("<td>" +paramvalue+ "</td></tr>\n"); }%></Table></Center></Body></HTML>
To access main.jsp, you will get the following results:
HTTP Header Request Example
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 the other methods of the HttpServletRequest class in the code above.
JSP Client Request