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


相关推荐

  • vsc怎么创建html文件_用vscode写一个html页面

    vsc怎么创建html文件_用vscode写一个html页面1.新建一个文件2.右下角点击纯文本3.选择想要创建的响应的文件,此处输入html4.然后输入!按tab就行了

    2022年8月21日
    22
  • 九九乘法表Java代码

    九九乘法表Java代码九九乘法表Java代码如下packagecom.day03;publicclassTest27{ publicstaticvoidmain(String[]args){ /*99乘法表*/ for(inti=1;i<=9;i++){ for(intj=1;j<=i;j++){ System.out.print(i+”*”+j+”=”+j*i+”\t”); } System.out.print(“

    2022年7月7日
    21
  • with for什么意思_learning through interaction

    with for什么意思_learning through interactionLearningDiscriminativeFeatureswithMultipleGranularitiesforPersonRe-Identification(MGN)论文:LearningDiscriminativeFeatureswithMultipleGranularitiesforPersonRe-Identification,2018,cvpr.作者:云从科技代码:https://github.com/Gavin666Github/reid-mgn

    2022年9月27日
    5
  • OHEM 笔记

    OHEM 笔记说明1.本文是博主的学习记录,主要为了方便以后查看,当然如果能为别人提供帮助就更好了,如果有不对的地方请指正2.本文重点是了解OHEM算法以及它与hardnegativemining的异同点,尽量用较少篇幅表达清楚论文整体思路,其他一些不影响理解论文算法的东西有需要的请查看其他博客3.论文中的链接是我经过大量搜索,个人认为讲解最清楚的参考,看本文的过程中如对一些名词有疑惑可以直接点击参考…

    2022年5月7日
    38
  • 构建增强现实移动应用程序的六款顶级工具

    构建增强现实移动应用程序的六款顶级工具

    2021年6月6日
    125
  • 金士顿16G优盘_金士顿u盘格式化分配单元大小

    金士顿16G优盘_金士顿u盘格式化分配单元大小事情起因好好的金士顿16g优盘(绝对是真的,之前本人已经使用了2年多),今天本来准备用U盘装个win10系统,从微软官网下载了MediaCreationTool.exe用这个工具做了一个U盘系统,然后装系统(系统也没有装成。。。。。悲剧),谁知道重启之后,优盘可以识别,但是只显示一个盘符,没有容量,双击优盘,就显示请插入优盘之类的。换了一台电脑,插上U盘,显示需要格式化,那就格式化吧。。。。。。几

    2025年10月17日
    4

发表回复

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

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