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 right path to your project.
Get path in 1.jsp:
Take the project name test as an example
(1) Get the full path of the current page including the project name:
[Java]View Plaincopyprint?
Request.getrequesturi ()
Results:/test/test.jsp
(2) Get project name:
[Java]View Plaincopyprint?
Request.getcontextpath ()
Results:/test
(3) Get the full name of the current page in the same directory:
[Java]View Plaincopyprint?
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:
[Java]View Plaincopyprint?
Application.getrealpath ("test.jsp")
Results: D:\resin\webapps\TEST\test.jsp
(5) Get the right path to the server on which the page is located:
[Java]View Plaincopyprint?
Abspath=new Java.io.File (Application.getrealpath (Request.getrequesturi ())). GetParent ();
Results: D:\resin\webapps\TEST
2. Get the path in the class:
(1) The path of the class:
[Java]View Plaincopyprint?
Class.class.getClass (). GetResource ("/"). GetPath ()
(2) Get the project path:
[Java]View Plaincopyprint?
System.getproperty ("User.dir")
Results: D:\TEST
3. Get the path in the servlet:
(1) Get the Project catalogue:
[Java]View Plaincopyprint?
Request.getsession (). Getservletcontext (). Getrealpath ("")//parameter can be specific to the package name.
Results: E:\Tomcat\webapps\TEST
(2) Get IE Address bar address:
[Java]View Plaincopyprint?
Request.getrequesturl ()
Results: Http://localhost:8080/TEST/test
(3) Get the relative address:
[Java]View Plaincopyprint?
Request.getrequesturi ()
Results:/test/test
Ways to get engineering paths in Java