惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Google Online Security Blog
Google Online Security Blog
T
Threat Research - Cisco Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
W
WeLiveSecurity
NISL@THU
NISL@THU
V
Vulnerabilities – Threatpost
V2EX - 技术
V2EX - 技术
Cisco Talos Blog
Cisco Talos Blog
Simon Willison's Weblog
Simon Willison's Weblog
The Last Watchdog
The Last Watchdog
P
Privacy International News Feed
博客园 - 三生石上(FineUI控件)
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Scott Helme
Scott Helme
C
Cybersecurity and Infrastructure Security Agency CISA
Cyberwarzone
Cyberwarzone
量子位
Security Archives - TechRepublic
Security Archives - TechRepublic
Microsoft Azure Blog
Microsoft Azure Blog
F
Fortinet All Blogs
H
Hacker News: Front Page
D
Docker
N
Netflix TechBlog - Medium
阮一峰的网络日志
阮一峰的网络日志
G
Google Developers Blog
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
Last Week in AI
Last Week in AI
H
Hackread – Cybersecurity News, Data Breaches, AI and More
K
Kaspersky official blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
爱范儿
爱范儿
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
小众软件
小众软件
S
Security Affairs
博客园 - 聂微东
AI
AI
Latest news
Latest news
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Engineering at Meta
Engineering at Meta
Cloudbric
Cloudbric
S
Secure Thoughts
Spread Privacy
Spread Privacy
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org

博客园 - sunliho

Java ClassLoader and Context ClassLoader find flow scope varibles in spring web flow Building Liferay on Tomcat Debugging Liferay in Eclipse ssl 服务器证书,客户端信任证书 db2 command 标准sql Linux系统下架设APACHE SVN服务器全过程 修改ip地址 mysql in linux network configuration in linux Linux系统信息查看命令大全 linux常用命令 Permission denied in linux install jdk in linux generic dao hibernate.cfg.xml 配置(摘录) - sunliho - 博客园 hibernate 配置文件(摘录) - sunliho - 博客园 web.xml配置详解(摘录)
相对路径与绝对路径 - sunliho - 博客园
sunliho · 2010-09-06 · via 博客园 - sunliho

1.基本概念的理解  
 
绝对路径:绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如: C:\xyz\test.txt 代表了test.txt文件的绝对路径。http://www.sun.com/index.htm也代表了一个 URL绝对路径。
  
相对路径:相对与某个基准目录的路径。包含Web的相对路径(HTML中的相对目录),例如:在 Servlet中,"/"代表Web应用的跟目录。和物理路径的相对表示。例如:"./" 代表当前目录, "../"代表上级目录。这种类似的表示,也是属于相对路径。 另外关于URI,URL,URN等内容,请参考RFC相关文档标准。 RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax, (http://www.ietf.org/rfc/rfc2396.txt)

2.关于JSP/Servlet中的相对路径和绝对路径。

    2.1服务器端的地址   
         服务器端的相对地址指的是相对于你的web应用的地址,这个地址是在服务器端解析的 (不同于html和javascript中的相对地址,他们是由客户端浏览器解析的)也就是说这时候 在jsp和servlet中的相对地址应该是相对于你的web应用,即相对于http://192.168.0.1/webapp/的。
   其用到的地方有: forward:servlet中的request.getRequestDispatcher(address);这个address是 在服务器端解析的,所以,你要forward到a.jsp应该这么写: request.getRequestDispatcher(“/user/a.jsp”)这个/相对于当前的web应用webapp, 其绝对地址就是:http://192.168.0.1/webapp/user/a.jsp。 sendRedirect:在jsp中

   2.2、客户端的地址 所有的html页面中的相对地址都是相对于服务器根目录(http://192.168.0.1/)的, 而不是(跟目录下的该Web应用的目录)http://192.168.0.1/webapp/的。 Html中的form表单的action属性的地址应该是相对于服务器根目录(http://192.168.0.1/)的, 所以,如果提交到a.jsp为:action="/webapp/user/a.jsp"或action=""/user/a.jsp;
   提交到servlet为actiom="/webapp/handleservlet"
  Javascript也是在客户端解析的,所以其相对路径和form表单一样。
   因此,一般情况下,在JSP/HTML页面等引用的CSS,Javascript.Action等属性前面最好都加上 ,以确保所引用的文件都属于Web应用中的目录。 另外,应该尽量避免使用类似".","./","http://www.cnblogs.com/"等类似的相对该文件位置的相对路径,这样 当文件移动时,很容易出问题。

3. JSP/Servlet中获得当前应用的相对路径和绝对路径
   3.1 JSP中获得当前应用的相对路径和绝对路径 根目录所对应的绝对路径:request.getRequestURI() 文件的绝对路径

 :application.getRealPath(request.getRequestURI());
当前web应用的绝对路径 :application.getRealPath("/");
取得请求文件的上层目录:new File(application.getRealPath(request.getRequestURI())).getParent()

3.2 Servlet中获得当前应用的相对路径和绝对路径 根目录所对应的绝对路径:request.getServletPath();

文件的绝对路径 :request.getSession().getServletContext().getRealPath (request.getRequestURI())
当前web应用的绝对路径 :servletConfig.getServletContext().getRealPath("/");
(ServletContext对象获得几种方式: javax.servlet.http.HttpSession.getServletContext() javax.servlet.jsp.PageContext.getServletContext() javax.servlet.ServletConfig.getServletContext() )

 4.java 的Class中获得相对路径,绝对路径的方法
   4.1单独的Java类中获得绝对路径

   根据java.io.File的Doc文挡,可知: 默认情况下new File("/")代表的目录为:
System.getProperty("user.dir")。 一下程序获得执行类的当前路径
package org.cheng.file;
import java.io.File;
 public class FileTest {
 public static void main(String[] args) throws Exception { System.out.println(Thread.currentThread().getContextClassLoader().getResource("")); System.out.println(FileTest.class.getClassLoader().getResource(""));   System.out.println(ClassLoader.getSystemResource("")); System.out.println(FileTest.class.getResource(""));
System.out.println(FileTest.class.getResource("/"));
 //Class文件所在路径
System.out.println(new File("/").getAbsolutePath());
 System.out.println(System.getProperty("user.dir"));
 }
 }

  4.2服务器中的Java类获得当前路径(来自网络)

 (1).Weblogic WebApplication的系统文件根目录是你的weblogic安装所在根目录。 例如:如果你的weblogic安装在c:\bea\weblogic700..... 那么,你的文件根路径就是c:\. 所以,有两种方式能够让你访问你的服务器端的文件:
      a.使用绝对路径: 比如将你的参数文件放在c:\yourconfig\yourconf.properties, 直接使用 new FileInputStream("yourconfig/yourconf.properties");
      b.使用相对路径: 相对路径的根目录就是你的webapplication的根路径,即WEB-INF的上一级目录,将你的参数文件放 在yourwebapp\yourconfig\yourconf.properties, 这样使用: new FileInputStream("./yourconfig/yourconf.properties"); 这两种方式均可,自己选择。
 (2).Tomcat 在类中输出System.getProperty("user.dir");显示的是%Tomcat_Home%/bin
 (3).Resin 不是你的JSP放的相对路径,是JSP引擎执行这个JSP编译成SERVLET 的路径为根.比如用新建文件法测试File f = new File("a.htm"); 这个a.htm在resin的安装目录下 (4).如何读相对路径哪? 在Java文件中getResource或getResourceAsStream均可 例:getClass().getResourceAsStream(filePath);//filePath可以是"/filename",这里的/代表web 发布根路径下WEB-INF/classes 默认使用该方法的路径是:WEB-INF/classes。已经在Tomcat中测试。

 5.读取文件时的相对路径,避免硬编码和绝对路径的使用。