Several ways to get Java[web] project-related paths

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.
Here's how to get the path in detail in Jsp,servlet,java! Take Java Web project name MyPath as an example:
get path in 1.jsp :


(1) gets the full path of the current page containing the project name : Request.getrequesturi ();
Result:/mypath/jsp/pathpage.jsp
(2) gets the project name : Request.getcontextpath ();
Result:/mypath
(3) gets the full name of the current page in the directory : Request.getservletpath ();
Result: If the page is in the JSP directory/jsp/pathpage.jsp
(4) gets the full path of the server on which the page is located : Application.getrealpath ("jsp/pathpage.jsp ");
Result: F:\apache-tomcat-6.0.35\webapps\MyPath\jsp\pathpage.jsp
(5) Gets the absolute path of the server on which the page is located : String Abspath = new Java.io.File (Application.getrealpath (Request.getservletpath ())). GetParent ();
Result: F:\apache-tomcat-6.0.35\webapps\MyPath\jsp
(6) get access Path to Project : Request.getscheme () + "://" + Request.getservername () + ":" +request.getserverport () +request.getcontextpath () + "/";
Result: http://localhost:8080/MyPath/


2. Get the path in the class :

(1) get the storage path for the Class file : String path = Class.class.getClass (). GetResource ("/"). GetPath ();
Results:/f:/workspace/mypath/webroot/web-inf/classes/

STR will get the path to the class where the function resides.
String str = path.tostring ();

Cut off some of the first 1 useless characters
str = str.substring (1,str.length ());

Replace%20 with a space (if the folder name has a space, it becomes%20 on the acquired string)
str = Str.replaceall ("%20", "" ");

Find "Web-inf" in the position of the string
int num = Str.indexof ("Web-inf");

Intercept can
str = str.substring (0, num+ "web-inf". Length ());

The final result is: F:/workspace/mypath/webroot/web-inf

(2) get the project path : System.getproperty ("User.dir");
Results: F:\WorkSpace\MyPath

(3) get The absolute path of class : PathJava.class.getClass (). GetResource (""). GetPath ();
Results:/f:/workspace/mypath/webroot/web-inf/classes/com/path/test/

3. Get the path in the servlet :

(1) Engineering Catalog : Request.getsession (). The Getservletcontext (). Getrealpath ("") parameter can be specific to the package name.
Results: F:\apache-tomcat-6.0.35\webapps\MyPath
(2) Get IE Address bar address : Request.getrequesturl ()
Results: http://localhost:8080/MyPath/jsp
(3) Get relative address : Request.getrequesturi ()
Results:/mypath/jsp

Several ways to get Java[web] project-related paths

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.