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
Class load Root path
String ClassPath = This.getclass (). GetResource ("/"). GetPath ();
Class load Root path
URL Xmlpath = This.getclass (). getClassLoader (). GetResource ("");
Class is in the project root path
String Proclasspath = This.getclass (). GetResource (""). GetPath ();
Project Server script files path file
directory = new File ("");//parameter is empty
String Prorootpath = Directory.getcanonicalpath ();
Project Server script file path
String Propath = System.getproperty ("User.dir");
Gets all classpath including jar package path
String Allclasspath = System.getproperty ("Java.class.path");
Path to project deployment
String Path = Request.getsession (). Getservletcontext (). Getrealpath ("/");
SYSTEM.OUT.PRINTLN ("Class load Root path:" + ClassPath);
SYSTEM.OUT.PRINTLN ("Class load Root path:" + Xmlpath);
System.out.println ("The class is located in the project path:" + Proclasspath);
SYSTEM.OUT.PRINTLN ("Project Server script file path:" + Prorootpath);
SYSTEM.OUT.PRINTLN ("Project Server script file path:" + Propath);
SYSTEM.OUT.PRINTLN ("Path of the Project deployment:" + Allclasspath);
System.out.println ("Get all classpath:" + path);
The results are as follows:
Class Load Root path:/d:/workspace/training2016/04-pg/0401-source/base2016/base_web/target/base_web-1.0-snapshot/web-inf/ Classes/
Class Load root path: File:/d:/workspace/training2016/04-pg/0401-source/base2016/base_web/target/base_ Web-1.0-snapshot/web-inf/classes/
Class is located in the project path:/d:/workspace/training2016/04-pg/0401-source/base2016/base_web/ TARGET/BASE_WEB-1.0-SNAPSHOT/WEB-INF/CLASSES/COM/STUDY/MVC/
Project Server script file path: D:\apache-tomcat-8.0.361\bin
Project Server script file path: D:\apache-tomcat-8.0.361\bin
Path to project deployment: D:\workspace\Training2016\04-PG\0401-Source\base2016\base_ Web\target\base_web-1.0-snapshot\
Get all classpath: d:\apache-tomcat-8.0.361\bin\bootstrap.jar;d:\ Apache-tomcat-8.0.361\bin\tomcat-juli.jar
3. Obtain the path in the servlet:
(1) Get the Engineering Directory: 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) Gets the relative address: Request.getrequesturi ()
Result:/test/test