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


相关推荐

  • SQLldr_sqlldr远程数据库

    SQLldr_sqlldr远程数据库1.SQLLDR导入 1.1简介 SQL*LOADER是ORACLE的数据加载工具,通常用来将操作系统文件(数据)迁移到ORACLE数据库中。SQL*LOADER是大型数据仓库选择使用的加载方法,因为它提供了最快速的途径(DIRECT,PARALLEL)。 2.2语法和参数语法:SQLLDRkeyword=value[,keyword=value,..

    2025年7月30日
    3
  • word如何一键全选,word怎么全选所有内容(word文档的快捷键操作)「建议收藏」

    word如何一键全选,word怎么全选所有内容(word文档的快捷键操作)「建议收藏」全选快捷键能够提升我们在实际操作word时工作效能,在实际操作Word2003中如何对文本文档中的文本开展选中呢?下边为大伙儿出示几类选中的方式,肯定功能强大。Word如何选中?方式一、应用Word全选快捷键“CtrlA”开展选中(也适用excel表);方式二、进行工具栏中的“编写”,随后挑选“选中”按键来选中;方式三、运用电脑鼠标选中,鼠标左键按着没放随后拖拽到最终还可以选中;方式…

    2022年5月25日
    131
  • keras提供的网络_kubernetes网络

    keras提供的网络_kubernetes网络GoogleNet网络详解与keras实现GoogleNet网络详解与keras实现GoogleNet系列网络的概览Pascal_VOC数据集第一层目录第二层目录第三层目录InceptionV1模块介绍Inception的架构GoogleNet的图片Keras代码实现为了搭建Inception网络我们使用了以下策略整个代码的流程如下实验结果实验结果分析本博客相关引用本

    2022年8月14日
    5
  • touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event使用

    touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event使用iOSProgramming–触摸事件处理(2)        在上一篇《iOSProgramming–触摸事件处理(1)》中了解触摸、事件和响应者之后,接下去学习如何处理用户的触摸事件。首先触摸的对象是视图,而视图的类UIView继承了UIRespnder类,但是要对事件作出处理,还需要重写UIResponder类中定义的事件处理函数。根据不通的触摸状态,程序会调用相应的处理

    2022年7月25日
    7
  • 对红黑树的认识总结

    一、对红黑树的基本理解(一)对红黑树的基本定义理解(二)对红黑树是“近似平衡”的理解1.将红色节点从红黑树中去掉,分析包含黑色节点的红黑树的高度2.把红色节点加回去,分析高度变化(三)红黑树与AVL树的比较:二、实现红黑树的基本思想分析(一)理解左旋(rotateleft)、右旋(rotateright)操作(二)插入操作的平衡调整情况一:如果关注节点是a,它的叔叔节点d是红色情况二:如果关注节点是a,它的叔叔节点d是黑色,关注节点

    2022年4月11日
    44
  • DRDS 柔性事务漫谈

    DRDS 柔性事务漫谈

    2022年4月2日
    65

发表回复

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

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