2012-03-29 22 views
0

我正在开发一个GWT Web应用程序,并在部署到Tomcat时遇到问题。Tomcat jaas.conf/log4j.properties位置

问题很简单,但我不知道如何解决它,而不告诉Tomcat在哪里查找这些文件。

当我将应用程序部署到Tomcat上时,它会查找/ bin目录中的文件,因此如果将log4j.properties和jaas.conf放在该目录中,它就像一个魅力一样。

事情是我想能够将这些文件保存在我的webapp中。

我该怎么办? 有什么我可以添加到web.xml?

我试图把这两个文件放到/WEB-INF/classes目录中,但没有成功。

当我在Eclipse中运行我的项目时,我的jaas.config必须位于/ war文件夹中,而log4j.properties保留在/ src文件夹中。

编辑: 我读this,并尝试它,即使我不使用log4j的Tomcat内部日志记录,但它也没有工作。

我使用Tomcat 7.0

回答

0

这个答案适合我的特殊问题,但任何人都可以确定地适应它,使它在他的项目中工作。

我的项目有两个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的位置。

2

关于的jaas.config: 实现一个ServletContextListenercontextInitialized方法中执行以下操作(这是如果的jaas.config是在战争的根源,否则,只是改变了路径):

String jaasConfigPath = event.getServletContext().getRealPath("jaas.config"); 
System.setProperty("java.security.auth.login.config", jaasConfigPath); 
+0

我会仔细研究它! – David 2012-04-04 15:18:54