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:
C:\xyz\test.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.
2. Relative/absolute path 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 the http://192.168.0.1/webapp/.
The places it uses are:
Forward:servlet in the 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 ("/rtccp/user/a.jsp");%>
2.2, client's address in JSP
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 action= "/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 ()%> 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. Jsp/servlet the current relative/absolute path in the
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 ()
3.2 The relative and absolute paths of the current application are obtained in the 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 ()
4.Java Gets the absolute path in the class of the relative/absolute path
4.1 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 Com.itm.path; public class testsystemproperty{public static void Main (string[] args) {System.out.println ("Java Runtime Environment version: \ n" +s
Ystem.getproperty ("java.version"));
System.out.println ("Java Runtime Environment vendor: \ n" +system.getproperty ("Java.vendor"));
System.out.println ("Url:\n of Java Vendor" +system.getproperty ("Java.vendor.url"));
System.out.println ("Java installation directory: \ n" +system.getproperty ("Java.home"));
System.out.println ("Java Virtual machine spec version: \ n" +system.getproperty ("java.vm.specification.version"));
System.out.println ("Java class format version number: \ n" +system.getproperty ("java.class.version"));
System.out.println ("Java classpath: \ n" +system.getproperty ("Java.class.path"));
SYSTEM.OUT.PRINTLN ("List of paths to search when loading library: \ n" +system.getproperty ("Java.library.path"));
SYSTEM.OUT.PRINTLN ("Default temporary file path: \ n" +system.getproperty ("Java.io.tmpdir"));
System.out.println ("The name of the JIT compiler to use: \ n" +system.getproperty ("Java.compiler"));
SYSTEM.OUT.PRINTLN ("Path of one or more extended directories: \ n" +system.getproperty ("Java.ext.dirs")); SYstem.out.println ("Name of the operating system: \ n" +system.getproperty ("Os.name"));
SYSTEM.OUT.PRINTLN ("Architecture of the operating system: \ n" +system.getproperty ("Os.arch"));
System.out.println ("version of the operating system: \ n" +system.getproperty ("os.version"));
System.out.println ("File delimiter (in UNIX system is"/"): \ n" +system.getproperty ("File.separator"));
SYSTEM.OUT.PRINTLN ("The Path delimiter (in UNIX system is:"): \ n "+system.getproperty" ("Path.separator"));
System.out.println ("n" on UNIX system): \ n "+system.getproperty" ("Line.separator"));
System.out.println ("User's account name: \ n" +system.getproperty ("User.Name"));
System.out.println ("User's home directory: \ n" +system.getproperty ("User.home"));
System.out.println ("User's current working directory: \ n" +system.getproperty ("User.dir")); System.out.println ("uri notation for the absolute path of the current classpath:---->>: \ n" + thread.currentthread (). Getcontextclassloader ().
GetResource ("")); System.out.println ("Gets the URI directory of the current class Filetest.class file.) does not include himself.
: \ n "+testsystemproperty.class.getresource" ("")); System.out.println ("Gets the absolute URI path of the current classpath.") : \ n "+ TestsystempropErty.class.getResource ("/"));
}
}
4.2 Java classes in server get current path (from Network)
(2). Tomcat
Output System.getproperty ("User.dir") in the class; It shows%tomcat_home%/bin.
(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. Five Java path problem Final Solution
The following is from: http://blog.csdn.net/shendl/article/details/1427475
-relative path addressing for all resources can be located
Preface
Java path problem, very difficult to get. The most recent work involves creating and reading files, and I'll give you a thorough solution to the Java path problem.
I wrote a method that is more capable than a classloader.getresource (String relative path) method. It can be accepted ". /"Such parameters allow us to use relative paths to locate resources outside the classpath. In this way, we can use the path relative to the classpath to locate resources in all locations.
Java Path
The paths used in Java are divided into two types: absolute and relative. Specifically, it is divided into four kinds:
1, the absolute resource path of the URI form
such as: file:/d:/java/eclipse32/workspace/jbpmtest3/bin/aaa.b
A URL is a special case of a URI. The prefix/protocol of the URL must be known by Java. A URL can open a resource, and a URI is not.
URLs and Uri objects can be converted to each other, using the respective Touri (), Tourl () method.
2. Absolute path of the Local system
D:/java/eclipse32/workspace/jbpmtest3/bin/aaa.b
Java.io the class in the package, you need to use this form of argument.
However, they generally also provide parameters for the type of URI, and the parameters of the URI type accept a URI-style string. Therefore, by using URI conversion, you can also use the absolute path of the URI style in the class in the Java.io package.
relative path relative to Classpath
such as: relative to
file:/d:/java/eclipse32/workspace/jbpmtest3/bin/the relative path of this path. Among them, bin is the classpath of this project. All Java source file compiled. class files are copied to this directory.
This article is quoted from ITM_HADF's blog