request 中關於路徑的部分方法舉例解析

來源:互聯網
上載者:User

request 中關於路徑的部分方法舉例解析

 

 我們假設這個web應用的虛擬目錄為UploadAdnDownload其下有兩個檔案:form.html,另一為 down.jsp, form.html中調用down.jspString str=request.getRealPath("/");String str1=request.getContextPath();String str2=request.getRequestURI();StringBuffer str3=request.getRequestURL();System.out.println("str="+str);System.out.println("str1="+str1);System.out.println("str2="+str2);System.out.println("str3="+str3); 如上幾個方法均在down.jsp中,執行後輸出的內容如下: str=C:/Documents and Settings/華禹/jbproject/updateanddownload/UploadAdnDownload/  -----------工程全域目錄
str1=/UploadAdnDownload       ------------------------------工程虛擬目錄
str2=/UploadAdnDownload/down.jsp----------------------------取得請求的URL
str3=http://localhost:8080/UploadAdnDownload/down.jsp-------取得請求的全域URL

 

request.getRealPath("url");//虛擬目錄映射為實際目錄
request.getRealPath("./");//網頁所在的目錄
request.getRealPath("../");//網頁所在目錄的上一層目錄

------------------------------------------------------------------------- 轉載自: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1717528 JavaEE程式有一大路徑陷阱,那就是ServletContext的getRealPath方法。我們常常使用getRealPath(“/”)來獲得Web應用程式根目錄的絕對路徑。這是絕對要不得的!提供這個方法絕對是JavaEE API開發組的一大敗筆。使用它,我們會萬劫不複!絕對不要使用ServletContext的getRealPath方法擷取Web應用的路徑!應該使用ServletContext的getResource()方法,直接使用相對於Web應用根目錄的相對路徑來擷取資源。 ServletContext介面中定位資源的方法getResourcejava.net.URL getResource(java.lang.String path)                         throws java.net.MalformedURLExceptionReturns a URL to the resource that is mapped to a specified path. The path must begin with a "/" and is interpreted as relative to the current context root.This method allows the servlet container to make a resource available to servlets from any source. Resources can be located on a local or remote file system, in a database, or in a .war file.The servlet container must implement the URL handlers and URLConnection objects that are necessary to access the resource.This method returns null if no resource is mapped to the pathname.Some containers may allow writing to the URL returned by this method using the methods of the URL class.The resource content is returned directly, so be aware that requesting a .jsp page returns the JSP source code. Use a RequestDispatcher instead to include results of an execution.This method has a different purpose than java.lang.Class.getResource, which looks up resources based on a class loader. This method does not use class loaders. Parameters:path - a String specifying the path to the resource Returns:the resource located at the named path, or null if there is no resource at that path Throws:java.net.MalformedURLException - if the pathname is not given in the correct form getResourceAsStreamjava.io.InputStream getResourceAsStream(java.lang.String path)Returns the resource located at the named path as an InputStream object.The data in the InputStream can be of any type or length. The path must be specified according to the rules given in getResource. This method returns null if no resource exists at the specified path.Meta-information such as content length and content type that is available via getResource method is lost when using this method.The servlet container must implement the URL handlers and URLConnection objects necessary to access the resource.This method is different from java.lang.Class.getResourceAsStream, which uses a class loader. This method allows servlet containers to make a resource available to a servlet from any location, without using a class loader. Parameters:path - a String specifying the path to the resource Returns:the InputStream returned to the servlet, or null if no resource exists at the specified path getRealPathjava.lang.String getRealPath(java.lang.String path)Returns a String containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem would be served by a request for "http://host/contextPath/index.html", where contextPath is the context path of this ServletContext..The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive). Parameters:path - a String specifying a virtual path Returns:a String specifying the real path, or null if the translation cannot be performed 說明可以看到,ServletContext介面中的getResource()等方法,可以找到任何從應用程式的根目錄開始的資源。包括在.war包這樣的壓縮檔中。參數必須以/開頭。而我們常用的getRealPath(“/”)方法,在.war包發布時,就會失效。會返回null。因此,我們應該避免使用getRealPath(“/”)這樣的方法來擷取應用程式的絕對路徑。
如果你不想使用我在《Java路徑問題最終解決方案—可定位所有資源的相對路徑定址》中提出的助手類ClassLoaderUtil 的public static URL getExtendResource(String relativePath)方法,那麼你應該使用ServletContext介面的
java.net.URL getResource(java.lang.String path)
                         throws java.net.MalformedURLException
方法,URL對象可以方便的轉為URI,和String對象。    儘管沒有ServletContext的源碼,但是我可以猜想到getResource等方法一定在底層使用了ClassLoader的getResource方法。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.