Javaweb Project Get path various methods.

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: String U=class.class.getclass (). GetResource ("/"). GetPath ()
Results:/d:/test/webroot/web-inf/classes/pack/

[Java]View Plaincopy
  1. //str will get the path to the class where the function resides.   
  2.   string str = u.tostring ();   
  3. //cut off some of the front 6 useless characters   
  4.   str=str.substring (6 ,str.length ());  
  5. //Change%20 to a space (if the name of the folder has a space, it becomes%20 on the acquired string)   
  6.   str=str.replaceall ("%20 " ,  "   "
  7. //find" Web-inf "in the position of the string   
  8.   int  num = str.indexof ( " Web-inf "
  9. //intercept can be   
  10. Str=str.substring (0, num+"Web-inf". Length ());


(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

Javaweb Project Get path various methods.

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.