Ways to get engineering paths in Java

Source: Internet
Author: User

The relative paths that are called in the JSP and class files are different. In the JSP, the root directory is webroot in the class file, the root directory is webroot/web-inf/classes of course you can also use System.getproperty ("user.dir") Get the right path to your project.
Get path in 1.jsp:
Take the project name test as an example
(1) Get the full path of the current page including the project name:

[Java]View Plaincopyprint?

Request.getrequesturi ()


Results:/test/test.jsp
(2) Get project name:

[Java]View Plaincopyprint?

Request.getcontextpath ()


Results:/test
(3) Get the full name of the current page in the same directory:

[Java]View Plaincopyprint?

Request.getservletpath ()


Result: If the page is/test/jsp/test.jsp in the JSP directory
(4) Get the full path of the server on which the page is located:

[Java]View Plaincopyprint?

Application.getrealpath ("test.jsp")


Results: D:\resin\webapps\TEST\test.jsp
(5) Get the right path to the server on which the page is located:

[Java]View Plaincopyprint?

Abspath=new Java.io.File (Application.getrealpath (Request.getrequesturi ())). GetParent ();


Results: D:\resin\webapps\TEST
2. Get the path in the class:
(1) The path of the class:

[Java]View Plaincopyprint?

Class.class.getClass (). GetResource ("/"). GetPath ()


(2) Get the project path:

[Java]View Plaincopyprint?

System.getproperty ("User.dir")


Results: D:\TEST
3. Get the path in the servlet:
(1) Get the Project catalogue:

[Java]View Plaincopyprint?

Request.getsession (). Getservletcontext (). Getrealpath ("")//parameter can be specific to the package name.


Results: E:\Tomcat\webapps\TEST
(2) Get IE Address bar address:

[Java]View Plaincopyprint?

Request.getrequesturl ()


Results: Http://localhost:8080/TEST/test
(3) Get the relative address:

[Java]View Plaincopyprint?

Request.getrequesturi ()


Results:/test/test

Ways to get engineering paths in Java

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.