The content comes from snannan_268
Keyword: Get path in Java to get path in Java:
1.jsp Get path:
Take the project name test as an example:
(1) Get the full path of the current page that contains the project name: Request.getrequesturi ()
Results:/test/test.jsp
(2) Get project name: Request.getcontextpath ()
Result:/test
(3) Get the current page in the same directory as the full name: Request.getservletpath ()
Results: If the page is in the JSP directory/test/jsp/test.jsp
(4) Get the full path of the server on which the page is located: Application.getrealpath ("page. jsp")
Results: d:/resin/webapps/test/test.jsp
(5) Get the absolute path of the server on which the page is located: Abspath=new java.io.File (Application.getrealpath (Request.getrequesturi ())). GetParent ();
Result: D:/resin/webapps/test
2. Gets the path in the class:
(1) The absolute path of the class: Class.class.getClass (). GetResource ("/"). GetPath ()
Results:/d:/test/webroot/web-inf/classes/pack/
(2) Get the project path: System.getproperty ("User.dir")
Result: d:/ TEST
3. Obtain the path in the servlet:
(1) Get the Project catalog: Request.getsession (). The Getservletcontext (). Getrealpath ("") parameter can be specific to the package name.
Result: E:/tomcat/webapps/test
(2) Get IE Address bar address: Request.getrequesturl ()
Results: http://localhost:8080/TEST/test
(3) Get relative Address: Request.getrequesturi ()
Result:/test/test
Get path in Java