Document directory
- 3.1. General method 1
- 3.2. General method 2 (this method is similar to the method in 1.1. The difference is that this method must start)
- 4.1. Starting from Servlet
- 4.2. Starting from httpservletrequest
- 5.1. method 1
- 5.2. method 2
- 6.1. Get path in JSP:
- 6.2. Obtain the path in the class:
- 6.3. Get the path in servlet:
Refer to network articles for more information. 1. Obtain the real physical path of the web server project.
Request. getSession (). getServletContext (). getRealPath ("/");
2. Obtain relative paths
Note: You can obtain the relative path (that is, if you do not specify the relative path) in the following ways (whether it is a general java project or a web project)
String relativelyPath = System. getProperty ("user. dir ");
In the above relative path, the files in the java project are relative to the root directory of the project.
File paths in web projects vary depending on different web Servers
(Tomcat is relative to the tomcat installation directory \ bin)
3. Obtain the class loading directory (that is, obtain the class loading directory when a class is running) 3.1. General method 1
(For general java and web projects, first locate the first level directory where the package path can be viewed)
Inputstream is = testaction. Class. getclassloader (). getresourceasstream ("test.txt ");
(Test.txt file path is project name \ SRC \ test.txt; the first level directory of the class testaction package is located in the src directory)
In the above formula, replace testactionand test.txt with the corresponding class name and file name.
3.2. General method 2 (this method is similar to the method in 1.1. The difference is that this method must start)
Inputstream is = test1.class. getresourceasstream ("/test.txt ");
(Test.txt file path is project name \ SRC \ test.txt, the first level directory of the class test1 package is located in the src directory)
4. Obtain the root directory of the web Project (after release) 4.1. Starting from servlet
You can create a Servlet and write the following statement in its init method:
Servletcontext S1 = This. getservletcontext ();
String temp = s1.getrealpath ("/"); (key)
Result format: D: \ tool \ tomcat-6.0 \ webapps \ 002_ext \ (002_ext is the project name)
If s1.getrealpath ("") is called, the output D: \ tool \ tomcat-6.0 \ webapps \ 002_ext (with a "\" missing "\")
4.2. Starting from httpServletRequest
String cp11111 = request. getsession (). getservletcontext (). getrealpath ("/");
Result format: D: \ tool \ tomcat-6.0 \ webapps \ 002_ext \
5. Obtain classpath
(In eclipse, obtain the path of the SRC or classes directory)
5.1. method 1
Thread. currentThread (). getContextClassLoader (). getResource (""). getPath ();
Eg:
String t = Thread. currentThread (). getContextClassLoader (). getResource (""). getPath ();
System. out. println ("t ---" + t );
Output: t ---/E:/order/002_ext/WebRoot/WEB-INF/classes/
5.2. method 2
JdomParse. class. getClassLoader (). getResource (""). getPath () (JdomParse is the class in a src package, the same below)
Eg:
String p1 = JdomParse. class. getClassLoader (). getResource (""). getPath ();
System. out. println ("JdomParse. class. getClassLoader (). getResource --" + p1 );
Output:
JdomParse. class. getClassLoader (). getResource --/E:/order/002_ext/WebRoot/WEB-INF/classes/
In addition, if you want to put the file in a package, you can obtain the file in the following ways (first locate the last level directory of the package)
Eg
String p2 = JdomParse. class. getResource (""). getPath ();
System. out. println ("JdomParse. class. getResource ---" + p2 );
Output:
JdomParse. class. getResource ---/E:/order/002_ext/WebRoot/WEB-INF/classes/jdom/
(JdomParse is the class in the jdom package under the src directory)
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.
6. Obtain the path in Jsp, Servlet, and Java Methods 6.1. 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
6.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
6.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
(3) Get the relative address: request. getrequesturi ()
Result:/test