- 浏览: 492605 次
- 性别:
- 来自: 济南
文章分类
最新评论
-
Ruby_lgr:
这样写了还是绕不过证书呢
httpclient 无信任证书使用https -
shihengli2010:
li2hengyuan 写道楼主 MySSLProtocol ...
httpclient 无信任证书使用https -
wangyudong:
很多API doc生成工具生成API文档需要引入第三方依赖,重 ...
httpclient 无信任证书使用https -
wangyudong:
由CXF实现的微服务需要有比较好的工具去测试RESTful A ...
CXF bus interceptor配置 -
xlzxlw:
多谢大师!帮到我了!这个类很有用!!感谢!!!
httpclient 无信任证书使用https
JavaEE路径陷阱之getRealPath 转
- 博客分类:
- JAVA
JavaEE路径陷阱之getRealPath
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方法。
发表评论
-
spring-security use-expressions详解
2012-05-14 08:42 15810使用 Spring 表达式语言配置访问控制基于角色标准投票机制 ... -
freemarke之TemplateDirectiveModel详解
2012-03-19 22:40 16203TemplateDirectiveModel接口是 ... -
threadlocal 使用
2011-11-23 15:16 1097深入研究java.lang.ThreadLocal类 ... -
spring bean 加载顺序
2011-11-22 17:29 10676问题来源: 有一个bean为A,一个bean为B。想要A在容 ... -
httpclient 无信任证书使用https
2011-10-14 15:36 278111、当不需要使用任何证书访问https网页时,只需配置信任任何 ... -
probe 使用
2011-06-30 08:56 5361TOMCAT创建和发布WEB ... -
jvm细节
2011-06-29 16:34 1411java的垃圾回收对程序 ... -
jms特性
2011-06-09 16:51 14911. 消息通信模型 JMS ... -
td 边框
2011-03-25 14:36 6325同时用样式表为 table、td 指定了边框后,可能会发生 ... -
rubby和java 充血与贫血模型比较
2011-02-15 14:54 2373domain model,又称为领域模型,是Java企业应用讨 ... -
职位简称
2011-02-15 14:44 1595PG: 程序员 (Programer)SE:软件工程师(系统工 ... -
JDK降级到1.4
2011-01-29 14:50 14731 使用工具Retrotranslator ... -
再说synchronized
2010-12-22 10:28 1105JAVA中synchronized关键字能够作为函数的修饰符, ... -
java获取操作系统信息
2010-12-20 15:44 1067Properties props=System.getProp ... -
java 线程中一些模糊概念的解释
2010-12-07 09:26 11761,并发与并行 并 ... -
java socket 阻塞
2010-11-22 09:30 10096本篇文章观点和例子来自 《Java网络编程精解》, 作者为孙卫 ... -
synchronized 使用
2010-11-05 16:10 941在某个方法中使用 synchronized(this) { ... -
log4j 使用
2010-11-04 09:52 953Log4j使用总结 收藏 一 ... -
xfire 返回复杂对象list
2010-10-21 16:56 1794<?xml version="1.0" ... -
struts2标签
2010-10-21 12:30 899struts2的标签库很强大,主要的标签有: A: < ...
相关推荐
javaEE开发中各种路径配置的总结,例如前端页面的相对路径的格式等
javaEE-web的文件路径, 用getClass().getResourceAsStream("/")来确定文件路径 并用jdom读取xml和下载1
JavaEE课程设计:票据管理系统源码 JavaEE课程设计:票据管理系统源码 JavaEE课程设计:票据管理系统源码 JavaEE课程设计:票据管理系统源码 JavaEE课程设计:票据管理系统源码 JavaEE课程设计:票据管理系统...
尚硅谷JavaEE&技术之&MyBatisPlus;
JavaEE 30招 JavaEE架构师进阶之路.zip
最全最清楚的Java 以及JavaEE 相对路径和绝对路径解析,能让我们正确使用相对路径
《JavaEE编程课程设计》期末项目报告书.docx《JavaEE编程课程设计》期末项目报告书.docx《JavaEE编程课程设计》期末项目报告书.docx《JavaEE编程课程设计》期末项目报告书.docx《JavaEE编程课程设计》期末项目报告书...
JAVAEE期中考试试题JAVAEE期中考试试题JAVAEE期中考试试题JAVAEE期中考试试题JAVAEE期中考试试题JAVAEE期中考试试题JAVAEE期中考试试题JAVAEE期中考试试题JAVAEE期中考试试题JAVAEE期中考试试题JAVAEE期中考试试题...
class文件生成java源代码 javaee src,javaee源代码
JavaEE
javaEE概述
javaEE
JavaEE教程
javaEE android 简单界面对比
javaee是企业中的中级技术!这是我的拙作! 本作品采用了javaee标准!希望能对大家有所帮助!
可在 eclipse 中查看 java javaee 源码java javaee 源码java javaee 源码java javaee 源码java javaee 源码java javaee 源码java javaee 源码java javaee 源码
该文件为javaEE实现简易博客工程源代码,其中css部分为网上找的模板,不妥删。其余业务流程代码均为原创。 嗯,是我们javaEE结课要求的大作业,可以用来作参考。
JavaEE期末复习模拟题
javaee官方英文版api文档,zip格式。需要的可以转换为chm格式
JAVAEE系列笔记2JAVAEE系列笔记2JAVAEE系列笔记2JAVAEE系列笔记2JAVAEE系列笔记2JAVAEE系列笔记2JAVAEE系列笔记2