Servlet學習筆記 -ServletRequest、ServletResponse

1、ServletRequest

  在Servlet中,ServletRequest是一個非常重要的接口。這個類是由Servlet容器進行實例化,然後作爲參數傳遞給了service()方法。在HttpServlet中,對應的是HttpServletRequest接口,HttpServletRequest 接口繼承與ServletRequest接口。

  在Servlet 3.0 API中,ServletRequest接口或類的層級結構如下:
在這裏插入圖片描述
  當然,ServletRequest接口或類的層級結構中不僅僅包括Servlet 3.0 API中的這幾個接口或類,同時在Tomcat容器和spring中都有很對相應的實現,比如:org.apache.catalina.core.ApplicationRequest和org.springframework.web.multipart.MultipartHttpServletRequest等。

ServletRequest作用:

   Servlet容器對於接收到的每一個請求,都會創建一個ServletRequest對象,並把這個對象作爲參數傳遞給Servlet的sevice( )方法。ServletRequest對象封裝了關於這次請求的相關信息。繼承該接口的接口,可以攜帶一些和協議相關的特定信息,比如javax.servlet.http.HttpServletRequest接口,就攜帶了一些HTTP協議的一些信息。

ServletRequest方法:

  1. public Object getAttribute(String name);
    獲取ServletRequest對象中的對應name的參數值。在ServletRequest對象中,參數的設置有兩種方式:
       1>、一種是由Servelt容器根據不同的請求而添加的參數信息,比如Https請求,Servlet容器就會根據客戶端的證書,生成javax.servlet.request.X509Certificate對象,並添加到ServletRequest實例中;
       2>、還有一種是通過編程方式添加的參數,即通過ServletRequest.setAttribute()方法添加,這種方式必須在調用RequestDispatcher對象之前進行。

  2. public Enumeration getAttributeNames();
    返回ServletRequest對象中,所有屬性的key對應的集合,返回類型Enumeration。

  3. public void setAttribute(String name, Object o);
    添加屬性到request請求的方法,上面提到的編程式添加屬性方法,一般與RequestDispatcher配合使用。如果添加的對象o=null的話,相當於執行了removeAttribute方法,即移除參數操作。還需要注意的是:name的命名方式,所有以java.*、javax.*、sun.*、com.sun.*、oracle.*、com.oracle.*字符串開頭的都作爲預留,不允許開發者使用。

  4. public void removeAttribute(String name);
    移除該請求中的一個參數。

  5. public String getCharacterEncoding();
    獲取該request請求體的字符編碼,如果沒有設置則返回null。

  6. public void setCharacterEncoding(String env) throws java.io.UnsupportedEncodingException;
    設置該request請求體的字符編碼,會覆蓋原有的字符編碼。需要在獲取參數前進行設置。

  7. public int getContentLength(); 和 public long getContentLengthLong();
    兩個方法作用基本一樣,連官方文檔的解釋基本上都是一模一樣的。區別:getContentLengthLong()方法是從servlet3.1開始引入的;還有就是請求體長度的限制,getContentLengthLong()方法取消了Integer#MAX_VALUE的限制(未驗證)。

  8. public String getContentType();
    返回請求體的MIME類型。

  9. public ServletInputStream getInputStream() throws IOException;
    獲取請求體的二進制流 ServletInputStream 對象。讀取請求體內容的方法還有getReader()方法,但是兩者不能同時使用。

  10. public BufferedReader getReader() throws IOException;
    獲取請求體的字符串數據,用BufferedReader對象表示,轉換成字符串數據的時候,使用當前設置的字符串進行轉碼。和getInputStream()方法類似,同樣是兩者不能同時使用。

  11. public String getRemoteAddr();
    獲取客戶端的IP地址。

  12. public String getRemoteHost();
    獲得客戶端的主機名。在獲取主機名的過程中,會涉及到dns查詢,所以對性能上會有所影響。

  13. public int getRemotePort();
    獲取客戶端的端口號。

  14. public String getLocalName();
    獲取服務器的主機名。

  15. public String getLocalAddr();
    獲取服務器的IP地址。

  16. public int getLocalPort();
    獲取服務器的端口號。

  17. public Locale getLocale();
    獲取請求的Locale信息,該信息根據請求頭中的Accept-Language來判斷,如果沒有該請求頭,則使用默認的Locale對象。

  18. public Enumeration<Locale> getLocales();
    和getLocale()方法類似,只不過該方法返回的是集合,而且集合中的Locale對象的優先級依次遞減。

  19. public boolean isSecure();
    返回當前請求是否使用了安全協議,比如Https。

  20. public RequestDispatcher getRequestDispatcher(String path);
    返回RequestDispatcher對象,和ServletContext#getRequestDispatcher功能類似。區別在於:這個方法中可以使用相對路徑。

  21. public DispatcherType getDispatcherType();
    獲取DispatcherType 值,表示RequestDispatcher對象類型。

  22. public ServletContext getServletContext();
    獲取當前請求對應的ServletContext 對象。

  23. public AsyncContext startAsync() throws IllegalStateException;
    啓用了服務端異步處理請求邏輯的功能。調用該方法後,對客戶端沒有任何影響,但是服務端會將處理該請求的線程釋放掉,這樣讓容器可以有機會將相關資源分配給其它的請求,可以減輕系統負擔。當該請求的業務邏輯處理好了,然後再行對客戶端的響應。

  24. public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) throws IllegalStateException;
    和startAsync()方法功能類似。

  25. public boolean isAsyncStarted();
    獲取當前請求是否使用了服務端異步處理功能。

  26. public boolean isAsyncSupported();
    判斷是否支持服務端異步處理的功能。

  27. public AsyncContext getAsyncContext();
    獲取當前請求對應的AsyncContext對象。

2、ServletResponse

  ServletResponse對象一般都是和ServletRequest對象成對出現的。ServletRequest處理客戶端請求,ServletResponse對象封裝客戶端響應。ServletResponse的實例對象也會在Servlet容器調用service()方法的時候,作爲參數進行傳遞。ServletResponse隱藏了向瀏覽器發送響應的複雜過程。在HttpServlet中,對應的是HttpServletResponse接口,HttpServletResponse 接口繼承與ServletResponse接口。

  在Servlet 3.0 API中,ServletResponse接口或類的層級結構如下:
在這裏插入圖片描述
  和ServletRequest接口類似,ServletResponse接口層級結構中也是不僅僅包括Servlet 3.0 API中的這幾個接口或類,同時在Tomcat容器和spring中都有很對相應的實現,比如:org.apache.catalina.core.ApplicationResponse和org.springframework.web.util.ContentCachingResponseWrapper等。

ServletResponse作用:

   Servlet容器對於接收到的每一個請求,都會在創建一個ServletRequest對象的同時,創建一個ServletResponse對象,並把這兩個對象作爲參數傳遞給Servlet的sevice( )方法。其中,ServletRequest對象封裝了關於這次請求的相關信息,而ServletResponse對象中則封裝了一些響應客戶端的相關信息,比如編碼信息、響應的數據信息、數據字符編碼信息等。

ServletResponse方法:

  1. public String getCharacterEncoding();
    獲取當前響應體的字符編碼。如果沒有設置,返回的是“ISO-8859-1”。

  2. public void setCharacterEncoding(String charset);
    設置當前響應體的字符編碼,該方法設置的字符編碼會覆蓋掉setContentType()或setLocale()方法中的設置過。該方法需要在調用getWriter()方法或響應已經提交前,才能生效。在HTTP中,一般對應了響應頭中的“Content-Type”。

    setContentType("text/html; charset=UTF-8");
    

    等效於

    setCharacterEncoding("UTF-8");
    setContentType("text/html");
    
  3. public String getContentType();
    獲取當前響應體的內容類型,如果沒有設置就返回null。如果字符編碼被設置了(即getCharacterEncoding()方法有返回值的話),該方法就會把字符編碼一起返回,比如:“text/html; charset=UTF-8”。

  4. public void setContentType(String type);
    和setCharacterEncoding()方法類似,這個方法是用來設置內容類型的。

  5. public void setContentLength(int len);
    設置響應體的長度。在HTTP請求中,對應“Content-Length”。

  6. public void setContentLengthLong(long length);
    和setContentLength()方法功能一樣。

  7. public ServletOutputStream getOutputStream() throws IOException;
    獲取一個可以向客戶端寫二進制數據流的ServletOutputStream對象,二進制流不會進行編碼。當調用flush()方法時,就會把緩存數據刷新到響應內容中。

  8. public PrintWriter getWriter() throws IOException;
    和getOutputStream()方法功能類似,只不過該方法是一字符串數據類型進行響應客戶端,且字符串數據需要通過指定的編碼格式進行編碼。

  9. public void setBufferSize(int size);
    設置緩衝區大小。當緩存區滿的時候,就相當於執行了一次flush()方法。

  10. public int getBufferSize();
    獲取當前緩存區的大小。

  11. public void flushBuffer() throws IOException;
    強制執行了一次刷新操作,即當緩存沒有滿的時候,可以通過該方法執行一次向客戶端寫數據的操作。

  12. public void resetBuffer();
    清空緩存區中的內容,當時不考慮response的頭信息和狀態信息。如果response已經被提交,則會拋出IllegalStateException異常。

  13. public boolean isCommitted();
    判斷當前response是否已經被提交了。

  14. public void reset();
    重啓當前響應信息,和resetBuffer()區別在於:這個方法會把response的頭信息和狀態信息也清除掉。

  15. public void setLocale(Locale loc);
    設置響應頭的本地化信息,其中也包括了字符編碼的內容,如果setContentType()方法和setCharacterEncoding()方法沒有進行設置,就會使用本地化信息中的字符編碼的配置。

  16. public Locale getLocale();
    獲取響應頭的本地化實例對象Locale。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章