[英]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.