小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

關(guān)于serveletContext.getRealPath()方法

 流浪在北京 2011-03-13


關(guān)于serveletContext.getRealPath()方法
 

1.關(guān)于request.getRealPath
問(wèn)題:
String filename=request.getRealPath(filename)
-------------------
信息:
warning: [deprecation] getRealPath(java.lang.String) in javax.servlet.ServletRequest has been deprecated

解決:
這個(gè)getRealPath方法已經(jīng)不建議使用了

參看request.getRealPath的java doc:
Deprecated. As of Version 2.1 of the Java Servlet API, use ServletContext.getRealPath(java.lang.String) instead. 


而在servlet中使用getServletContext.getRealPath()這個(gè)方法受到war 和non-war的影響,以及不同app server實(shí)現(xiàn)的影響,運(yùn)氣好的話,你常常會(huì)得到null,嘿嘿,比如你在weblogic上部署war文件,又調(diào)用這個(gè)方法..

推薦ServletContext.getResourceAsStream


2.關(guān)于serveletContext.getRealPath返回NULL和不同的app server返回不同的結(jié)果

問(wèn)題:

有幾個(gè)配置文本配置文件(是一些報(bào)表的模板),放在WEB-INF下面的config目錄下,程序中是這樣得到這個(gè)config的實(shí)際路徑的:
先用 serveletContext.getRealPath得到根路徑,tomcat中比如是
c:\tomcat\webapp\test

然后我加上 "/WEB-INF/config/aa.config",這樣得到文件的path然后進(jìn)行讀入,應(yīng)用在tomcat上跑是ok的,后來(lái)將war放到weblogic上,出錯(cuò),原因是:
在weblogic上用getRealPath得到的是像
myserver\stage\_appsdir_test_war\test.war!\WEB-INF\config....
這樣的路徑,于是一直報(bào)FileNotFoundException

解決:

serveletContext.getRealPath
這個(gè)方法在不同的服務(wù)器上所獲得的實(shí)現(xiàn)是不一樣的, 建議是通過(guò)classloader來(lái)獲得你配置的資源文件

context.getRealPath("/")可能返回了null,你可以輸入來(lái)看看,
對(duì)一個(gè)打包的應(yīng)用來(lái)說(shuō),是沒(méi)有RealPath的概念的,調(diào)用getRealPath只會(huì)簡(jiǎn)單地返回null。其實(shí),也很

好理解,一個(gè)文件被打包入了.war文件,就不存在目錄結(jié)構(gòu)了(雖然包中仍然存在目錄結(jié)構(gòu),但這不等同于文件系統(tǒng)中的目錄結(jié)構(gòu))。所以,對(duì)war包中的資源是無(wú)法得到RealPath的。這樣也就無(wú)從通過(guò)文件IO進(jìn)行讀取了。

那么,如何讀取war包中的資源呢?答案是使用:
ServletContext.getResourceAsStream("/WEB-INF/config/aa.config")方法。


原則:基本上就是盡量使用j2ee規(guī)范中的各層次classloader來(lái)獲取資源,而不是試圖去找文件的絕對(duì)路
方法:調(diào)用this.getClass().getClassLoader().getResource("/").getPath(); 獲取到classes目錄的全路徑

使用:在得到classes目錄的全路徑后再根據(jù)字符串的截取與拼裝達(dá)到你的要求即可。



   絕對(duì)不要使用ServletContext的getRealPath方法獲取Web應(yīng)用的路徑!應(yīng)該使用ServletContext的getResource()方法,直接使用相對(duì)于Web應(yīng)用根目錄的相對(duì)路徑來(lái)獲取資源。

ServletContext接口中定位資源的方法
getResource
java.net.URL getResource(java.lang.String path)
throws java.net.MalformedURLException
Returns 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
getResourceAsStream
java.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
getRealPath
java.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

說(shuō)明
可以看到,ServletContext接口中的getResource()等方法,可以找到任何從應(yīng)用程序的根目錄開(kāi)始的資源。包括在.war包這樣的壓縮文件中。參數(shù)必須以/開(kāi)頭。
而我們常用的getRealPath(“/”)方法,在.war包發(fā)布時(shí),就會(huì)失效。會(huì)返回null。
因此,我們應(yīng)該避免使用getRealPath(“/”)這樣的方法來(lái)獲取應(yīng)用程序的絕對(duì)路徑。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多