[Java] How to obtain relative paths in Java

Source: Internet
Author: User
Tags rfc
1. understanding of basic concepts
Absolute path: the absolute path is the real path of the file or directory on your home page on the hard disk (URL and physical path), for example:
C: XYZ est.txt represents the absolute path of the test.txt file. http://www.sun.com/index.htmalso represents
Absolute URL path.
Relative Path: the path relative to a base directory. Contains the relative path of the Web (relative directory in HTML), for example:
In servlet, "/" indicates the relative representation of the Web application and directory. and physical path. For example, "./" indicates the current directory,
"../" Indicates the upper-level directory. This type is similar, and it is also a relative path.
For more information about Uri, URL, and urn, see RFC standards.
RFC 2396: Uniform resource identifiers (URI): Generic syntax, (http://www.ietf.org/rfc/rfc2396.txt)
2. About the relative paths and absolute paths in JSP/servlet.
2.1 server address
The relative address of the server is relative to the address of your web application. The address is resolved on the server.
(Different from the relative addresses in HTML and JavaScript, they are parsed by the client browser .)
The relative address in JSP and Servlet should be relative to your web application, that is, relative to http: // localhost/webapp.
Where it is used:
Forward: request. getrequestdispatcher (Address) in servlet; this address is
It is parsed on the server side. Therefore, you must forward to A. jsp as follows:
Request. getrequestdispatcher ("/user/a. jsp,
The absolute address is: http: // localhost/webapp/user/a. jsp.
Sendredirect: in JSP, you can write http: // localhost/test. jsp or relative paths, but the URL is displayed as a jump.
2.22, client address
The relative addresses of all HTML pages are relative to the server root directory (http: // localhost,
Instead of http: // localhost/webapp.
The address of the Form Action attribute in HTML is relative to the server root directory (http: // localhost,
Therefore, if the request is submitted to a. jsp as: Action = "/webapp/user/a. jsp" or action = "/user/a. jsp;
Submit to Servlet as actiom = "/webapp/handleservlet"
Javascript is parsed on the client, so its relative path is the same as the form.
Therefore, in general, it is best to add CSS, JavaScript, action, and other attributes referenced in JSP/html pages.
To make sure all referenced files belong to the directory in the Web application.
In addition, try to avoid using relative paths similar to ".", "./", ".././", such as relative paths relative to the file location.
When the file is moved, it is prone to problems.
3. Obtain the relative and absolute paths of the current application in JSP/servlet.
3.1 obtain the relative and absolute paths of the current application in JSP
Absolute path corresponding to the root directory: request. getrequesturi ()
Absolute path of the file: application. getrealpath (request. getrequesturi ());
Absolute path of the current web application: application. getrealpath ("/");
Obtain the upper-level directory of the request file: New file (application. getrealpath (request. getrequesturi (). getparent ()
3.2 obtain the relative and absolute paths of the current application in Servlet
Absolute path corresponding to the root directory: request. getservletpath ();
Absolute path of the file: request. getsession (). getservletcontext (). getrealpath
(Request. getrequesturi ())
Absolute path of the current web application: servletconfig. getservletcontext (). getrealpath ("/");
(The servletcontext object can be obtained in the following ways:
Javax. servlet. http. httpsession. getservletcontext ()
Javax. servlet. jsp. pagecontext. getservletcontext ()
Javax. servlet. servletconfig. getservletcontext ()
)
4. Methods for obtaining relative paths and absolute paths in Java class
4.1 obtain the absolute path in a separate Java class
According to the java. Io. File doc, we can see that:
By default, the directory represented by new file ("/") is: system. getproperty ("user. dir ").
The program obtains 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 ("/"); // path of the class file
System. Out. println (new file ("/"). getabsolutepath ());
System. Out. println (system. getproperty ("user. dir "));
}
}
4.2 The Java class on the server obtains the current path
(1). WebLogic
The webapplication System File root directory is the root directory where your WebLogic installation is located.
For example, if your WebLogic is installed in C: eaweblogic700 .....
The root path of your file is C :.
Therefore, you can access files on your server in two ways:
A. Use absolute path:
For example, put your parameter file in C: yourconfigyourconf. properties,
Use new fileinputstream ("yourconfig/yourconf. properties") directly ");
B. Use the relative path:
The root directory of the relative path is the root path of your webapplication, that is, the upper-level directory of the WEB-INF, put your parameter file
In yourwebappyourconfigyourconf. properties,
This method is used as follows:
New fileinputstream ("./yourconfig/yourconf. properties ");
You can select either of the two methods.
(2). Tomcat
Output System. getproperty ("user. dir") in the class; % atat_home %/bin
(3). Resin
It is not the relative path of your JSP, but the JSP Engine executes this JSP to compile it into a Servlet
For example, use the new file method to test file F = new file ("a.htm ");
This a.htm is in the resin installation directory.
(4). How to read relative paths?
In a Java file, either getresource or getresourceasstream can be used.
For example, getclass (). getresourceasstream (filepath); // filepath can be "/FILENAME", where/Represents the Web
Publish WEB-INF/classes under root path
By default, the path to this method is: WEB-INF/classes. has been tested in Tomcat.
5. Read the relative path of the file to avoid hard encoding and absolute path usage.
5.1 use the di mechanism of spring to obtain files and avoid hard coding.
Refer to the following connection: http://www.javajia.net/viewtopic.php? P = 90213 &
5.2 read configuration files
Refer to the following connection: 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 Encoding
Refer to the following connection: http://club.gamvan.com/club/clubPage.jsp? Ipage = 1 & tid = 10708 & CCID = 8
6. Common Operations on files in Java (copying, moving, deleting, and creating)
Common Java File Operations http://www.easydone.cn/014/200604022353065155.htm
Java File Operations Daquan (JSP) http://www.pconline.com.cn/pcedu/empolder/gj/java/0502/559401.html
Java file operations (Java Chinese Network) http://www.51cto.com/html/2005/1108/10947.htm
Java how to create delete/modify copy directories and file http://www.gamvan.com/developer/java/2005/2/264.html
Summary:
By using the above content, you can perform operations on the Web application server, such as moving files, searching for files, copying and deleting files, and the relative address of the server. The concept of absolute address is clearer. we recommend that you refer to the standard RFC document in Uri. java. io. file. java.net. uri. and so on.
Related Article

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.