Java,jsp,servlet get current project path-absolute path

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") to get your project's absolute path.

Another: The way to get the path in detail in Jsp,servlet,java!

Get path in 1.jsp:

Take the project name test as an example:

(1) Get the full path of the current page containing the project name: Request.getrequesturi ()
Results:/test/test.jsp
(2) Get project name: Request.getcontextpath ()
Results:/test
(3) Get the current page in the same directory under the full name: 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: Application.getrealpath ("page. JSP")
Results: D:\resin\webapps\TEST\test.jsp
(5) Obtain the absolute path of the server on which the page is located: Abspath=new java.io.File (Application.getrealpath (Request.getrequesturi ())). GetParent ();
Results: D:\resin\webapps\TEST

2. Get the path in the class:

(1) The absolute path of the class: Class.class.getClass (). GetResource ("/"). GetPath ()
Results:/d:/test/webroot/web-inf/classes/pack/
(2) The path to get the project: System.getproperty ("User.dir")
Results: D:\TEST

3. Get the path in the servlet:

(1) Get the Project catalog: Request.getsession (). Getservletcontext (). Getrealpath ("") parameter can be specific to the package name.
Results: E:\Tomcat\webapps\TEST
(2) Get IE Address bar address: Request.getrequesturl ()
Results: Http://localhost:8080/TEST/test
(3) Get relative Address: Request.getrequesturi ()
Results:/test/test

Java,jsp,servlet get current project path-absolute path

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.