This problem depends on where your configuration file is stored. If you put it in the project's classes directory (or subdirectory), you can use **. class. getresource ('relative path') to obtain the configuration file path. if it is another directory, you can only get the project root directory + configuration file directory through servletcontext at project startup to determine the path. and put the path in a place where class files can be referenced.
The following is a Class I wrote when I was working on the project to get the path. It may not be well written, but I still hope it will help you:
Package com. example. Web;
Import java. Io. file;
Import java.net. url;
Import javax. servlet. servletcontext;
Import javax. servlet. http. httpservletrequest;
/**
* Path Retrieval Class
**/
Public class webpath {
/**
* Obtain the absolute path of the Project root directory
*
* @ Return for example: F: \ tongjianpeng \ j2eeutil
**/
Public static string getabsolutepathwithproject (){
Return System. getproperty ("user. dir ");
}
/**
* Obtain the drive letter of the project.
**/
Public static string getdriverpathwithproject (){
Return new file ("/"). getabsolutepath ();
}
/**
* Obtain the absolute path of the Project root directory
*
* @ Return project root. For example, <br/> F: \ Tomcat \ webapps \ j2eeutil \
**/
Public static string getabsolutepathwithwebproject (
Httpservletrequest request ){
Return request. getsession (). getservletcontext (). getrealpath ("/");
}
/**
* Obtain the absolute path of the specified directory under the root directory of the project.
*
* @ Param the specified directory under the root directory of the project
*. Example:/login/
* @ Return the specified directory under the root directory of the project. For example: <br/> F: \ Tomcat \ webapps \ j2eeutil \ login \
**/
Public static string getabsolutepathwithwebproject (
Httpservletrequest request, string path ){
Return request. getsession (). getservletcontext (). getrealpath (PATH );
}
/**
* Obtain the absolute path of the Project root directory
*
* @ Return project root. For example, <br/> F: \ Tomcat \ webapps \ j2eeutil \
**/
Public static string getabsolutepathwithwebproject (servletcontext context ){
Return context. getrealpath ("/");
}
/**
* Obtain the absolute path of the specified directory under the root directory of the project.
*
* @ Param the specified directory under the root directory of the project
*. Example:/login/
* @ Return the specified directory under the root directory of the project. For example: <br/> F: \ Tomcat \ webapps \ j2eeutil \ login \
**/
Public static string getabsolutepathwithwebproject (servletcontext context,
String path ){
Return context. getrealpath (PATH );
}
/**
* Obtain the absolute path of the Project classpath directory.
*
* @ Return absolute path of the classes directory <br/>
* File:/F:/tomcat/webapps/j2eeutil/WEB-INF/classes/
**/
Public static URL getabsolutepathwithclass (){
Return webpath. Class. getresource ("/");
}
/**
* Obtain the absolute path of the specified directory under the project classpath directory.
*
* @ Param path
* The specified directory under the classes directory. For example:/COM/
* @ Return file:/F:/tomcat/webapps/j2eeutil/WEB-INF/classes/COM/
**/
Public static URL getabsolutepathwithclass (string path ){
Return webpath. Class. getresource (PATH );
}
/**
* Obtain the absolute path of the directory where the specified class file is located.
*
* @ Param clazz
* Class
* @ Return indicates the absolute path of the class file. For example: <br/> main. Java class under the com. Aries. util. Web package. <br/>
* Path: file :/
* F:/tomcat/webapps/j2eeutil/WEB-INF/classes/COM/Aries/util/web/
**/
Public static URL getabsolutepathwithclass (class clazz ){
Return clazz. getresource ("");
}
}
References: http://zhidao.baidu.com/question/142445629.html