The relative paths that are invoked in the JSP and class files are different. In 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 obtain your project absolute path.
Another: In the Jsp,servlet,java in detail to obtain the path Method!
1.jsp to get the path:
Take the project name test as an example:
(1) Get the full path of the current page containing the project name: Request.getrequesturi ()
Result:/test/test.jsp
(2) Get project name: Request.getcontextpath ()
Result:/test
(3) Get the current page in the directory under the full name: Request.getservletpath ()
Results: If the page is/test/jsp/test.jsp under the JSP directory
(4) Get the full path of the server on the page: Application.getrealpath ("page. JSP")
Result: d:/resin/webapps/test/test.jsp
(5) The absolute path to the server on which the page is located: Abspath=new java.io.File (Application.getrealpath (Request.getrequesturi ())). GetParent ();
Result: D:/resin/webapps/test
2. Get the path in the class:
(1) The absolute path of the class: String U=class.class.getclass (). GetResource ("/"). GetPath ()
Result:/d:/test/webroot/web-inf/classes/pack/
STR will get the path
String str = u.tostring () of the class where this function is located;
Truncate some of the preceding 6 useless characters
str=str.substring (6,str.length ());
Replace the%20 with a space (if the name of the folder has a space, it will become%20 on the obtained string)
Str=str.replaceall ("%20", "");
Find "Web-inf" at the position of the string
int num = str.indexof ("Web-inf");
Interception can be
str=str.substring (0, num+ "web-inf". Length ());
(2) Get the project path: System.getproperty ("User.dir")
Result: D:/test
3. Get the path in the servlet:
(1) Obtain the Engineering directory: Request.getsession (). Getservletcontext (). Getrealpath ("") parameter can be specific to the package name.
Result: E:/tomcat/webapps/test
(2) Get IE Address bar address: Request.getrequesturl ()
Result: Http://localhost:8080/TEST/test
(3) Relative address: Request.getrequesturi ()
Result:/test/test