Get relative and absolute paths in Servlet and class
Obtain relative and absolute paths in JSP
Absolute path corresponding to the root directory: request. absolute path of the getrequesturi () file: application. getrealpath (request. getrequesturi (); absolute path of the current web application: application. getrealpath ("/"); get the upper directory of the request file: New file (application. getrealpath (request. getrequesturi ())). getparent ()
Get relative and absolute paths in Servlet
Absolute path corresponding to the root directory: request. getservletpath ();Result:/Servlet/client_note specifies the absolute path of the folder: This. getservletcontext (). getrealpath (URL); // The URL is a known file name "/upload"Result: D: \ Tomcat 6.0 \ webapps \ anxunclientsearch \ Upload File absolute path: request. getsession (). getservletcontext (). getrealpath (request. getrequesturi ())Result: D: \ Tomcat 6.0 \ webapps \ anxunclientsearch \ servlet \ client_note absolute path of the current web application: servletconfig. getservletcontext (). getrealpath ("/");
(Several methods for obtaining the servletcontext object: javax. servlet. http. httpsession. getservletcontext () javax. servlet. jsp. pagecontext. getservletcontext () javax. servlet. servletconfig. getservletcontext ())
--------------------- Test data ------------------------
System. out. println ("absolute path corresponding to the root directory" + request. getservletpath (); system. out. println ("absolute file path" + request. getsession (). getservletcontext (). getrealpath (request. getrequesturi (); system. out. println ("content path:" + request. getcontextpath (); system. out. println ("absolute path:" + request. getrequesturl ());
Absolute path of the root directory/servlet/client_note file D: \ Tomcat 6.0 \ webapps \ anxunclientsearch \ servlet \ client_note path:/anxunclientsearch absolute path: http: // 192.168.1.102: 8080/anxunclientsearch/servlet/client_note
Obtain relative and absolute paths in the class
According to the java. Io. File doc, by default, new file ("/") represents the directory system. getproperty ("user. dir ").
--------------------- Test data ------------------------
System. Out. println ("absolute path corresponding to the root directory" + request. getservletpath ());