前言:HttpServletRequest對象封裝了用戶端進行HTTP協議請求時的所有資訊,HttpServletRequest繼承了ServletRequest,所以和ServletRequest一樣,是由tomcat伺服器提供實現的。具體過程如下:
1)Web客戶向Servlet容器發出Http請求;
2)Servlet容器解析Web客戶的Http請求;
3)Servlet容器建立一個HttpRequest對象,在這個對象中封裝Http請求資訊;
1.HttpServlet
說到HttpServletRequest就不得不說一下HttpServlet,下面看它的API:
javax.servlet.http
Class HttpServlet
java.lang.Object javax.servlet.GenericServlet javax.servlet.http.HttpServlet
All Implemented Interfaces: Serializable, Servlet, ServletConfig
| Method Summary |
| protected void |
doDelete(HttpServletRequest req,HttpServletResponse resp) Called by the server (via the service method) to allow a servlet to handle a DELETE request. |
| protected void |
doGet(HttpServletRequest req,HttpServletResponse resp) Called by the server (via the service method) to allow a servlet to handle a GET request. |
| protected void |
doHead(HttpServletRequest req,HttpServletResponse resp) Receives an HTTP HEAD request from the protected service method and handles the request. |
| protected void |
doOptions(HttpServletRequest req,HttpServletResponse resp) Called by the server (via the service method) to allow a servlet to handle a OPTIONS request. |
| protected void |
doPost(HttpServletRequest req,HttpServletResponse resp) Called by the server (via the service method) to allow a servlet to handle a POST request. |
| protected void |
doPut(HttpServletRequest req,HttpServletResponse resp) Called by the server (via the service method) to allow a servlet to handle a PUT request. |
| protected void |
doTrace(HttpServletRequest req,HttpServletResponse resp) Called by the server (via the service method) to allow a servlet to handle a TRACE request. |
| protected long |
getLastModified(HttpServletRequest req) Returns the time the HttpServletRequest object was last modified, in milliseconds since midnight January 1, 1970 GMT. |
| protected void |
service(HttpServletRequest req,HttpServletResponse resp) Receives standard HTTP requests from the public service method and dispatches them to thedoXXX methods defined in this class. |
| void |
service(ServletRequest req,ServletResponse res) Dispatches client requests to the protected service method. |
可以看到HttpServlet實現了servlet介面,所以其中的
service(ServletRequest req,ServletResponse res)方法和servlet中的一致,在每次請求時調用。只抽取部分方法,概要資訊如下:
(1).doGet(HttpServletRequest req,HttpServletResponse resp):接收HTTP的GET請求;
(2).doPost(HttpServletRequest req,HttpServletResponse resp):接收HTTP的POST請求;
(3).service(HttpServletRequest req,HttpServletResponse resp):接收HTTP請求,並把它分配到doXXX方法中;
2.HttpServletRequest的API:
javax.servlet.http
Interface HttpServletRequest All Superinterfaces: ServletRequest All Known Implementing Classes: HttpServletRequestWrapper
| String |
getAuthType() Returns the name of the authentication scheme used to protect the servlet. |
| String |
getContextPath() Returns the portion of the request URI that indicates the context of the request. |
| Cookie[] |
getCookies() Returns an array containing all of the Cookie objects the client sent with this request. |
| long |
getDateHeader(String name) Returns the value of the specified request header as a long value that represents aDate object. |
| String |
getHeader(String name) Returns the value of the specified request header as a String. |
| Enumeration |
getHeaderNames() Returns an enumeration of all the header names this request contains. |
| Enumeration |
getHeaders(String name) Returns all the values of the specified request header as an Enumeration ofString objects. |
| int |
getIntHeader(String name) Returns the value of the specified request header as an int. |
| String |
getMethod() Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT. |
| String |
getPathInfo() Returns any extra path information associated with the URL the client sent when it made this request. |
| String |
getPathTranslated() Returns any extra path information after the servlet name but before the query string, and translates it to a real path. |
| String |
getQueryString() Returns the query string that is contained in the request URL after the path. |
| String |
getRemoteUser() Returns the login of the user making this request, if the user has been authenticated, ornull if the user has not been authenticated. |
| String |
getRequestedSessionId() Returns the session ID specified by the client. |
| 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. |
| StringBuffer |
getRequestURL() Reconstructs the URL the client used to make the request. |
| String |
getServletPath() Returns the part of this request's URL that calls the servlet. |
| HttpSession |
getSession() Returns the current session associated with this request, or if the request does not have a session, creates one. |
| HttpSession |
getSession(boolean create) Returns the current HttpSession associated with this request or, if there is no current session andcreate is true, returns a new session. |
| Principal |
getUserPrincipal() Returns a java.security.Principal object containing the name of the current authenticated user. |
| boolean |
isRequestedSessionIdFromCookie() Checks whether the requested session ID came in as a cookie. |
| boolean |
isRequestedSessionIdFromUrl() Deprecated. As of Version 2.1 of the Java Servlet API, useisRequestedSessionIdFromURL() instead. |
| boolean |
isRequestedSessionIdFromURL() Checks whether the requested session ID came in as part of the request URL. |
| boolean |
isRequestedSessionIdValid() Checks whether the requested session ID is still valid. |
| boolean |
isUserInRole(String role) Returns a boolean indicating whether the authenticated user is included in the specified logical "role". |
這裡只列舉常用的幾個方法(有些API中沒有列舉到的,是在ServletRequest中已有的方法):
(1)獲得客戶機資訊
getRequestURL():返回用戶端發出請求時的完整URL。
getRequestURI():返回請求行中的資源名部分。
getQueryString() :返回請求行中的參數部分。
getRemoteAddr():返回傳出請求的客戶機的IP地址。
getRemoteHost() :返回傳出請求的客戶機的完整主機名稱。
getRemotePort():返回客戶機所使用的網路連接埠號碼。
getLocalAddr() :返回WEB伺服器的IP地址。
getLocalName():返回WEB伺服器的主機名稱。
(2)獲得客戶機要求標頭
getHeaders(String name):獲得要求標頭資訊組成的Enumeration。
(3)獲得客戶機請求參數(用戶端提交的資料)