Take the project name/demoweb as an example:
The accessed JSP is: http: // localhost: 8080/demoweb/test/index. jsp.
1. Obtain the relative and absolute paths of the current application in JSP.
(1) obtain the Project name: request. getcontextpath ()
Result:/demoweb
(2) obtain the full path of the current page containing the Project name: request. getrequesturi ()
Result:/demoweb/test/testpath. jsp
(3) obtain the address in the IE Address Bar: request. getrequesturl ()
Result: http: // localhost: 8080/demoweb/test/testpath. jsp
(4) obtain the full name in the directory where the current page is located: request. getservletpath ()
Result:/test/testpath. jsp
(5) obtain the full path (actual path) of the server where the page is located: application. getrealpath ("testpath. jsp ")
Result: D: \ develop files \ apache-Tomcat-5.5.15 \ apache-Tomcat-5.5.15 \ webapps \ demoweb \ testpath. jsp
D: \ develop files \ apache-Tomcat-5.5.15 \ apache-Tomcat-5.5.15 is the installation path of Tomcat.
2. Methods for obtaining relative paths and absolute paths in Java class
(1) absolute path of the class:
System. Out. println (testpath. Class. getresource ("/"). getpath ());
Result:/E:/workspace/workspace_tcc/demoweb/webroot/WEB-INF/classes/
System. Out. println (testpath. Class. getresource (""));
Result: file:/E:/workspace/workspace_tcc/demoweb/webroot/WEB-INF/classes/demo1/
(2) obtain the project path: system. getproperty ("user. dir ")
Result: e: \ workspace \ workspace_tcc \ demoweb
The following is a link to the Java path:
Java path Summary
Java relative path/absolute path Summary ()