Reputation: 777
I am working on a GWT application, and I'm trying to retrieve the file from "war/config/config.properties". In GWT server side I made a class called Config.java which creates a new Properties object and populates it from config.properties, I've extended Config with RemoteServiceServlet to be able to use getServletContext(). I am using the following line to load the properties
prop.load(ServletContext.class.getResourceAsStream("config/config.properties"));
and my config.properties file looks like this
database=db
host=192.168.1.1
password=pass
user=user
and my config class looks like this
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);
}
}
and yet for the life of me I can't figure out why I'm getting a null pointer exception when I am loading properties, simply refuses to work. Stack trace is here, any idea's what I'm doing wrong?
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)``
edit: I changed my code to reflect a comment below, new error is posted here
> java.lang.NullPointerException at
> javax.servlet.GenericServlet.getServletContext(GenericServlet.java:160)
> at com.cbs.ioma.server.Config.<init>(Config.java:19)
edit: I changed my class, could you take a look and tell me how I'm supposed to call the servlet? I don't get how I'm supposed to override the method, if its called by another method in Servlet. how do I implement it?
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();
}
}
}
Upvotes: 1
Views: 1373
Reputation: 290
I propose you use a higher level library to take care of that for you.
For example use ResourceBundle
ResourceBundle bundle = ResourceBundle.getBundle(fully.qualified.path.using.dots.to.properties);
bundle.getString("your_property_key");
Upvotes: 0
Reputation: 280092
I misread your question. If the file is in war/config/config.properties
, you should use
ServletContext sc = ...// get servlet context;
sc.getResource("/config/config.properties");
as described in the javadoc:
Returns a URL to the resource that is mapped to a specified path. The path must begin with a "/" and is interpreted as relative to the current context root.
Edit
When a Servlet
container starts up, it instantiates your Servlet
(Config
for you) class using the no-args constructor. It then calls its Servlet#init(ServletConfig)
method where you can finally have access to the ServletContext
. This is where you should initialize your Properties
object. So override the method and move your current constructor code to it.
For your information
Don't do this
ServletContext.class.getResourceAsStream("config/config.properties")
If config/config.properties
is at the root of the classpath, use
ServletContext.class.getResourceAsStream("/config/config.properties")
The top case will take the name of the package that ServletContext
is in and try to resolve your specified path with it. For example, ServletContext
is in javax.servlet
, so
/javax/servlet/config/config.properties
Your resource is clearly not in there. The rules for what path you should specify are described in the javadoc of Class#getResource(String)
.
Upvotes: 1