?
Dokumen ini menggunakan Manual laman web PHP Cina Lepaskan
當瀏覽器請求一個網(wǎng)頁時,它會向網(wǎng)絡服務器發(fā)送一系列不能被直接讀取的信息,因為這些信息是作為HTTP信息頭的一部分來傳送的。您可以查閱HTTP協(xié)議來獲得更多的信息。
下表列出了瀏覽器端信息頭的一些重要內(nèi)容,在以后的網(wǎng)絡編程中將會經(jīng)常見到這些信息:
信息 | 描述 |
---|---|
Accept | 指定瀏覽器或其他客戶端可以處理的MIME類型。它的值通常為?image/png?或?image/jpeg |
Accept-Charset | 指定瀏覽器要使用的字符集。比如 ISO-8859-1 |
Accept-Encoding | 指定編碼類型。它的值通常為?gzip?或compress |
Accept-Language | 指定客戶端首選語言,servlet會優(yōu)先返回以當前語言構(gòu)成的結(jié)果集,如果servlet支持這種語言的話。比如 en,en-us,ru等等 |
Authorization | 在訪問受密碼保護的網(wǎng)頁時識別不同的用戶 |
Connection | 表明客戶端是否可以處理HTTP持久連接。持久連接允許客戶端或瀏覽器在一個請求中獲取多個文件。Keep-Alive?表示啟用持久連接 |
Content-Length | 僅適用于POST請求,表示 POST 數(shù)據(jù)的字節(jié)數(shù) |
Cookie | 返回先前發(fā)送給瀏覽器的cookies至服務器 |
Host | 指出原始URL中的主機名和端口號 |
If-Modified-Since | 表明只有當網(wǎng)頁在指定的日期被修改后客戶端才需要這個網(wǎng)頁。 服務器發(fā)送304碼給客戶端,表示沒有更新的資源 |
If-Unmodified-Since | 與If-Modified-Since相反, 只有文檔在指定日期后仍未被修改過,操作才會成功 |
Referer | 標志著所引用頁面的URL。比如,如果你在頁面1,然后點了個鏈接至頁面2,那么頁面1的URL就會包含在瀏覽器請求頁面2的信息頭中 |
User-Agent | 用來區(qū)分不同瀏覽器或客戶端發(fā)送的請求,并對不同類型的瀏覽器返回不同的內(nèi)容 |
request對象是javax.servlet.http.HttpServletRequest類的實例。每當客戶端請求一個頁面時,JSP引擎就會產(chǎn)生一個新的對象來代表這個請求。
request對象提供了一系列方法來獲取HTTP信息頭,包括表單數(shù)據(jù),cookies,HTTP方法等等。
接下來將會介紹一些在JSP編程中常用的獲取HTTP信息頭的方法。詳細內(nèi)容請見下表:
序號 | 方法& 描述 |
---|---|
1 | Cookie[] getCookies() 返回客戶端所有的Cookie的數(shù)組 |
2 | Enumeration getAttributeNames() 返回request對象的所有屬性名稱的集合 |
3 | Enumeration getHeaderNames() 返回所有HTTP頭的名稱集合 |
4 | Enumeration getParameterNames() 返回請求中所有參數(shù)的集合 |
5 | HttpSession getSession() 返回request對應的session對象,如果沒有,則創(chuàng)建一個 |
6 | HttpSession getSession(boolean create) 返回request對應的session對象,如果沒有并且參數(shù)create為true,則返回一個新的session對象 |
7 | Locale getLocale() 返回當前頁的Locale對象,可以在response中設(shè)置 |
8 | Object getAttribute(String name) 返回名稱為name的屬性值,如果不存在則返回null。 |
9 | ServletInputStream getInputStream() 返回請求的輸入流 |
10 | String getAuthType() 返回認證方案的名稱,用來保護servlet,比如 "BASIC" 或者 "SSL" 或 null 如果 JSP沒設(shè)置保護措施 |
11 | String getCharacterEncoding() 返回request的字符編碼集名稱 |
12 | String getContentType() 返回request主體的MIME類型,若未知則返回null |
13 | String getContextPath() 返回request URI中指明的上下文路徑 |
14 | String getHeader(String name) 返回name指定的信息頭 |
15 | String getMethod() 返回此request中的HTTP方法,比如 GET,,POST,或PUT |
16 | String getParameter(String name) 返回此request中name指定的參數(shù),若不存在則返回null |
17 | String getPathInfo() 返回任何額外的與此request URL相關(guān)的路徑 |
18 | String getProtocol() 返回此request所使用的協(xié)議名和版本 |
19 | String getQueryString() 返回此 request URL包含的查詢字符串 |
20 | String getRemoteAddr() 返回客戶端的IP地址 |
21 | String getRemoteHost() 返回客戶端的完整名稱 |
22 | String getRemoteUser() 返回客戶端通過登錄認證的用戶,若用戶未認證則返回null |
23 | String getRequestURI() 返回request的URI |
24 | String getRequestedSessionId() 返回request指定的session ID |
25 | String getServletPath() 返回所請求的servlet路徑 |
26 | String[] getParameterValues(String name) 返回指定名稱的參數(shù)的所有值,若不存在則返回null |
27 | boolean isSecure() 返回request是否使用了加密通道,比如HTTPS |
28 | int getContentLength() 返回request主體所包含的字節(jié)數(shù),若未知的返回-1 |
29 | int getIntHeader(String name) 返回指定名稱的request信息頭的值 |
30 | int getServerPort() 返回服務器端口號 |
在這個例子中,我們會使用HttpServletRequest類的getHeaderNames()方法來讀取HTTP信息頭。這個方法以枚舉的形式返回當前HTTP請求的頭信息。
獲取Enumeration對象后,用標準的方式來遍歷Enumeration對象,用hasMoreElements()方法來確定什么時候停止,用nextElement()方法來獲得每個參數(shù)的名字。
<%@ 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>\n"); String paramValue = request.getHeader(paramName); out.println("<td> " + paramValue + "</td></tr>\n"); } %> </table> </center> </body> </html>
訪問main.jsp,將會得到以下結(jié)果:
Header Name | Header Value(s) | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
accept | *
}
}
編譯LogFilter.java文件,然后將編譯后的class文件放在<Tomcat安裝目錄>/webapps/ROOT/WEB-INF/classes目錄下。 web.xml文件中的JSP過濾器映射過濾器被定義,然后映射成一個URL或JSP文件名,與servlet被定義然后映射的方式差不多。在部署描述文件web.xml中,使用<filter>標簽來進行過濾器映射: <filter> <filter-name>LogFilter</filter-name> <filter-class>LogFilter</filter-class> <init-param> <param-name>test-param</param-name> <param-value>Initialization Paramter</param-value> </init-param> </filter> <filter-mapping> <filter-name>LogFilter</filter-name> <url-pattern>* Accept-Encoding: gzip Accept-Language: en Accept-Charset: iso-8859-1,*,utf-8 Cookie: name=xyz JSP腳本通過request對象中的getCookies()方法來訪問這些cookies,這個方法會返回一個Cookie對象的數(shù)組。 Servlet Cookies 方法下表列出了Cookie對象中常用的方法:
使用JSP設(shè)置Cookies使用JSP設(shè)置cookie包含三個步驟: (1)創(chuàng)建一個Cookie對象: 調(diào)用Cookie的構(gòu)造函數(shù),使用一個cookie名稱和值做參數(shù),它們都是字符串。 Cookie cookie = new Cookie("key","value"); 請務必牢記,名稱和值中都不能包含空格或者如下的字符: [ ] ( ) = , " / ? @ : ; (2) 設(shè)置有效期:調(diào)用setMaxAge()函數(shù)表明cookie在多長時間(以秒為單位)內(nèi)有效。下面的操作將有效期設(shè)為了24小時。 cookie.setMaxAge(60*60*24); (3) 將cookie發(fā)送至HTTP響應頭中:調(diào)用response.addCookie()函數(shù)來向HTTP響應頭中添加cookies。 response.addCookie(cookie); 實例演示<% // 為 first_name 和 last_name設(shè)置cookie Cookie firstName = new Cookie("first_name", request.getParameter("first_name")); Cookie lastName = new Cookie("last_name", request.getParameter("last_name")); // 設(shè)置cookie過期時間為24小時。 firstName.setMaxAge(60*60*24); lastName.setMaxAge(60*60*24); // 在響應頭部添加cookie response.addCookie( firstName ); response.addCookie( lastName ); %> <html> <head> <title>Setting Cookies</title> </head> <body> <center> <h1>Setting Cookies</h1> </center> <ul> <li><p><b>First Name:</b> <%= request.getParameter("first_name")%> </p></li> <li><p><b>Last Name:</b> <%= request.getParameter("last_name")%> </p></li> </ul> </body> </html> 將上面兩個文件放在<Tomcat安裝目錄>/webapps/ROOT目錄下,然后訪問http://localhost:8080/hello.jsp,將會得到如下輸出結(jié)果: 試著輸入First Name和Last Name,然后點擊提交按鈕,它將會在您的屏幕中顯示first name和last name,并且設(shè)置first name和last name兩個cookie,下一次點擊提交按鈕時會發(fā)給服務器。 使用JSP讀取Cookies想要讀取cookies,您就需要調(diào)用request.getCookies()方法來獲得一個javax.servlet.http.Cookie對象的數(shù)組,然后遍歷這個數(shù)組,使用getName()方法和getValue()方法來獲取每一個cookie的名稱和值。 讓我們來讀取上個例子中的cookies。 <html> <head> <title>Reading Cookies</title> </head> <body> <center> <h1>Reading Cookies</h1> </center> <% Cookie cookie = null; Cookie[] cookies = null; // 獲取cookies的數(shù)據(jù),是一個數(shù)組 cookies = request.getCookies(); if( cookies != null ){ out.println("<h2> Found Cookies Name and Value</h2>"); for (int i = 0; i < cookies.length; i++){ cookie = cookies[i]; out.print("Name : " + cookie.getName( ) + ", "); out.print("Value: " + cookie.getValue( )+" <br/>"); } }else{ out.println("<h2>No cookies founds</h2>"); } %> </body> </html> 如果您把first name cookie設(shè)置成"John",last name設(shè)置成"Player",訪問 http://localhost:8080/main.jsp,將會得到如下輸出結(jié)果: Found Cookies Name and Value Name : first_name, Value: John Name : last_name, Value: Player 使用JSP刪除Cookies刪除cookies非常簡單。如果您想要刪除一個cookie,按照下面給的步驟來做就行了:
實例演示下面的程序刪除一個名為"first_name"的cookie,當您下次運行main.jsp時,first_name將會為null。 <html> <head> <title>Reading Cookies</title> </head> <body> <center> <h1>Reading Cookies</h1> </center> <% Cookie cookie = null; Cookie[] cookies = null; // 獲取當前域名下的cookies,是一個數(shù)組 cookies = request.getCookies(); if( cookies != null ){ out.println("<h2> Found Cookies Name and Value</h2>"); for (int i = 0; i < cookies.length; i++){ cookie = cookies[i]; if((cookie.getName( )).compareTo("first_name") == 0 ){ cookie.setMaxAge(0); response.addCookie(cookie); out.print("Deleted cookie: " + cookie.getName( ) + "<br/>"); } out.print("Name : " + cookie.getName( ) + ", "); out.print("Value: " + cookie.getValue( )+" <br/>"); } }else{ out.println( "<h2>No cookies founds</h2>"); } %> </body> </html> 訪問它,將會得到如下輸出結(jié)果: Cookies Name and Value Deleted cookie : first_name Name : first_name, Value: John Name : last_name, Value: Player 再次訪問http://localhost:8080/main.jsp,將會得到如下結(jié)果: Found Cookies Name and Value Name : last_name, Value: Player 您也可以手動在瀏覽器中刪除cookies。點擊Tools菜單項,然后選擇Internet Options,點擊Delete Cookies,就能刪除所有cookies了。 關(guān)于我們 聯(lián)系我們 留言板 手冊網(wǎng) |