Java to obtain the project access path, java to obtain the project path

Source: Internet
Author: User

Java to obtain the project access path, java to obtain the project path

The relative paths called in jsp and class files are different.

In jsp, the root directory is WebRoot in the class file, the root directory is WebRoot/WEB-INF/classes of course you can also use System. getProperty ("user. dir ") to obtain the absolute path of your project.

1. Obtain the path in jsp:
Take the project name TEST as an example:
(1) obtain the full path of the current page containing the Project name: request. getRequestURI ()
Result:/TEST/test. jsp
(2) obtain the Project name: request. getContextPath ()
Result:/TEST
(3) Get the full name in the directory where the current page is located: request. getServletPath ()
Result: If the page is in the/TEST/jsp/test. jsp directory
(4) obtain the full path of the server where the page is located: application. getRealPath ("Page. jsp ")
Result: D: \ resin \ webapps \ TEST \ test. jsp
(5) obtain the absolute path of the server where the page is located: absPath = new java. io. File (application. getRealPath (request. getRequestURI (). getParent ();
Result: D: \ resin \ webapps \ TEST

2. Obtain the path in the class:
(1) absolute path of the Class: class. Class. getClass (). getResource ("/"). getPath ()
Result:/D:/TEST/WebRoot/WEB-INF/classes/pack/
(2) obtain the project path: System. getProperty ("user. dir ")
Result: D: \ TEST

3. Get the path in Servlet:
(1) obtain the project directory: the request. getSession (). getServletContext (). getRealPath ("") parameter can be specific to the package name.
Result: E: \ Tomcat \ webapps \ TEST
(2) obtain the address in the IE Address Bar: request. getRequestURL ()
Result: http: // localhost: 8080/TEST/test
(3) Get the relative address: request. getRequestURI ()
Result:/TEST/test

In addition, the Class also has a getResourceAsStream method. Remember to use this method to read an xml file in the same package from a previous project.

1. How to obtain the current file path

Frequently used:
(1). Test. class. getResource ("")
The URI Directory of the current FileTest. class file is obtained. Not yourself!
(2). Test. class. getResource ("/")
The absolute URI of the current classpath is obtained.
(3). Thread. currentThread (). getContextClassLoader (). getResource ("")
The obtained absolute URI of the current ClassPath.
(4). Test. class. getClassLoader (). getResource ("")
The obtained absolute URI of the current ClassPath.
(5). ClassLoader. getSystemResource ("")
The obtained absolute URI of the current ClassPath.
Try not to use the relative path relative to the current user directory of System. getProperty ("user. dir"). We can see that the results are varied later.
(6) new File (""). getAbsolutePath () is also available.

Note: if there are spaces in these paths and the spaces are replaced, you can use path = java.net. URLDecoder. decode (path, "UTF-8") for conversion;

2. Web Server
(1). Tomcat
Output System. getProperty ("user. dir") in the class; % atat_home %/bin
(2). Resin
It is not the relative path of your JSP. It is the root path for the JSP Engine to compile the JSP 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.
(3). How to read files
Use ServletContext. getResourceAsStream ().
(4). Obtain the real path of the file

String file_real_path = ServletContext. getRealPath ("mypath/filename ");
Request. getRealPath ("/") is not recommended ("/");


How to obtain the class loading path and project root path in java

// Method 1: Obtain the root path D: \ git \ daotie \ target \ classesFile f = new File (this. getClass (). getResource ("/"). getPath (); System. out. println (f); // obtain the project path of the current class; if "/" is not added, obtain the loading directory of the current Class D: \ git \ daotie \ target \ classes \ myFile f2 = new File (this. getClass (). getResource (""). getPath (); System. out. println (f2); // Method 2: Obtain the project path D: \ git \ daotie \ daotieFile directory = new File (""); // The parameter is null String courseFile = directory. getCanonicalPath (); System. out. println (courseFile); // file:/D:/git/daotie/target/classes/URL xmlpath = this. getClass (). getClassLoader (). getResource (""); System. out. println (xmlpath); // type 4: D: \ git \ daotie \ daotieSystem. out. println (System. getProperty ("user. dir ");/** result: C: \ Documents and Settings \ Administrator \ workspace \ projectName * Get the current project path * // Category 5: obtain all the class paths, including the jar package paths

In java, if the access path is obtained?

In Javascript
1. Obtain the URL of the previous page
Var url = document. referrer;
2. Obtain the URL of the current page
Var ur = location. href;
Java
1. Obtain the URL of the previous page
Request. getHeader ("Referer ");
2. Obtain the relative URI path and parameters of the current request.
Request. getRequestURI () + "? "+ Request. getQueryString ();

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.