[英]Tomcat jaas.conf/log4j.properties location
我正在開發一個GWT Web應用程序,在部署到Tomcat時,我遇到了一個問題。
問題很簡單,但我不知道如何修復它而不告訴Tomcat在哪里查找這些文件。
當我將我的應用程序直接部署到Tomcat時,它會在/ bin目錄中查找文件,因此如果我將log4j.properties和我的jaas.conf放在那里,它就像一個魅力。
問題是我希望能夠將這些文件保存在我的webapp中。
我該怎么做? 我可以添加到web.xml嗎?
我試圖將兩個文件放入/WEB-INF/classes
目錄,但它沒有用完。
當我在Eclipse中運行我的項目時,我的jaas.config必須位於/ war文件夾中,而我的log4j.properties保留在/ src文件夾中。
編輯:我讀了這個並嘗試了它,即使我不使用log4j進行Tomcat內部日志記錄,但它也沒有用。
我使用Tomcat 7.0
關於jaas.config:實現ServletContextListener
並在contextInitialized
方法中執行以下操作(如果jaas.config位於war的根目錄中,否則,只需更改路徑):
String jaasConfigPath = event.getServletContext().getRealPath("jaas.config");
System.setProperty("java.security.auth.login.config", jaasConfigPath);
這個答案將適合我的特殊問題,但任何人都可以適應它,使其適用於他的項目。
我的項目有兩個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的位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.