0

我有一个项目,用户从 jsp 页面登录。后来通过一种方法,我验证了属性文件中的用户名和密码。但是执行时出现以下错误:

java.io.FileNotFoundException: config.properties (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at com.djbsso.model.dao.ProcessLogin.processLogin(ProcessLogin.java:17)
    at com.djbsso.controller.LoginServlet.doPost(LoginServlet.java:42)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Unknown Source)
0
Sep 10, 2013 12:23:09 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
    at org.apache.jsp.dashboard_jsp._jspService(dashboard_jsp.java:63)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Unknown Source)


try { 
  prop.load(new FileInputStream("config.properties")); 
  actUsername = prop.getProperty("CCB_USERNAME"); 
  actPassword = prop.getProperty("CCB_PASSWORD"); 
  System.out.println(actUsername); System.out.println(actPassword);
   if (username.equals(actUsername) && password.equals(actPassword)) { 
       isLoggedIn = 1; 
   }  

这是我调用属性文件的代码:

package com.djbsso.model.dao;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class ProcessLogin {

    private final Properties prop = new Properties();

    public int processLogin(String username, String password, String application) {
        int isLoggedIn = 0;
        String actUsername = null;
        String actPassword = null;
        try {
            prop.load(new FileInputStream("config.properties"));

            actUsername = prop.getProperty("CCB_USERNAME");
            actPassword = prop.getProperty("CCB_PASSWORD");
            System.out.println(actUsername);
            System.out.println(actPassword);
            if (username.equals(actUsername) && password.equals(actPassword)) {
                isLoggedIn = 1;
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return isLoggedIn;
    }

}

有人可以帮我在哪里保存属性文件吗?

4

3 回答 3

1

如果将配置文件放在类路径中,则可以调用

InputStream is = getClass().getResourceAsStream("config.properties");
于 2013-09-10T07:17:56.397 回答
0
Properties prop = new Properties();  
prop.load(getClass().getClassLoader().getResourceAsStream("config.properties"));  

那你可以试试

actUsername = prop.getProperty("CCB_USERNAME");
actPassword = prop.getProperty("CCB_PASSWORD");
...
....

在哪里放置你的属性文件最好的解释

于 2013-09-10T07:20:39.420 回答
0

你也可以这样做:

props.load(new FileInputStream("/var/config.propertoes")).

parameter for FileInputStream must be absolute path of config file
于 2013-09-10T07:45:31.627 回答