java getrealpath_JavaEE路径陷阱之getRealPath「建议收藏」

java getrealpath_JavaEE路径陷阱之getRealPath「建议收藏」JavaEE程序有一大路径陷阱,那就是ServletContext的getRealPath方法。我们常常使用getRealPath(“/”)来获得Web应用程序根目录的绝对路径。这是绝对要不得的!提供这个方法绝对是JavaEEAPI开发组的一大败笔。使用它,我们会万劫不复!绝对不要使用ServletContext的getRealPath方法获取Web应用的路径!应该使用ServletContex…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

JavaEE程序有一大路径陷阱,那就是ServletContext的getRealPath方法。我们常常使用getRealPath(“/”)来获得Web应用程序根目录的绝对路径。这是绝对要不得的!提供这个方法绝对是JavaEE API开发组的一大败笔。使用它,我们会万劫不复!

绝对不要使用ServletContext的getRealPath方法获取Web应用的路径!应该使用ServletContext的getResource()方法,直接使用相对于Web应用根目录的相对路径来获取资源。

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

说明

可以看到,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方法。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/191695.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • matlab循环读取txt文件「建议收藏」

    matlab循环读取txt文件「建议收藏」一般情况下,假如我要读取一个名为a.txt的文件,只需要利用下面的语句:a=load(‘a.txt’);现在假如我需要循环读取saif_1.txt,saif_2.txt,,,一直到saif_10.txt,他们都是10*1的矩阵,对他们进行转置操作后,再合并到一个文件中,可以利用下面的语句:forN=1:10a=load([‘saif_’,num2str(N),’.txt’]);……

    2022年10月7日
    0
  • maven学习系列——(二)maven的安装和一些基本的配置

    这一篇主要会总结maven在window上的安装,以及Eclipse安装maven插件。 会整理和贴出具体的安装步骤等! 配置大概会整理一下,方便自己查看和使用!Maven 的使用在Windows上使用比较多,一般的开发都是在Windows上;Linux上的使用相对比较少,不过会总结Windows和Linux系统两种安装方式。

    2022年2月25日
    30
  • Kohana 数据库

    Kohana 数据库

    2022年1月11日
    47
  • java导出pdf模板_java模板导出PDF[通俗易懂]

    java导出pdf模板_java模板导出PDF[通俗易懂]本次完善综合特点:一对一,点对点的给对应的地方写值,比如模板里面放了个name标识,在程序里把“张三”赋给name,那么输出的pdf里面name的地方就变成了张三,准确方便快捷支持中文,可以使用自己下载的字体。支持图片:图片的大小范围可以在模板随意调,生成出来的图片不会超过范围。而且不需要根据坐标去算,程序里面自动计算的。支持多页模板,即使是好几页的模板,只要每个变量对应的范围确定好了,生成出来的…

    2022年5月10日
    40
  • 项目与数据库时差8小时解决,设置SpringBoot的时区

    项目与数据库时差8小时解决,设置SpringBoot的时区Componentpub PostConstruc TimeZone setDefault TimeZone getTimeZone UTC TimeZone setDefault TimeZone getTimeZone Asia Shanghai TimeZone setDefault Time

    2025年6月8日
    0
  • 【AekdyCoin】求小于等于N的与N互质的数的和

    【AekdyCoin】求小于等于N的与N互质的数的和又向大牛学到了一点。以下内容转大牛文章:ifgcd(n,i)=1thengcd(n,n-i)=1(1反证法:如果存在K!=1使gcd(n,n-i)=k,那么(n-i)%k==0而n%k=0那么必须保证i%k=0k是n的因子,如果i%k=0那么gcd(n,i)=k,矛盾出现;于是问题变的非常简单ANS=N*phi(N)/2i,n-i总是成对

    2022年7月23日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号