getservletcontext.getrealpath_request.getattribute取不到值

getservletcontext.getrealpath_request.getattribute取不到值关于serveletContext.getRealPath()方法1.关于request.getRealPath问题:Stringfilename=request.getRealPath(filename)——————-信息:warning:[deprecation]getRealPath(java.

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

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

关于serveletContext.getRealPath()方法

1.关于request.getRealPath
问题:
String filename=request.getRealPath(filename)
——————-
信息:
warning: [deprecation] getRealPath(java.lang.String) in javax.servlet.ServletRequest has been deprecated

解决:
这个getRealPath方法已经不建议使用了

参看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()这个方法受到war 和non-war的影响,以及不同app server实现的影响,运气好的话,你常常会得到null,嘿嘿,比如你在weblogic上部署war文件,又调用这个方法..

推荐ServletContext.getResourceAsStream


2.关于serveletContext.getRealPath返回NULL和不同的app server返回不同的结果

问题:

有几个配置文本配置文件(是一些报表的模板),放在WEB-INF下面的config目录下,程序中是这样得到这个config的实际路径的:
先用 serveletContext.getRealPath得到根路径,tomcat中比如是
c:\tomcat\webapp\test

然后我加上 “/WEB-INF/config/aa.config”,这样得到文件的path然后进行读入,应用在tomcat上跑是ok的,后来将war放到weblogic上,出错,原因是:
在weblogic上用getRealPath得到的是像
myserver\stage\_appsdir_test_war\test.war!\WEB-INF\config….
这样的路径,于是一直报FileNotFoundException

解决:

serveletContext.getRealPath
这个方法在不同的服务器上所获得的实现是不一样的, 建议是通过classloader来获得你配置的资源文件

context.getRealPath(“/”)可能返回了null,你可以输入来看看,
对一个打包的应用来说,是没有RealPath的概念的,调用getRealPath只会简单地返回null。其实,也很

好理解,一个文件被打包入了.war文件,就不存在目录结构了(虽然包中仍然存在目录结构,但这不等同于文件系统中的目录结构)。所以,对war包中的资源是无法得到RealPath的。这样也就无从通过文件IO进行读取了。

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


原则:基本上就是尽量使用j2ee规范中的各层次classloader来获取资源,而不是试图去找文件的绝对路
方法:调用this.getClass().getClassLoader().getResource(“/”).getPath(); 获取到classes目录的全路径

使用:在得到classes目录的全路径后再根据字符串的截取与拼装达到你的要求即可。



绝对不要使用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(“/”)这样的方法来获取应用程序的绝对路径。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

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

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


相关推荐

  • hadoop是什么意思_hadoop三大组件

    hadoop是什么意思_hadoop三大组件Hadoop是一个由Apache基金会所开发的分布式系统基础架构。用户可以在不了解分布式底层细节的情况下,开发分布式程序。充分利用集群的威力进行高速运算和存储。[1]Hadoop实现了一个分布式文件系统(HadoopDistributedFileSystem),简称HDFS。HDFS有高容错性的特点,并且设计用来部署在低廉的(low-cost)硬件上;而且它提供高吞吐量(highthro

    2022年9月29日
    2
  • 请简述MyBatis框架与Hibernate框架的区别_报告与总结的不同点

    请简述MyBatis框架与Hibernate框架的区别_报告与总结的不同点两者相同点Hibernate与MyBatis都可以是通过SessionFactoryBuider由XML配置文件生成SessionFactory,然后由SessionFactory生成Session,最后由Session来开启执行事务和SQL语句。其中SessionFactoryBuider,SessionFactory,Session的生命周期都是差不多的。Hibernate和MyBatis都…

    2025年10月18日
    5
  • id门禁卡复制到手机_门禁卡怎么复制到苹果手机?「建议收藏」

    id门禁卡复制到手机_门禁卡怎么复制到苹果手机?「建议收藏」NFC手机一枚RootExplorerNFCTaginfo方法/步骤1,读取卡的62616964757a686964616fe78988e69d8331333363396461ID。安装“NFCTagInfo”,打开手机的NFC设置,门禁卡贴到手机后盖NFC部分,“NFCTagInfo”读取校园卡ID。可以看到我的卡ID是13:67:A9:0A修改手机NFC的ID。打开R.E.管理器,根目…

    2022年5月2日
    261
  • 三星note3怎样刷原生Android,三星note三可以刷原生android系统吗?

    三星note3怎样刷原生Android,三星note三可以刷原生android系统吗?可以的。1刷之前要备份好个人的通讯录等资料。如果你的手机使用正常就不用去刷了。自己刷也是可以的,但要到网上下载手机软件,三星的网上版本多,有些是专为水货编写的。2刷机最好在风险可控前提下的刷机。当前DIY的版本都是基于原版的,只不过是将原来的图片替换成另外的图片,将原来的铃声替换成另外的铃声,没有动核心部分。只是替换更改了部分图片、铃声或者菜单字符等,所以不应该有不良影响,按步骤操作,刷机是基本上…

    2022年6月19日
    39
  • Excel字符串截取(left&right&mid)

    Excel字符串截取(left&right&mid)Excel中字符串截取函数主要有left、right和mid1.left函数  在字符串“wang”中,从左起截取2个字符,结果是“wa”。2.right函数  在字符串“wang”中,从右起截取2个字符,结果是“ng”。3.mid函数  从字符串“wang”第2个位置,截取长度为2的字符串,结果是“an”。

    2025年8月10日
    7
  • 电脑蓝屏错误代码0x000000ED_0x00000019蓝屏win7

    电脑蓝屏错误代码0x000000ED_0x00000019蓝屏win7相信大家都遇到过电脑蓝屏的情况,而且蓝屏故障是一件非常麻烦的事情,前不久小编电脑出现蓝屏并且提示错误代码是0x000000BE,很多用户可能会直接选择重装系统,其实不用这么麻烦,出现0x000000BE很有可能是硬件设备驱动程序存在BUG或安装不正确引起,具体一起来看看。解决方法:在开机过程中按下f8键进入Windows高级启动菜单,进入安全模式,也许会有改善。再重启电脑,继续按F8键,此时可以选…

    2022年10月8日
    2

发表回复

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

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