Take the project name test as an example:
(1) obtain the full path of the current page containing the Project name: request. getrequesturi ()
Result:/test. jsp
(2) obtain the Project name: request. getcontextpath ()
Result:/test
(3) Get the full name in the directory where the current page is located: request. getservletpath ()
Result: If the page is in the/test/JSP/test. jsp directory
(4) obtain the full path of the server where the page is located: application. getrealpath ("Page. jsp ")
Result: D: \ resin \ webapps \ test. jsp
(5) obtain the absolute path of the server where the page is located: abspath = new java. Io. File (application. getrealpath (request. getrequesturi (). getparent ();
Result: D: \ resin \ webapps \ test
2. Obtain the path in the class:
(1) absolute path of the class: class. Class. getclass (). getresource ("/"). getpath ()
Result:/D:/test/webroot/WEB-INF/classes/Pack/
(2) obtain the project path: system. getproperty ("user. dir ")
Result: D: \ test
3. Get the path in servlet:
(1) obtain the project directory: the request. getsession (). getservletcontext (). getrealpath ("") parameter can be specific to the package name.
Result: e: \ Tomcat \ webapps \ test
(2) obtain the address in the IE Address Bar: request. getrequesturl ()
Result: http: // localhost: 8080/test
(3) Get the relative address: request. getrequesturi ()
Result:/test