JSP客戶端請求
當瀏覽器請求的網頁,它會發送大量的信息,而不能直接因為這些信息作為出行的HTTP請求報頭的一部分被讀取Web服務器。您可以查看HTTP協議進行更多這方麵的信息。
以下是來自瀏覽器端,你會使用非常頻繁的網絡編程的重要頭信息:
頭 | 描述 |
---|---|
Accept | This header specifies the MIME types that the browser or other clients can handle. Values of image/png or image/jpeg are the two most common possibilities. |
Accept-Charset | This header specifies the character sets the browser can use to display the information. For example ISO-8859-1. |
Accept-Encoding | This header specifies the types of encodings that the browser knows how to handle. Values of gzip or compress are the two most common possibilities. |
Accept-Language | This header specifies the client's preferred languages in case the servlet can produce results in more than one language. For example en, en-us, ru, etc. |
Authorization | This header is used by clients to identify themselves when accessing password-protected Web pages. |
Connection | This header indicates whether the client can handle persistent HTTP connections. Persistent connections permit the client or other browser to retrieve multiple files with a single request. A value ofKeep-Alive means that persistent connections should be used |
Content-Length | This header is applicable only to POST requests and gives the size of the POST data in bytes. |
Cookie | This header returns cookies to servers that previously sent them to the browser. |
Host | This header specifies the host and port as given in the original URL. |
If-Modified-Since | This header indicates that the client wants the page only if it has been changed after the specified date. The server sends a code, 304 which means Not Modified header if no newer result is available. |
If-Unmodified-Since | This header is the reverse of If-Modified-Since; it specifies that the operation should succeed only if the document is older than the specified date. |
Referer | This header indicates the URL of the referring Web page. For example, if you are at Web page 1 and click on a link to Web page 2, the URL of Web page 1 is included in the Referer header when the browser requests Web page 2. |
User-Agent | This header identifies the browser or other client making the request and can be used to return different content to different types of browsers. |
HttpServletRequest 對象:
request 象是javax.servlet.http.HttpServletRequest對象的一個實例。每次客戶端請求一個頁麵的JSP引擎創建一個新的對象來表示該請求。
request 對象提供的方法來獲得,包括表單數據的HTTP頭信息,餅乾,HTTP方法等。
有以下幾個重要的方法,可以用來讀取HTTP頭在你的JSP程序。這些方法可使用HttpServletRequest對象代表客戶端請求的Web服務器。
S.N. | 方法 & 描述 |
---|---|
1 |
Cookie[] getCookies() Returns an array containing all of the Cookie objects the client sent with this request. |
2 |
Enumeration getAttributeNames() Returns an Enumeration containing the names of the attributes available to this request. |
3 |
Enumeration getHeaderNames() Returns an enumeration of all the header names this request contains. |
4 |
Enumeration getParameterNames() Returns an Enumeration of String objects containing the names of the parameters contained in this request. |
5 |
HttpSession getSession() Returns the current session associated with this request, or if the request does not have a session, creates one. |
6 |
HttpSession getSession(boolean create) Returns the current HttpSession associated with this request or, if if there is no current session and create is true, returns a new session. |
7 |
Locale getLocale() Returns the preferred Locale that the client will accept content in, based on the Accept-Language header |
8 |
Object getAttribute(String name) Returns the value of the named attribute as an Object, or null if no attribute of the given name exists. |
9 |
ServletInputStream getInputStream() Retrieves the body of the request as binary data using a ServletInputStream. |
10 |
String getAuthType() Returns the name of the authentication scheme used to protect the servlet, for example, "BASIC" or "SSL," or null if the JSP was not protected |
11 |
String getCharacterEncoding() Returns the name of the character encoding used in the body of this request. |
12 |
String getContentType() Returns the MIME type of the body of the request, or null if the type is not known. |
13 |
String getContextPath() Returns the portion of the request URI that indicates the context of the request. |
14 |
String getHeader(String name) Returns the value of the specified request header as a String. |
15 |
String getMethod() Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT. |
16 |
String getParameter(String name) Returns the value of a request parameter as a String, or null if the parameter does not exist. |
17 |
String getPathInfo() Returns any extra path information associated with the URL the client sent when it made this request. |
18 |
String getProtocol() Returns the name and version of the protocol the request. |
19 |
String getQueryString() Returns the query string that is contained in the request URL after the path. |
20 |
String getRemoteAddr() Returns the Internet Protocol (IP) address of the client that sent the request. |
21 |
String getRemoteHost() Returns the fully qualified name of the client that sent the request. |
22 |
String getRemoteUser() Returns the login of the user making this request, if the user has been authenticated, or null if the user has not been authenticated. |
23 |
String getRequestURI() Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request. |
24 |
String getRequestedSessionId() Returns the session ID specified by the client. |
25 |
String getServletPath() Returns the part of this request's URL that calls the JSP. |
26 |
String[] getParameterValues(String name) Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist. |
27 |
boolean isSecure() Returns a boolean indicating whether this request was made using a secure channel, such as HTTPS. |
28 |
int getContentLength() Returns the length, in bytes, of the request body and made available by the input stream, or -1 if the length is not known. |
29 |
int getIntHeader(String name) Returns the value of the specified request header as an int. |
30 |
int getServerPort() Returns the port number on which this request was received. |
HTTP頭請求實例:
下麵是一個使用HttpServletRequest中的getHeaderNames()方法來讀取HTTP頭的請見例子。此方法返回一個包含與當前HTTP請求關聯的標頭信息的枚舉。
一旦我們有了一個Enumeration,我們可以循環下來以標準方式的枚舉,usinghasMoreElements()方法來確定何時停止使用和nextElement()方法來獲得每個參數的名稱。
<%@ page import="java.io.*,java.util.*" %> <html> <head> <title>HTTP Header Request Example</title> </head> <body> <center> <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> "); String paramValue = request.getHeader(paramName); out.println("<td> " + paramValue + "</td></tr> "); } %> </table> </center> </body> </html>
現在把上麵的代碼寫在main.jsp,並嘗試訪問它。這會產生什麼結果如下:
HTTP Header Request Example
頭名稱 | 頭的值(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 |
也可使用其他方法可以嘗試更上麵列出的方法很少以同樣的方式。