Solve path problems in Java Web:
The paths used in Java are divided into two types: absolute paths and relative paths. Ultimately, Java can only use absolute paths to find resources. All methods of finding resources on relative paths are just some convenient methods. But it is the API that helps us build an absolute path at the bottom to find resources!
When developing web applications, you often need to obtain the physical path of the current WebRoot in the server.
If it is a Servlet , Action , Controller , or Filter , Listener , interceptor and other related classes, we only need to obtain the ServletContext, and then use ServletContext.getRealPath("/") to obtain the physical address of the current application on the server.
If the ServletContext cannot be retrieved in the class, there are two ways to do it:
1. Use Java's class loading mechanism to call the XXX.class.getClassLoader().getResource(""); method to obtain ClassPath, and then process to obtain the WebRoot directory. This method can only take effect under WebRoot/WEB-INF/classes. If the class is packaged into a jar file, the method will be invalid. At this time, the following method should be used.
2. The idea of spring framework. In WEB-INF/web.xml, create a param of webAppRootKey, specify a value (default is webapp.root) as the key value, and then execute String webAppRootKey = getServletContext().getRealPath("/"); through Listener, or Filter, or Servlet. Then write the webapp.root corresponding to webAppRootKey as Key and Value into the System Properties system properties. Then, the physical path of WebRoot is obtained through System.getProperty("webapp.root") in the program.
According to the second idea, we can expand it further. However, for applications deployed in a server, if it is not your needs, please read it down.
Here are some ways to get the classpath and the absolute path to the current class. You can use some of these methods to get the absolute path to the resources you need:
1.
DebitNoteAction.class.getResource("")What you get is the URI directory of the current class FileTest.class file. Not including yourself!
like:
file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/
atcarnet/src/com/evi/modules/atacarnet/action/
2.
DebitNoteAction.class.getResource("/")What you get is the absolute URI path of the current classpath.
like:
file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/
3.
Thread.currentThread().getContextClassLoader().getResource("")What you get is also the absolute URI path of the current ClassPath
like:
file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/
4.
DebitNoteAction.class.getClassLoader().getResource("")or
ClassLoader.getSystemResource("")What you get is also the absolute URI path of the current ClassPath.
like:
file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/
5. Obtain the server relative path
System.getProperty("user.dir")For example: E:/apache-tomcat-5.5.16/apache-tomcat-5.5.16/bin
I recommend using Thread.currentThread().getContextClassLoader().getResource("") to get the URI notation of the absolute path of the current classpath
6. Obtain the absolute path in the project
Generally used
request.getRealPath("/")or
request.getRealPath("/config/")But it is not recommended to use request.getRealPath("/"). You can try the ServletContext.getRealPath("/") method to get the absolute path to the root directory of the web application
It is very easy to get the src file, because src is the default relative directory. For example, if you say you want to get the test.java file in the com directory under src, you only need this to be enough.
File f = new File(com/test.java);
But if I want to get files that are not in the src directory or WebRoot directory, I need to get them from the same level of src or WebRoot, for example, doc
My hard method is implemented like this:
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"));Thank you for reading, I hope it can help you. Thank you for your support for this site!