请帮帮我。我是java的新手,尤其是java web(JSP)。
因此,在我尝试登录的情况下,servlet类将首先调用我的配置类来配置log4j配置文件。
这是我的项目树:
这是我的代码:
登录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);
}
MainConfiguration
public static void loadConfiguration()
{
// To configure place of logger settings file
PropertyConfigurator.configure("configuration/logger_dev.conf");
}
因此,当我尝试运行该项目并尝试登录时,会引发错误。
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上运行。感谢。
答案 0 :(得分:0)
可以从
中读取配置文件等资源.class
文件最终的同一目录)。然后可以通过java.lang.Class.getResource
或java.lang.Class.getResourceAsStream
。WEB-INF
文件夹中的某个位置,以避免将文件传递给客户端。然后可以通过javax.servlet.ServletContext.getResource
或javax.servlet.ServletContext.getResourceAsStream
。