<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 3, 文章 - 152, 評論 - 17, 引用 - 0
    數據加載中……

    全解forward()和sendRedirect()

    昨天在servlet中搞些頁面重定向的問題,在處理頁面的path時遇到了一些惱人的問題,今天來深究一下:

    Javax.servlet.RequestDispatcher.forward(ServletRequest request, ServletResponse response)

    Javax.servlet.http.HttpServletResponse.sendRedirect(String location)

    forward

    作用于服務器端,重定向后客戶端瀏覽器地址欄的URL不變,無法通過get方式傳遞參數,不過可以通過HttpServletResponse. setAttribute(key, values)來做:

    JDKDOC中的描述:

    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.

    forward should 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 an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward.

    這個方法允許一個servlet對一個請求作完處理后,并使用另一個資源作響應。

    forward應該在向客戶端發出響應之前被調用(response的輸出流被刷新之前),如果已經向客戶端發出了響應,這個方法將拋出一個“IllegalStateException”的異常。未發出響應response的輸出緩沖區在調用forward會被自動的清空。

    得到RequestDispatcher引用的方法:

    1、  javax.servlet.ServletContext.getRequestDispatcher(String path)

    JDKDOC中的描述:

    The pathname must begin with a "/" and is interpreted as relative to the current context root. Use getContext to obtain a RequestDispatcher for resources in foreign contexts. This method returns null if the ServletContext cannot return a RequestDispatcher.

    用的最多,最容易理解的方法,path必須是以“/”開頭,路徑是相對于全局上下文路徑,對應于web應用的根目錄

    2、javax.servlet.ServletRequest.getRequestDispatcher(String path)

    JDKDOC中的描述:

    The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as relative to the current context root. This method returns null if the servlet container cannot return a RequestDispatcher.

    可以是相對地址,以當前資源的根目錄,但不能超出當前資源根目錄。

    可以是以“/”開頭的地址,當前root作為根目錄環境。

    2、  javax.servlet.ServletContext.getNamedDispatcher(String name)

    JDKDOC中的描述:

    Servlets (and JSP pages also) may be given names via server administration or via a web application deployment descriptor.

    得到名為name的資源。

    Servlets(也可是JSP 頁面)可能會有個給定的名字,該名字是服務器或者web應用的部署描述web.xml提供的。

    sendRedirect

    工作在客戶端,重定向后客戶端瀏覽器地址欄的URL變為新的資源URL,可以通過get方式傳遞參數。

           JDKDOC的描述:

    Sends a temporary redirect response to the client using the specified redirect location URL. 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.

    這個方法可以接受相對的URL,在發送響應之前servlet容器會將其轉化為絕對的URL。如果相對的URL不以“/”開頭,容器將解釋為是相對當前請求的路徑URI,如果是以“/”開頭,則解釋為是相對于servlet容器的更目錄環境。

    注意:如果已經向客戶端響應了請求,這個方法將拋出一個“IllegalStateException”的異常。調用了這個方法后,這個響應是否被提交或是否提交完畢。

           sendRedirect比較簡單,參數location就是代表重定向目標新資源。

           localtion可以有以下幾種情況:

    可以是絕對地址,如:http://localhost:8080/servlet/a.jsp

    可以是相對地址,相對于當前訪問資源的根目錄,如:a.jsp

    可以是以“/”開頭的地址,則認為是先對該web應用的根目錄,如:/a.jsp

    posted on 2005-08-01 15:44 閱讀(459) 評論(0)  編輯  收藏 所屬分類: Jsp & Servlet


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲熟女www一区二区三区| 91亚洲国产在人线播放午夜| 亚洲人av高清无码| 妻子5免费完整高清电视| 亚洲精品福利网泷泽萝拉| 69免费视频大片| 亚洲欧洲综合在线| 啦啦啦高清视频在线观看免费 | 天堂在线免费观看中文版| 亚洲人成网站免费播放| 日韩精品免费电影| 成人免费观看男女羞羞视频| 亚洲综合网站色欲色欲| 在线观看免费无码视频| 久久精品国产亚洲AV麻豆不卡| 久久精品私人影院免费看| 亚洲视频免费在线看| 丁香花在线观看免费观看| 亚洲狠狠婷婷综合久久| 一本久久综合亚洲鲁鲁五月天| 国产日韩久久免费影院 | 色妞www精品视频免费看| 亚洲日韩精品无码专区网址| 久艹视频在线免费观看| 亚洲av无码电影网| vvvv99日韩精品亚洲| 大地资源中文在线观看免费版| 亚洲综合久久久久久中文字幕| 成年女性特黄午夜视频免费看 | 男女免费观看在线爽爽爽视频| 亚洲人成自拍网站在线观看| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 亚洲欧美乱色情图片| 亚洲午夜无码片在线观看影院猛| 国产无遮挡无码视频免费软件 | 四虎成人精品一区二区免费网站| 一级做a爰全过程免费视频毛片| 内射少妇36P亚洲区| 日本免费一区二区三区最新vr| 日本高清不卡aⅴ免费网站| 亚洲av一本岛在线播放|