Difference between getRealPath () and getContextPath,
Reprinted from: http://sucre.iteye.com/blog/319178
In programs, you often need to obtain the file path. Sometimes you need to use relative paths, and sometimes you need to use absolute paths. When we mention absolute paths, you must think of getRealPath () however, this method is no longer recommended, but what should we do if we still need to get an absolute path? Don't worry. Use this:
Java code
1 request. getSession (). getServletContext (). getRealPath ("/") + "userinfo. properties ";
In this way, the absolute path will be obtained when the file is used. The following figure shows the effect after running:
Java code
1 E: \ tomcat \ webapps \ myajax \ userinfo. properties
To obtain the relative path, use:
Java code
1 request. getContestPath ()
After running:
Java code
1/myajax/userinfo. properties