Difference between getRealPath () and getContextPath,

Source: Internet
Author: User

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

 

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.