Servlet无法找到指定的文件

时间:2017-06-14 02:27:53

标签: java jsp servlets

请帮帮我。我是java的新手,尤其是java web(JSP)。

因此,在我尝试登录的情况下,servlet类将首先调用我的配置类来配置log4j配置文件。

这是我的项目树:

enter image description here

这是我的代码:

  1. 登录Servlet

    protected void doPost(HttpServletRequest request, HttpServletResponse response)  
        throws ServletException, IOException{  
    
        System.out.println("Before Starting...");
        MainConfiguration.loadConfiguration();
    
        response.setContentType("text/html");
        RequestDispatcher rd=request.getRequestDispatcher("home.jsp");  
        rd.forward(request, response);
    }
    
  2. MainConfiguration

    public static void loadConfiguration()
    {
        // To configure place of logger settings file
        PropertyConfigurator.configure("configuration/logger_dev.conf");
    }
    
  3. 因此,当我尝试运行该项目并尝试登录时,会引发错误。

    Before Starting...
    log4j:ERROR Could not read configuration file [configuration/logger_dev.conf].
    java.io.FileNotFoundException: configuration\logger_dev.conf (The system cannot find the file specified)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:297)
        at org.apache.log4j.PropertyConfigurator.configure(PropertyConfigurator.java:315)
        at com.utility.MainConfiguration.loadConfiguration(MainConfiguration.java:47)
        at com.controller.LoginServlet.doPost(LoginServlet.java:20)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    log4j:ERROR Ignoring configuration file [configuration/logger_dev.conf].
    

    但是当我尝试使用java main调用我的配置类时,它运行正常。

    请建议如何让它在servlet上运行。感谢。

1 个答案:

答案 0 :(得分:0)

可以从

中读取配置文件等资源
  1. 文件系统。建议不要这样做,因为该文件通常不是部署的WAR的一部分,因此必须单独处理(当您不知道它在部署后在文件系统上的最终位置时)。
  2. 类路径。确保构建过程将文件复制到已部署的类路径(.class文件最终的同一目录)。然后可以通过java.lang.Class.getResourcejava.lang.Class.getResourceAsStream
  3. 访问该文件
  4. WEB内容,例如在WEB-INF文件夹中的某个位置,以避免将文件传递给客户端。然后可以通过javax.servlet.ServletContext.getResourcejavax.servlet.ServletContext.getResourceAsStream
  5. 访问它