HttpServlet的转发和重定向
HttpServletResponse重定向
1.HTTP協(xié)議規(guī)定了一種重定向的機(jī)制,重定向的運(yùn)作流程如下
用戶在瀏覽器輸入特定的URL,請求訪問服務(wù)端的某個組件。
服務(wù)端的組件返回一個狀態(tài)碼為302的響應(yīng)結(jié)果。該響應(yīng)結(jié)果的含義為:讓瀏覽器在請求訪問另一個Web組件。在響應(yīng)結(jié)果中 提供了另一個組件的URL。
當(dāng)瀏覽器端接收到這種響應(yīng)結(jié)果后,再立即自動請求訪問另一個Web組件
瀏覽器接收到來自另一個Web組件的響應(yīng)結(jié)果
補(bǔ)充301重定向和302重定向的區(qū)別
301重定向是永久的重定向,不會保留舊的網(wǎng)址,搜索引擎抓取內(nèi)容的同時會將新的網(wǎng)址替換舊的網(wǎng)址。適用于 網(wǎng)站的跳轉(zhuǎn)
302重定向是暫時的重定向,搜索引擎抓取新的內(nèi)容時會保留舊的地址,適用于未登錄跳轉(zhuǎn)到登錄頁面
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方法的。因?yàn)橹囟ㄏ驒C(jī)制是HTTP協(xié)議規(guī)定的。
API中藍(lán)色字體,表示response.sendRedirect(String location),如果參數(shù)location 以"/"開頭 表示相對URL 。如果以"Http://"開頭 表示絕對路徑
API字體中的金色字體,表示 如果在源組件進(jìn)行重定向之前,已經(jīng)提交了響應(yīng)結(jié)果(調(diào)用ServletResponse相關(guān)的close()方法),那么sendRedirect方法會拋出IllegalStateException異常
API中的紫色字體,表示Servlet源組件生成的響應(yīng)結(jié)果 不會被發(fā)送到客戶端,sendRedirect一律返回302 瀏覽器接收到302狀態(tài)碼的時候 會立刻請求目標(biāo)組件的URL。客戶端接到的響應(yīng)結(jié)果是目標(biāo)Web組件的響應(yīng)結(jié)果
源組件和目標(biāo)組件不共享一個ServletRequest對象,因此不共享請求范圍內(nèi)的共享數(shù)據(jù)(顯而易見 是瀏覽器重新像服務(wù)器發(fā)送了一個請求)
RequestDispatcher轉(zhuǎn)發(fā)
轉(zhuǎn)發(fā)是通過RequestDispatcher實(shí)現(xiàn)的
RequestDispatcher:請求分發(fā)器(故名思議: 同一個請求 分發(fā)不同的Servlet)
首先看實(shí)現(xiàn)轉(zhuǎn)發(fā)的: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(源組件) 先對客戶請求做一些預(yù)處理的操作,然后把請求轉(zhuǎn)發(fā)給其他Web組件(目標(biāo)組件)來完成包括生成相應(yīng)結(jié)果在內(nèi)的后序操作
轉(zhuǎn)發(fā)的時 源組件和目標(biāo)組件共享一個ServletRequest對象和ServletResponse對象(不同于重定向,轉(zhuǎn)發(fā)的時候?yàn)g覽器并沒有重新像服務(wù)器發(fā)送一個請求)
這是轉(zhuǎn)發(fā)和重定向最大的區(qū)別
補(bǔ)充:RequestDispatcher除了有forward方法外 還有include()方法 include()和forward()的共同點(diǎn)是 源組件和目標(biāo)組件共享一個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
總結(jié)
以上是生活随笔為你收集整理的HttpServlet的转发和重定向的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java 组件化(gradle)
- 下一篇: JavaScript 二进制运算