`

JavaEE路径陷阱之getRealPath

    博客分类:
  • j2ee
阅读更多
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方法
分享到:
评论

相关推荐

    javaee路径总结

    javaEE开发中各种路径配置的总结,例如前端页面的相对路径的格式等

    javaee大作业实例

    1. **Servlet**:Servlet是JavaEE中最基础的组件之一,用于处理HTTP请求。开发者通常会用它来创建动态网页,接收和响应用户的数据。 2. **JSP(JavaServer Pages)**:JSP是JavaEE中的视图层技术,允许开发者在HTML...

    JavaEE课程设计:票据管理系统源码.zip

    JavaEE课程设计:票据管理系统源码 JavaEE课程设计:票据管理系统源码 JavaEE课程设计:票据管理系统源码 JavaEE课程设计:票据管理系统源码 JavaEE课程设计:票据管理系统源码 JavaEE课程设计:票据管理系统...

    JavaEE源码javaEE相关安装包及教程

    JavaEE 应用程序的运行需要一个支持JavaEE标准的应用服务器。常见的JavaEE应用服务器包括: - **GlassFish**:由Oracle提供的开源JavaEE应用服务器。 - **WildFly**(原名JBoss AS):Red Hat公司的开源应用服务器。...

    javaee_src.zip

    JavaEE Src.zip是一个压缩包,其中包含了JavaEE应用程序开发的源代码。JavaEE,全称为Java Platform, Enterprise Edition,是Java平台的一个版本,专为构建企业级应用而设计。它提供了一系列的API和服务,用于开发...

    JavaEE期末复习模拟题

    JavaEE是Java企业版(Java Enterprise Edition)的简称,它是一个用于开发企业级应用程序的Java平台。这个平台包含了多个服务、APIs和协议,旨在帮助开发者构建可扩展、高性能、安全且可管理的网络应用程序。JavaEE...

    javaEE结课作业之简易博客网站

    在本项目"javaEE结课作业之简易博客网站"中,我们主要探讨的是如何使用JavaEE技术栈构建一个基本的博客系统。这个项目的核心在于理解并应用JavaEE的相关概念和技术,包括Servlet、JSP、JavaBean以及前端CSS样式。...

    JAVAEE视频教程下载地址

    在标题“JAVAEE视频教程下载地址”以及描述“JAVAEE学习教程下载地址集合,还有外语、经济之类也在其中”中,我们可以了解到这是一份集合了多个JAVAEE学习视频教程的下载地址清单。除此之外,还包含了一些外语和经济...

    JavaEE 30招 JavaEE架构师进阶之路.zip

    JavaEE 30招 JavaEE架构师进阶之路.zip

    javaee_api.zip_JavaEE_Javaee的api_Javax.javaee-api_javaee api 文档_

    1. **Servlet**:Servlet是JavaEE中的核心组件之一,用于处理HTTP请求和响应。它扩展了Web服务器的功能,使得服务器能够处理动态内容。Servlet API定义了Servlet类和ServletConfig接口,以及Servlet生命周期相关的...

    最全和最清楚的Java(Java EE)相对(绝对)路径解析

    最全最清楚的Java 以及JavaEE 相对路径和绝对路径解析,能让我们正确使用相对路径

    javaEE android 简单界面对比

    JavaEE和Android是两个在IT领域中非常重要的平台,它们分别在服务器端和移动设备端发挥着关键作用。本文将对比分析JavaEE和Android在创建简单界面时的特点、技术和应用场景。 首先,JavaEE(Java Enterprise ...

    javaee网络工程期末大作业

    【JavaEE网络工程期末大作业】是一门涵盖了Java企业级应用开发核心技能的课程实践,主要目标是让学生掌握JavaEE平台上的应用程序设计与实现。在这个期末大作业中,学生需要运用所学的知识,构建一个完整的网络工程...

    javaee 包,javaee.jar

    在JavaEE体系中,Servlet是核心组件之一,它是一种服务器端的Java API,用于处理HTTP请求。Servlet可以用来接收来自客户端的请求,进行业务逻辑处理,然后返回响应。"servlet必备"表明这个压缩包可能包含了开发...

    javaee.jar包下载

    为了方便开发,你可以直接下载提供的"javaeejar包",将其添加到项目的类路径(ClassPath)中,这样就可以顺利地使用JavaEE的各种服务和API,进行Web应用的开发和部署了。 总结一下,JavaEE.jar是JavaEE平台的核心...

    javaee课程源代码

    JavaEE,全称为Java Platform, Enterprise Edition,是Java平台企业版的简称,是一个用于开发企业级应用程序的框架。它提供了一系列API和服务,使得开发者能够构建分布式、多层架构的Web应用,包括数据库连接、安全...

    JAVAEE中文版(最新)

    1. **Servlet**:Servlet是JavaEE中的核心组件之一,它是一个Java类,用于扩展服务器的功能。Servlet可以处理HTTP请求,生成响应,并且能够动态生成网页内容。 2. **JSP(JavaServer Pages)**:JSP是一种基于Java...

    javaEE概述

    javaEE概述

    JavaEE项目案例源码

    【JavaEE项目案例源码】是一个典型的Web开发实践,展示了如何使用JSP(JavaServer Pages)和JavaBean技术构建一个在线订购子系统。这个案例涵盖了JavaEE平台中的多个关键组件和技术,旨在帮助开发者理解实际应用中的...

Global site tag (gtag.js) - Google Analytics