[Java] obtain various paths in a web project

Source: Internet
Author: User

Take the project name/demoweb as an example:
The accessed JSP is: http: // localhost: 8080/demoweb/test/index. jsp.

1. Obtain the relative and absolute paths of the current application in JSP.

(1) obtain the Project name: request. getcontextpath ()

Result:/demoweb

(2) obtain the full path of the current page containing the Project name: request. getrequesturi ()

Result:/demoweb/test/testpath. jsp

(3) obtain the address in the IE Address Bar: request. getrequesturl ()

Result: http: // localhost: 8080/demoweb/test/testpath. jsp

(4) obtain the full name in the directory where the current page is located: request. getservletpath ()

Result:/test/testpath. jsp

(5) obtain the full path (actual path) of the server where the page is located: application. getrealpath ("testpath. jsp ")

Result: D: \ develop files \ apache-Tomcat-5.5.15 \ apache-Tomcat-5.5.15 \ webapps \ demoweb \ testpath. jsp

D: \ develop files \ apache-Tomcat-5.5.15 \ apache-Tomcat-5.5.15 is the installation path of Tomcat.

2. Methods for obtaining relative paths and absolute paths in Java class

(1) absolute path of the class:

System. Out. println (testpath. Class. getresource ("/"). getpath ());

Result:/E:/workspace/workspace_tcc/demoweb/webroot/WEB-INF/classes/

System. Out. println (testpath. Class. getresource (""));

Result: file:/E:/workspace/workspace_tcc/demoweb/webroot/WEB-INF/classes/demo1/

(2) obtain the project path: system. getproperty ("user. dir ")

Result: e: \ workspace \ workspace_tcc \ demoweb

The following is a link to the Java path:

Java path Summary

Java relative path/absolute path Summary ()

Related Article

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.