Java absolute path and relative path summary __java series

Source: Internet
Author: User
Tags parent directory rfc
1. Understanding of Basic Concepts

Absolute path: Absolute path is the real path of the file or directory on your home page on your hard disk, (URL and physical path) such as:
The c:xyz est.txt represents the absolute path of the Test.txt file. Http://www.sun.com/index.htm also represents a URL absolute path.

Relative path: The path relative to a base directory. Contains the relative path of the web (relative directories in HTML), for example: in
Servlet, "/" represents the directory of Web applications. The relative representation of the physical path. For example: "./" represents the current directory, "... /"represents the parent directory. This similar representation is also a relative path.
For additional information about Uri,url,urn, please refer to the RFC related documentation standard.

RFC 2396:uniform Resource Identifiers (URI): Generic Syntax,
(Http://www.ietf.org/rfc/rfc2396.txt)


2. About relative paths and absolute paths in Jsp/servlet.

2.1 Server-side addresses

The relative address on the server side refers to the address of your Web application, which is resolved on the server side (unlike the relative addresses in HTML and JavaScript, They are parsed by the client browser, which means that the relative address in the JSP and servlet at this time should be relative to your Web application, that is, relative to http://192.168.0.1/webapp/.

The places it uses are:
Forward:servlet in Request.getrequestdispatcher (address); This address is resolved on the server side, so You have to forward to a.jsp should write this: Request.getrequestdispatcher ("/user/a.jsp") this/relative to the current Web application WebApp, its absolute address is: http:// 192.168.0.1/webapp/user/a.jsp. Sendredirect: <%response.sendredirect in JSP ("/rtccp/user/a.jsp");%>

2.22, the client's address

The relative addresses in all HTML pages are relative to the server root (HTTP://192.168.0.1/) rather than to the directory of the Web application (in the directory) http://192.168.0.1/webapp/. The address of the form's action attribute in HTML should be relative to the server root (HTTP://192.168.0.1/), so if you commit to a.jsp as: action= "/webapp/user/a.jsp" or "action=" <%=request.getcontextpath ()% > "/user/a.jsp;
Submitting to the servlet for actiom= "/webapp/handleservlet" JavaScript is also resolved on the client side, so its relative path is the same as the form form.


Therefore, in general, in front of the jsp/html page, such as references to Css,javascript.action, and so on, it is best to add
<%=request.getcontextpath ()%&gt to ensure that the referenced files belong to a directory in the Web application. In addition, you should try to avoid using a similar ".", "./", ".. /.. /"A relative path relative to the location of the file, so that when the file is moved, it can easily go wrong.


3. Obtain the relative and absolute paths of the current application in Jsp/servlet

3.1 jsp to get the relative path and absolute path of the current application
Absolute path for root directory: Request.getrequesturi ()
Absolute path of the file: Application.getrealpath (Request.getrequesturi ());
Absolute path to the current Web application: Application.getrealpath ("/");
Gets the upper-level directory of the request file: New file (Application.getrealpath (Request.getrequesturi ())). GetParent ()

The relative and absolute paths of the current application are obtained in the 3.2 servlet
The absolute path of the root directory: Request.getservletpath ();
Absolute path of File: Request.getsession (). Getservletcontext (). Getrealpath
(Request.getrequesturi ())
Absolute path of the current Web application: Servletconfig.getservletcontext (). Getrealpath ("/");
(ServletContext objects get several ways:
Javax.servlet.http.HttpSession.getServletContext ()
Javax.servlet.jsp.PageContext.getServletContext ()
Javax.servlet.ServletConfig.getServletContext ()
)

The method of obtaining the relative path and absolute path in the class of 4.java

4.1 Absolute paths in a separate Java class
According to Java.io.File's doc block, we know:
By default, the directory represented by new File ("/") is: System.getproperty ("User.dir").
The program gets the current path of the execution class
  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 file path          system.out.println (New file ("/"). GetAbsolutePath ());            system.out.println (System.getproperty ("User.dir"));        }  }  

4.2 Java classes in server get current path (from Network)

(1). Weblogic

The WebApplication system file root directory is the root directory where your WebLogic installation resides.
For example: If your WebLogic is installed in c:eaweblogic700 ...
So, your file root path is C:.
So there are two ways that you can access your server-side files:
A. Use absolute paths:
Like putting your parameter file in C:yourconfigyourconf.properties,
Direct use of new FileInputStream ("Yourconfig/yourconf.properties");
B. Use relative paths:
The root directory of the relative path is the root path of your webapplication, the Web-inf directory, where you put your parameter files

In Yourwebappyourconfigyourconf.properties,
Use this:
New FileInputStream ("./yourconfig/yourconf.properties");
Both of these options are available to you.

(2). Tomcat

Output System.getproperty ("User.dir") in the class;%tomcat_home%/bin is displayed.

(3). Resin

Not your JSP put the relative path, is the JSP engine to execute this JSP compiled into a servlet
The path to the root. For example, to test the file F = new file ("A.htm") with the new document method;
This a.htm is under the resin installation directory.

(4). How to read relative paths.

GetResource or getResourceAsStream in a Java file can be

Example: GetClass (). getResourceAsStream (FilePath);//filepath can be "/filename", here/on behalf of the Web

Publish Root Path under Web-inf/classes

The default path for using this method is: web-inf/classes. has been tested in Tomcat.

5. Read the file relative path, to avoid hard coding and absolute path of use. (From Network)
5.1 Use spring's di mechanism to get files and avoid hard coding.
Refer to the following connection content:
http://www.javajia.net/viewtopic.php?p=90213&
5.2 Configuration file Read
Refer to the following connection content:
Http://dev.csdn.net/develop/article/39/39681.shtm

5.3 Read an XML file through a virtual path or relative path to avoid hard coding

Refer to the following connection content:
Http://club.gamvan.com/club/clubPage.jsp?iPage=1&tID=10708&ccID=8

Common operations for files in 6.Java (copy, move, delete, create, etc.) (from Network)
Common Java File Action classes
Http://www.easydone.cn/014/200604022353065155.htm

Java File Operations Encyclopedia (in JSP)
Http://www.pconline.com.cn/pcedu/empolder/gj/java/0502/559401.html

Java file Operations Detailed (Java Chinese network)
Http://www.51cto.com/html/2005/1108/10947.htm

JAVA How to create delete modify copy directory and file
Http://www.gamvan.com/developer/java/2005/2/264.html

Summarize:
Through the use of the above content, you can solve the Web application server side, mobile files, find files, copy
Delete files and other operations, at the same time, the relative address of the server, absolute address concept more clearly.
Recommended reference URI, the RFC standard text block. and understand the contents of Java.io.File Java.net.URI.
Other aspects of the understanding can be more in-depth and thorough.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.