这个答案将适合我的特定问题,但任何人都可以调整它以使其在他的项目中工作。
我的项目有两个依赖于外部文件的 servlet。为了将这些外部文件集中到我的 webapp 文件夹中,我使用了第三个 sevlet 来设置文件的位置。在我的 GWT web.xml 中,我添加了 StartUpServlet 并将其设置为首先由我的 Servlet 容器 (Tomcat) 加载。这是我的 web.xml:
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>StartUpServlet</servlet-name>
<servlet-class>com.banctecmtl.ca.vlp.view.webview.server.StartUpServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>VlpControllerService</servlet-name>
<servlet-class>com.banctecmtl.ca.vlp.view.webview.server.VlpControllerServiceImpl</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>UserAccessService</servlet-name>
<servlet-class>com.banctecmtl.ca.vlp.view.webview.server.UserAccessServiceImpl</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>EventService</servlet-name>
<servlet-class>de.novanic.eventservice.service.EventServiceImpl</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>EventService</servlet-name>
<url-pattern>/VirtualLabPortal/gwteventservice</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>VlpControllerService</servlet-name>
<url-pattern>/VirtualLabPortal/VlpController</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UserAccessService</servlet-name>
<url-pattern>/VirtualLabPortal/UserAccess</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>StartUpServlet</servlet-name>
<url-pattern>/VirtualLabPortal/StartUpServlet</url-pattern>
</servlet-mapping>
<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>VirtualLabPortal.html</welcome-file>
</welcome-file-list>
</web-app>
在这个StartUpServlet 中,我这样设置文件位置:(可能有更好的方法,但它有效):
public class StartUpServlet extends RemoteServiceServlet {
/**
* This Servlet is used to set configuration file locations.
*/
private static final long serialVersionUID = 6459940076859400546L;
private final String CONFIG_FOLDER = "config";
private final String LOG_FOLDER = "logs";
public void init() {
// Check the current of to have the good file separator for file
// browsing
String os = System.getProperty("os.name").toLowerCase();
// windows
String fileSeparator;
if (os.indexOf("win") >= 0) {
fileSeparator = "\\";
} else {
fileSeparator = "/";
}
String jaasConfigPath = super.getServletContext().getRealPath(
CONFIG_FOLDER + fileSeparator + "JaasConfig.conf");
String jaasConfigName = "JaasConfig";
String configFile = super.getServletContext().getRealPath(
CONFIG_FOLDER + fileSeparator + "config.properties");
String log4j = getServletContext().getRealPath(
CONFIG_FOLDER + fileSeparator + "log4j.properties");
String logFile = getServletContext().getRealPath(
LOG_FOLDER + fileSeparator + "vlplog.log");
// Order is important here as the log4j properties file use the system
// property : "logFile"
System.setProperty("logFile", logFile);
PropertyConfigurator.configure(log4j);
System.setProperty("jaasConfigName", jaasConfigName);
System.setProperty("jaasConfigPath", jaasConfigPath);
System.setProperty("configFile", configFile);
}
}
基本上,我得到了 ServletContext 真实路径,因此它在 Tomcat 上为我提供了以下路径:${CATALINA_HOME}/webapps/<myapps>
然后我使用它来设置文件位置:config/config.properties、config/log4j.properties 和我的 config/JaasConfig。
一旦设置好,我就可以在我的其他 servlet 中使用它们,例如 System.getProperty(KEY);
感谢 @Aviram 帮我设置 JaasConfig 的位置。