Path Problems in J2EE WEB projects

Source: Internet
Author: User
Resolve all paths in the J2EE WEB Project

The paths used in Java are divided into absolute paths and relative paths. In essence, Java can only use absolute paths to find resources. All relative paths are just some convenient methods to find resources. However, the API helps us build an absolute path at the underlying level to find resources!

When developing Web applications, you often need to obtain the physical path of the current webroot on the server.

For servlet, action, controller, filter, listener, interceptor, and other related classes, we only need to obtain servletcontext and then use servletcontext. getrealpath ("/") to obtain the physical address of the current application on the server.

If servletcontext is not obtained in the class, there are two ways to do this:

1) Use the class loading mechanism of Java: Call XXX. Class. getclassloader (). getresource (""); to obtain the classpath, and then obtain the webroot directory.
This method takes effect only when the class is in webroot/WEB-INF/classes and fails if the class is packaged into a jar file. In this case, we should use the following method.

2) Spring framework ideas in WEB-INF/web. in XML, create a webapprootkey PARAM and specify a value (default: webapp. root) as the key value, and then use listener, filter, or servlet to execute string webapprootkey = getservletcontext (). getrealpath. the root serves as the key and the value is written to the system properties. Then, use system. getproperty ("webapp. Root") in the program to obtain the physical path of webroot.

Based on the second idea, we can expand it. However, for applications deployed on a server, if you do not need them yet, please read them further.

The following are some methods to obtain the absolute path of classpath and the current class. You can use some of these methods to obtain the absolute path of the required resources:

1. debitnoteaction. Class. getresource ("")
The URI Directory of the current filetest. Class file is obtained. Not yourself!
For example: file:/D:/Eclipse/springtest/webroot/WEB-INF/classes/
Atacarnet/src/COM/EVI/modules/atacarnet/Action/

2. debitnoteaction. Class. getresource ("/")
Obtain the absolute URI path of the current classpath.
For example: file:/D:/Eclipse/springtest/webroot/WEB-INF/classes/

3. thread. currentthread (). getcontextclassloader (). getresource ("")
The obtained absolute URI path of the current classpath
For example: file:/D:/Eclipse/springtest/webroot/WEB-INF/classes/
This method is recommended.

4. debitnoteaction. Class. getclassloader (). getresource ("") or classloader. getsystemresource ("")
The obtained absolute URI path of the current classpath.
For example: file:/D:/Eclipse/springtest/webroot/WEB-INF/classes/

5. Obtain the relative server path
System. getproperty ("user. dir ")
Example: e: \ apache-Tomcat-5.5.16 \ apache-Tomcat-5.5.16 \ bin

6. Obtain the absolute path in the project
Generally, request. getrealpath ("/") or request. getrealpath ("/config /")

However, we do not advocate the use of request. getrealpath ("/"). You can use the servletcontext. getrealpath ("/") method to obtain the absolute path of the root directory of the Web application.

It is very easy to obtain SRC files, because SRC is the default relative directory. For example, if you want to obtain the test. Java file in the com directory under SRC, you only need to do this.
File F = new file (COM/test. Java );

But if I want to retrieve files not in the SRC or webroot directory, I want to retrieve them from directories of the same level as SRC or webroot, for example, Doc.
My hard method is implemented as follows:
String Path = This. getservletcontext (). getrealpath ("/");
Properties P = new properties ();
P. load (New fileinputstream (new file (path. substring (0, (path. lastindexof ("\ webroot") + 1) + "DOC/DB. properties ")));
System. Out. println (P. getproperty ("drivername "));
-------------------------------
Differences between getcontextpath, getservletpath, getrequesturi, getrequesturl, and getrealpath in request

Assume that your web application is named "news". In your browser, enter the Request Path: http: // localhost: 8080/news/main/list. jsp.

Then execute the line-oriented code and print the following results:

1. system. Out. println (request. getcontextpath ());
Print result:/news

2. system. Out. println (request. getservletpath ());
Print result:/main/list. jsp

3. system. Out. println (request. getrequesturi ());
Print result:/news/main/list. jsp

4. system. Out. println (request. getrequesturl ());
Printed result: http: // localhost: 8080/news/main/list. jsp

5. system. Out. println (request. getrealpath ("/"));
Print result: F: \ Tomcat 6.0 \ webapps \ news \ 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.