Java Web solves the path (absolute path vs. relative path) problem

  • 2020-05-27 05:30:27
  • OfStack

Solve the path problem in Java Web:

There are two types of paths used in Java: absolute paths and relative paths. After all, Java essentially USES only absolute paths to find resources. All the relative paths to find resources are simply convenience methods. But API at the bottom helped us build the absolute path to find the resource!

When developing applications for Web, it is often necessary to get the physical path of the current WebRoot in the server.

If it is Servlet, Action, Controller, or Filter, Listener, interceptor or other relevant classes, we only need to get ServletContext, and then get the physical address of the current application on the server through ServletContext.getRealPath ("/").

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

1. XXX.class.getClassLoader ().getResource(""); Method to get ClassPath, and then process to get the WebRoot directory. This can only take effect if the class is under WebRoot/ WEB-INF /classes. If the class is packaged into an jar file, this method will fail. Here's one way to do it.

2. spring framework, in WEB-INF/web.xml, create an param of webAppRootKey, specify a value (webapp.root by default) as the key value, and then execute String webAppRootKey = getServletContext().getRealPath("/") through Listener, or Filter, or Servlet; webapp.root corresponding to webAppRootKey is taken as Key respectively, and Value is written into System Properties system properties. The physical path of WebRoot is then obtained in the program by System.getProperty (" webapp.root ").

We can extend it one more time based on the second way of thinking. However, for applications deployed on one server, look again if that's not what you need.

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

1.


DebitNoteAction.class.getResource("")

You get the URI directory for the current FileTest.class file. Not including myself!

Such as:


file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/

atacarnet/src/com/evi/modules/atacarnet/action/

2.


 DebitNoteAction.class.getResource("/")

You get the absolute URI path for the current classpath.

Such as:


file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/

3.


Thread.currentThread().getContextClassLoader().getResource("")

You also get the absolute URI path for the current ClassPath

Such as:


file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/

4.


DebitNoteAction.class.getClassLoader().getResource("") 

or


ClassLoader.getSystemResource("")

You also get the absolute URI path for the current ClassPath.

Such as:


file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/

5. Get the relative path of the server


file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/
0

For example: E: / apache - tomcat - 5.5.16 / apache - tomcat - 5.5.16 / bin

I recommend Thread.currentThread ().getContextClassLoader ().getResource ("") to get the URI representation of the current absolute path of classpath

6. Get the absolute path in the project

1 a with


file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/
1

or


file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/
2

Instead of using request.getRealPath ("/"), you can use the ServletContext.getRealPath ("/") method to get the absolute path to the root 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, that's all you need


File f = new File(com/test.java);

But what if I want to get a file that's not in src or WebRoot, but from a directory that's equivalent to src or WebRoot, say doc

My hard method works like this:


file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/
4

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: