How to obtain the common Java class, the root path of the WEB Project _ Baidu Knows

Source: Internet
Author: User

The relative paths called in JSP and class files are different. In 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 obtain the absolute path of your project.

In addition, you can obtain detailed paths in JSP, Servlet, and Java!

1. Obtain the path in JSP:

Take the project name test as an example:

(1) obtain the full path of the current page containing the Project name: request. getrequesturi ()
Result:/test. jsp
(2) obtain the Project name: request. getcontextpath ()
Result:/test
(3) Get the full name in the directory where the current page is located: request. getservletpath ()
Result: If the page is in the/test/JSP/test. jsp directory
(4) obtain the full path of the server where the page is located: application. getrealpath ("Page. jsp ")
Result: D:/resin/webapps/test. jsp
(5) obtain the absolute path of the server where the page is located: abspath = new java. Io. File (application. getrealpath (request. getrequesturi (). getparent ();
Result: D:/resin/webapps/test

2. Obtain the path in the class:

(1) absolute path of the class: String u = Class. Class. getclass (). getresource ("/"). getpath ()
Result:/D:/test/webroot/WEB-INF/classes/Pack/

  1. // STR will get the path of the class where the function is located
  2. String STR = U. tostring ();
  3. // Cut off the first six useless characters
  4. STR = Str. substring (6, str. Length ());
  5. // Replace % 20 with a space (if the folder name contains spaces, it will be changed to % 20 on the obtained string)
  6. STR = Str. replaceall ("% 20 ",
    "");
  7. // Find the position of the WEB-INF in the string
  8. Int num = Str. indexof ("WEB-INF ");
  9. // Intercept
  10. STR = Str. substring (0, num + "WEB-INF". Length ());

(2) obtain the project path: system. getproperty ("user. dir ")
Result: D:/test

 

3. Get the path in servlet:

(1) obtain the project directory: the request. getsession (). getservletcontext (). getrealpath ("") parameter can be specific to the package name.
Result: e:/tomcat/webapps/test
(2) obtain the address in the IE Address Bar: request. getrequesturl ()
Result: http: // localhost: 8080/test
(3) Get the relative address: request. getrequesturi ()
Result:/test

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.