我正在开发一个 GWT 应用程序,我正在尝试从“war/config/config.properties”中检索文件。在 GWT 服务器端,我创建了一个名为 Config.java 的类,它创建了一个新的 Properties 对象并从 config.properties 填充它,我使用 RemoteServiceServlet 扩展了 Config,以便能够使用 getServletContext()。我正在使用以下行来加载属性
prop.load(ServletContext.class.getResourceAsStream("config/config.properties"));
我的 config.properties 文件看起来像这样
database=db
host=192.168.1.1
password=pass
user=user
我的配置类看起来像这样
public class Config extends RemoteServiceServlet {
Properties prop = new Properties();
public Config(){
//load a properties file
try {
ServletContext sc = getServletContext();
prop.load(sc.getResource("/config/config.properties").openStream());
//System.out.println("Get Real Path" + getServletContext().getRealPath("config") + "/config.properties");
//FileInputStream input = (FileInputStream) getServletContext().getResourceAsStream("/config/config.properties");
//prop.load(new FileInputStream(getServletContext().getRealPath("/config/config.properties")));
//prop.load(input);
System.out.println("properties file loaded successfully");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("Could not find properties file");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NullPointerException e) {
System.out.println("null pointer exception in loading properties file");
e.printStackTrace();
}
}
public String getProperty(String name){
return prop.getProperty(name);
}
}
但是对于我的生活,我无法弄清楚为什么在加载属性时会出现空指针异常,只是拒绝工作。堆栈跟踪在这里,任何想法是我做错了什么?
java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Unknown Source)
at java.util.Properties.load0(Unknown Source)
at java.util.Properties.load(Unknown Source)
at com.cbs.ioma.server.Config.<init>(Config.java:19)
at com.cbs.ioma.server.DatabaseServiceImpl.<init>(DatabaseServiceImpl.java:61)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at org.mortbay.jetty.servlet.Holder.newInstance(Holder.java:153)
at org.mortbay.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:339)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:463)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)``
编辑:我更改了我的代码以反映下面的评论,这里发布了新错误
> java.lang.NullPointerException at
> javax.servlet.GenericServlet.getServletContext(GenericServlet.java:160)
> at com.cbs.ioma.server.Config.<init>(Config.java:19)
编辑:我改变了我的课程,你能看看并告诉我我应该如何调用 servlet 吗?如果它被 Servlet 中的另一个方法调用,我不明白我应该如何覆盖该方法。我该如何实施?
public class Config extends RemoteServiceServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
Properties prop = new Properties();
public Config(){
}
public String getProperty(String name){
return prop.getProperty(name);
}
@Override
public void init(ServletConfig config) throws ServletException
{
super.init(config);
try {
prop.load(config.getServletContext().getResource("/config/config.properties").openStream());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}