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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python 主函数 输入

    python 主函数 输入看一段主函数调用函数的小程序:1importsys2defFuc():3print'hello'45if__name__=='__main__&#

    2022年7月6日
    22
  • 对供应商锁定说不:为原生资产建立一个开放的跨链桥标准[通俗易懂]

    对供应商锁定说不:为原生资产建立一个开放的跨链桥标准[通俗易懂]我们将与大家一起建立开放的原生资产跨链桥标准,将用户、dApp开发者和区块链生态的利益放在最核心的位置。

    2022年5月25日
    34
  • 大数据分析那点事

    写在前文,首先声明博主对数据分析领域也在不断学习当中,文章中难免可能会出现一些错误,欢迎大家及时指正,博主在此之前也曾对不同量级、不同领域的数据进行过分析,但是在过程中总是感觉有许多困惑,即自己也会问自己?自己分析的是否全面,是否有价值,从哪些方面出发?对于这些问题博主做了思考。归根到底还是在理论上,在阅读了相关的专业书籍和材料的基础上总结出本文,希望能给大家带来收获,同时由于内容过多,计划分三次完成全部内容,同时如果大家感觉对自己有帮助的话,记得收藏,博主会不断完善本文的缺陷和不足,希望真正能给大家带来收

    2022年4月8日
    49
  • 话里话外:流程图绘制初级:六大常见错误

    话里话外:流程图绘制初级:六大常见错误

    2021年8月2日
    126
  • matlab 马赫带效应,matlab图像处理基础实例

    matlab 马赫带效应,matlab图像处理基础实例matlab图像处理基础实例·边缘检测(edge)边缘检测时先要把其他格式图像转化为灰度图像>>f=imread(lbxx.bmp);>>a=rgb2gray(f);>>[g,t]=edge(a,canny);>>imshow(g)·剪贴(imcrop)、subplot等imfinfocolormapsubimageimadd…

    2025年6月28日
    5
  • 转引的注释怎么写_java注释模板

    转引的注释怎么写_java注释模板如题,使用idea的时候,因为它预定义的注释样式不尽人意,但还好的是支持自定义模板。自定义模板File->Settings图一然后搜索LiveTemplates,之后添加一个自己的模板组,我的是myGroup,然后是添加自己的快速注释快捷键等等,关于如何添加这个的过程大家可以在网上搜到很多,我就不多说了。敲黑板,重点来了,对于新手来说,如何配置下面这部分无…

    2022年9月28日
    2

发表回复

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

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