JSP 中的請求物件是什麼?
請求物件是 javax.servlet.http.HttpServletRequest 物件的例項。每次客戶端請求頁面時,JSP 引擎都會建立一個新物件以表示該請求。
請求物件提供獲取 HTTP 頭部資訊的方法,包括**表單資料、cookie、HTTP 方法**等。
以下是使用 HttpServletRequest 的 getHeaderNames() 方法讀取 HTTP 頭部資訊的示例。此方法返回一個包含與當前 HTTP 請求關聯的頭資訊列舉物件。
有了列舉後,我們可以以標準方式迭代列舉物件。我們將使用 hasMoreElements() 方法判斷何時停止以及 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>
廣告