javaWEB總結(7):HttpServlet和HttpServletRequest__java

來源:互聯網
上載者:User

前言: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)獲得客戶機請求參數(用戶端提交的資料)                                          

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.