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


相关推荐

  • 数字证书原理,公钥私钥加密原理 – 因为这个太重要了[通俗易懂]

    文中首先解释了加密解密的一些基础知识和概念,然后通过一个加密通信过程的例子说明了加密算法的作用,以及数字证书的出现所起的作用。接着对数字证书做一个详细的解释,并讨论一下windows中数字证书的管理,最后演示使用makecert生成数字证书。如果发现文中有错误的地方,或者有什么地方说得不够清楚,欢迎指出!1、基础知识这部分内容主要解释一些概念和术语,最好是先理解这…

    2022年4月11日
    33
  • 京东数据库泄露事件分析报告_某网站的用户数据库泄露

    京东数据库泄露事件分析报告_某网站的用户数据库泄露猪猪侠·2014/03/0112:020×00背景昨天一张截图在QQ群里疯传,并说京东数据库泄露,里面有钱的尽快转。有人问“我挨个试了一下,只有3个可以正常登陆,而且网上也并没有完整的数据库流出,所以我妄猜是否是支付宝或者是其他竞争对手的营销手段?”这件事件到底是真的假的呢?经过我的研究,京东的数据库没有被脱裤,只是无聊黑客的恶作剧,他们通过收集互联网已泄露的用户+密码信息,生成对应的字典…

    2022年9月19日
    0
  • 软件架构设计—软件架构概述[通俗易懂]

    软件架构设计—软件架构概述[通俗易懂]像学写文章一样,在学会字、词、句之后,就应上升到段落,就应追求文章的“布局谋篇”,这就是架构。通俗地讲,软件架构设计就是软件系统的“布局谋篇”。人们在软件工程实践中,逐步认识到了软件架构的重要性,从而开辟了一个崭新的研究领域。软件架构的研究内容主要涉及软件架构描述、软件架构设计、软件架构风格、软件架构评价和软件架构的形成方法等。软件设计人员学习软件架构知识旨在站在…

    2022年5月7日
    65
  • i386 i586 i686 的意思「建议收藏」

    i386 i586 i686 的意思「建议收藏」代表intel系列的cpu386几乎适用于所有的x86平台,不论是旧的pentum或者是新的pentum-IV与K7系列的CPU等等,都可以正常的工作!那个i指的是Intel兼容的CPU的意思,至于386不用说,就是CPU的等级啦!i586就是586等级的计算机,那是哪些呢?包括pentum第一代MMXCPU,AMD的K5,K

    2022年6月7日
    37
  • 芯片的架构_意法半导体

    芯片的架构_意法半导体在了解这些架构之前,我们应该先了解一下复杂指令集(CISC)和精简指令集(RISC)。怎么说这两个的区别呢?CISC的设计思路更加注重性能的发展,是一种高性能高功耗的芯片,在高密度的计算上更具有优势;RISC的设计思路更注重低功耗小尺寸,多用于移动端设备,在重复性任务上占优。举一个简单的例子来说明这个情况,我们在B站上常说的一键三连,CISC会把“点赞”“投币”“收藏”整理成一条指令在缓存中,再由处理器处理;但是对于RISC来说就是三条指令了先“点赞”再“投币”最后“收藏”,这样做的缺点就是很依赖内存带宽了

    2022年9月7日
    0
  • mybatis返回map集合_java遍历map的key和value

    mybatis返回map集合_java遍历map的key和value匿名用户1级2018-06-10回答一、概述MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在。在MyBatis进行查询映射时,其实查询出来的每一个属性都是放在一个对应的Map里面…

    2022年10月5日
    1

发表回复

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

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