Get relative and absolute paths in Servlet and class

Source: Internet
Author: User

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 ());

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.