One, the Java file to get the path
Thread.CurrentThread (). Getcontextclassloader (). GetResource ("")//Get the path of the resource file (. class file)
Classloader.getsystemresource ("")
Class_Name.class.getClassLoader (). GetResource ("")
Class_name.class. GetResource ("/")
Class_name.class. GetResource ("")//Get the path of the current class
System.getproperty ("User.dir")//Get the absolute path to the project root directory
System.getproperty ("Java.class.path")//Get class path and package path
The printed output is as follows:
file:/f:/work_litao/uri_test/webcontent/web-inf/classes/
file:/f:/work_litao/uri_test/webcontent/web-inf/classes/
file:/f:/work_litao/uri_test/webcontent/web-inf/classes/
file:/f:/work_litao/uri_test/webcontent/web-inf/classes/
file:/f:/work_litao/uri_test/webcontent/web-inf/classes/com/xml/imp/
F:work_litaouri_test
f:work_litaouri_testwebcontentweb-infclasses; F:work_litaouri_testwebcontentweb-inflibdom4j.jar
Second, the relative path and absolute path of the current application are obtained in JSP
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 ()
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 to 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 ()
)
The above mentioned is the entire content of this article, I hope you can enjoy.
Please take a moment to share the article with your friends or leave a comment. We will sincerely thank you for your support!