Address all path issues in the Java EE Web project

Source: Internet
Author: User

There are two types of paths used in Java: absolute paths and relative paths. Ultimately, Java can only use absolute paths to find resources in essence. All the relative paths to find resources are just a few convenient ways. It's just the API that helps us build an absolute path to find resources!

When developing Web-related applications, it is often necessary to obtain the physical path of the current Webroot in the server.

In the case of Servlet, Action, Controller, or filter, Listener, interceptor and other related classes, we only need to get servletcontext, and then through Servletcontext.getrealpath (" /") to get the physical address of the current app on the server.

If ServletContext is not available in a class, there are two ways to do this:

1) Take advantage of Java's class loading mechanism: Call XXX.class.getClassLoader (). GetResource (""); method to get to Classpath, and then process to get the Webroot directory.
This can only take effect if the class is webroot/web-inf/classes, and if the class is packaged in a jar file, the method fails. This should be done in one of the following ways.

2) Spring framework idea, in Web-inf/web.xml, create a webapprootkey param, specify a value (default = Webapp.root) as the key value, and then through listener, or filter, Or the servlet executes string webapprootkey = Getservletcontext (). Getrealpath ("/"); The webapprootkey corresponding webapp.root are written to the system properties as Key,value respectively. The physical path of the Webroot is then obtained through System.getproperty ("Webapp.root") in the program.

According to the second way of thinking, we can also expand a bit. But for applications that are deployed on a single server, look down if it's not what you need.

Here are some ways to get the absolute path of classpath and the current class. You can use some of these methods to get the absolute path of the resources you need:

1.debitnoteaction.class.getresource ("")
The URI directory of the current class Filetest.class file is obtained. Don't include yourself!
such as: file:/d:/eclipse/springtest/webroot/web-inf/classes/
atacarnet/src/com/evi/modules/atacarnet/action/

2.debitnoteaction.class.getresource ("/")
Gets the absolute URI path of the current classpath.
such as: file:/d:/eclipse/springtest/webroot/web-inf/classes/

3.thread.currentthread (). Getcontextclassloader (). GetResource ("")
The resulting absolute URI path is also the current classpath
such as: file:/d:/eclipse/springtest/webroot/web-inf/classes/
This method is recommended for use.

4.debitnoteaction.class.getclassloader (). GetResource ("") or Classloader.getsystemresource ("")
The resulting absolute URI path is also the current classpath.
such as: file:/d:/eclipse/springtest/webroot/web-inf/classes/

5. Get the server-relative path
System.getproperty ("User.dir")
Example: E:\apache-tomcat-5.5.16\apache-tomcat-5.5.16\bin

6. Get the absolute path in the project
Generally with Request.getrealpath ("/") or Request.getrealpath ("/config/")

But now it is not recommended to use Request.getrealpath ("/"), you can try the Servletcontext.getrealpath ("/") method to get the absolute path of the root directory of the Web application.

To get the src file is very easy, because SRC is the default relative directory, such as you say to get src under the COM directory of the Test.java file, you just need this is enough
File F = new file (Com/test.java);

But if I'm going to get a file that's not in the SRC directory or the Webroot directory, I'm going to take it from a directory like SRC or webroot, such as Doc.
My hard way to do this is to implement:
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"));
-------------------------------
Another: The difference between Getcontextpath, Getservletpath, Getrequesturi, Getrequesturl, Getrealpath in Request

Assuming your Web application name is news, you enter the request path in the browser: http://localhost:8080/news/main/list.jsp

Then the following line code is executed to print out 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 ());
Print Result: http://localhost:8080/news/main/list.jsp

5, System.out.println (Request.getrealpath ("/"));
Print Result: F:\Tomcat 6.0\webapps\news\test

Address all path issues in the Java EE Web project

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.