HttpServlet的转发和重定向
HttpServletResponse重定向
1.HTTP協議規定了一種重定向的機制,重定向的運作流程如下
用戶在瀏覽器輸入特定的URL,請求訪問服務端的某個組件。
服務端的組件返回一個狀態碼為302的響應結果。該響應結果的含義為:讓瀏覽器在請求訪問另一個Web組件。在響應結果中 提供了另一個組件的URL。
當瀏覽器端接收到這種響應結果后,再立即自動請求訪問另一個Web組件
瀏覽器接收到來自另一個Web組件的響應結果
補充301重定向和302重定向的區別
301重定向是永久的重定向,不會保留舊的網址,搜索引擎抓取內容的同時會將新的網址替換舊的網址。適用于 網站的跳轉
302重定向是暫時的重定向,搜索引擎抓取新的內容時會保留舊的地址,適用于未登錄跳轉到登錄頁面
2. 在JAVA Servlet API中 用于重定向的HttpServletResponse 接口
sendRedirect方法:
void sendRedirect(java.lang.Stringlocation)
throws java.io.IOException
Sends a temporary redirect response to the client using the specified redirect location URL and clears the buffer. The buffer will be replaced with the data set by this method. Calling this method sets the status code toSC_FOUND302 (Found). This method can accept relative URLs;the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative without a leading '/' the container interprets it as relative to the current request URI. If the location is relative with a leading '/' the container interprets it as relative to the servlet container root.
If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.
Parameters:location- the redirect location URL
注意是HttpServletResponse,在ServletResponse中是沒有sendRedirect方法的。因為重定向機制是HTTP協議規定的。
API中藍色字體,表示response.sendRedirect(String location),如果參數location 以"/"開頭 表示相對URL 。如果以"Http://"開頭 表示絕對路徑
API字體中的金色字體,表示 如果在源組件進行重定向之前,已經提交了響應結果(調用ServletResponse相關的close()方法),那么sendRedirect方法會拋出IllegalStateException異常
API中的紫色字體,表示Servlet源組件生成的響應結果 不會被發送到客戶端,sendRedirect一律返回302 瀏覽器接收到302狀態碼的時候 會立刻請求目標組件的URL。客戶端接到的響應結果是目標Web組件的響應結果
源組件和目標組件不共享一個ServletRequest對象,因此不共享請求范圍內的共享數據(顯而易見 是瀏覽器重新像服務器發送了一個請求)
RequestDispatcher轉發
轉發是通過RequestDispatcher實現的
RequestDispatcher:請求分發器(故名思議: 同一個請求 分發不同的Servlet)
首先看實現轉發的:forward方法
void forward(ServletRequestrequest,
ServletResponseresponse)
throws ServletException,
java.io.IOException
Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. This method allows one servlet to do preliminary processing of a request and another resource to generate the response.
For aRequestDispatcherobtained viagetRequestDispatcher(), theServletRequestobject has its path elements and parameters adjusted to match the path of the target resource.
forwardshould be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws anIllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward.
The request and response parameters must be either the same objects as were passed to the calling servlet's service method or be subclasses of theServletRequestWrapperorServletResponseWrapperclasses that wrap them.
This method sets the dispatcher type of the given request toDispatcherType.FORWARD.
Parameters:request- aServletRequestobject that represents the request the client makes of the servletresponse- aServletResponseobject that represents the response the servlet returns to the client
Servlet(源組件) 先對客戶請求做一些預處理的操作,然后把請求轉發給其他Web組件(目標組件)來完成包括生成相應結果在內的后序操作
轉發的時 源組件和目標組件共享一個ServletRequest對象和ServletResponse對象(不同于重定向,轉發的時候瀏覽器并沒有重新像服務器發送一個請求)
這是轉發和重定向最大的區別
補充:RequestDispatcher除了有forward方法外 還有include()方法 include()和forward()的共同點是 源組件和目標組件共享一個ServletRequest對象
void include(ServletRequestrequest,
ServletResponseresponse)
throws ServletException,
java.io.IOException
Includes the content of a resource (servlet, JSP page, HTML file) in the response. In essence, this method enables programmatic server-side includes.
TheServletResponseobject has its path elements and parameters remain unchanged from the caller's. The included servlet cannot change the response status code or set headers; any attempt to make a change is ignored.
The request and response parameters must be either the same objects as were passed to the calling servlet's service method or be subclasses of theServletRequestWrapperorServletResponseWrapperclasses that wrap them.
This method sets the dispatcher type of the given request toDispatcherType.INCLUDE.
Parameters:request- aServletRequestobject that contains the client's requestresponse- aServletResponseobject that contains the servlet's response
總結
以上是生活随笔為你收集整理的HttpServlet的转发和重定向的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java 组件化(gradle)
- 下一篇: JavaScript 二进制运算