the relative paths that are called in the JSP and class files are different. In the 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"get the absolute path to your project. 1get the path in the. JSP: Take the project name test as an example: (1) Gets the full path of the current page that contains the project name: Request.getrequesturi () Result:/test/test.jsp (2) Get project name: Request.getcontextpath () Result:/TEST (3get the current page in the same directory as the full name: Request.getservletpath () Result: 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=NewJava.io.File (Application.getrealpath (Request.getrequesturi ())). GetParent (); Results: D:\resin\webapps\TEST2. Get 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")) Results: D:\TEST3get the path in the servlet: (1) Get the Project catalog: Request.getsession (). Getservletcontext (). Getrealpath ("") parameters can be specific to the package name. Results: E:\Tomcat\webapps\TEST (2) Get IE Address bar address: Request.getrequesturl () Results: http://localhost:8080/test/test(3) Get relative Address: Request.getrequesturi () Results:/test/test Another, class class has a getResourceAsStream method, remember that there was a project to read in the same package of XML, the use of this. 1how to get the current file path commonly used: (1). Test.class. GetResource ("") Gets the URI directory of the current class Filetest.class file. Don't include yourself! (2). Test.class. GetResource ("/") Gets the absolute URI of the current classpath. (3). Thread.CurrentThread (). Getcontextclassloader (). GetResource ("") is also the absolute URI of the current classpath. (4). Test.class. getClassLoader (). GetResource ("") is also the absolute URI of the current classpath. (5). Classloader.getsystemresource ("") is also the absolute URI of the current classpath. Try not to use relative System.getproperty ("User.dir") The relative path of the current user directory, which can be seen in a variety of results. (6)NewFile (""). GetAbsolutePath () is also available. Note: If spaces are present in these paths, and the spaces are%20 instead, you can use Path = Java.net.URLDecoder.decode (path, "Utf-8") for conversion; 2. Web Server (1). Tomcat outputs System.getproperty in the class ("User.dir");%tomcat_home%/is displayed.Bin (2). Resin is not the relative path that your JSP puts, it is the JSP engine that executes this JSP to compile the path of the servlet to the root. For example, test file F with the new document Method=NewFile ("a.htm"); This a.htm is in the installation directory of resin (3). How to read a file using Servletcontext.getresourceasstream () you Can (4). Get the file True path string File_real_path=servletcontext.getrealpath ("Mypath/filename"); It is not recommended to use Request.getrealpath ("/"); Go to http://blog.csdn.net/rogerjava/article/details/7568466
Get path in Java