java web 路径(java web 路径)
java web 路径(java web 路径)
The path used in Java, divided into two types: absolute and relative paths. After all, the essence of Java can only use the absolute path to find resources. Methods all the relative path to find resources, but some convenience methods. But API is at the bottom and help us build the absolute path, in order to find the resources!
The application of Web in the development of the physical path, often need to get the current WebRoot in the server.
If it is Servlet, Action, Controller, or Filter, Listener, interceptor and other related classes, we only need to get the ServletContext, and then ServletContext.getRealPath ("/") to obtain the physical address of the application on the server.
If in the class not to ServletContext, there are two ways to do:
1. using Java class loading mechanism called XXX.class.getClassLoader (.GetResource) (""); to get to the ClassPath, and then processed to obtain the WebRoot directory, the only way is to take effect in the class WebRoot/WEB-INF/classes, if the class is packaged into a jar file, the method of failure. Then you should use the following ways.
2. spring framework, in WEB-INF/web.xml, create a webAppRootKey param, specify a value (default is webapp.root) as the key, and then through the Listener, or Filter, or Servlet String webAppRootKey (getServletContext) executive =.GetRealPath ("/"); and the corresponding webAppRootKey webapp.root as Key Value wrote. The System attribute in Properties system. Later in the program by System.getProperty ("webapp.root") to obtain the physical path of WebRoot.
According to the second kinds of ideas, we can expand out. But for the application in the deployment in a server, if it is not what you need. Please look down.
Here are some ways to get the classpath and some of the current class absolute path. Some of the ways you can use the absolute path in which to get the resources you need:
1. ("DebitNoteAction.class.getResource")
The FileTest.class URI is the current class