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") to get your project's absolute path.
get path in 1.jsp:
Take the project name test as an example:
(1) Get the full path of the current page containing the project name: Request.getrequesturi ()
Results:/test/test.jsp
(2) Get project name: Request.getcontextpath ()
Results:/test
(3) Get the current page in the same directory under the full name: Request.getservletpath ()
Result: If the page is/test/jsp/test.jsp in the JSP directory
(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) Obtain the absolute path of the server on which the page is located: Abspath=new java.io.File (Application.getrealpath (Request.getrequesturi ())). GetParent ();
Results: D:\resin\webapps\TEST
2. 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) The path to get the project: System.getproperty ("User.dir")
Results: D:\TEST
3. Get the path in the servlet:
(1) Get the Project catalog: Request.getsession (). Getservletcontext (). Getrealpath ("") parameter 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
In addition, the class class has a getResourceAsStream method, remember that there was a project to read in the same package of XML, the use of this.
1. How to get the current file path
common:
(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 ("")
Gets the absolute URI of the current classpath as well.
(4). Test.class.getClassLoader (). GetResource ("")
obtains an absolute URI that is also the current classpath.
(5). Classloader.getsystemresource ("")
obtains an absolute URI that is also the current classpath.
Try not to use relative paths to the current user directory compared to System.getproperty ("User.dir"), which can be seen in a variety of results.
(6) New File (""). GetAbsolutePath () is also available.
Note: If there are spaces in these paths, and the spaces are replaced by%20, you can use Path = Java.net.URLDecoder.decode (path, "Utf-8") to convert;
2.Web Server
(1). Tomcat
Output System.getproperty ("User.dir") in the class;%tomcat_home%/bin is displayed.
(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, the new file method is used for the test of file F = "new" File ("a.htm");
This a.htm is under the resin installation directory.
(3). How to read a file
Use Servletcontext.getresourceasstream () to
(4). Get the true path of the file
String File_real_path=servletcontext.getrealpath ("Mypath/filename");
The use of Request.getrealpath ("/") is not recommended;
Turn http://blog.csdn.net/rogerjava/article/details/7568466
Don't let yesterday's frustration make today's dream fade. Successful people always modify the method without modifying the target
Java Get project Path