0

I am trying to get the properties from system.properties file which looks like this -

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/stud_mng" 
duser="root"
dpass=""
logfile=d:/log/test_log.txt

I also mapped this file in my web.xml like this -

<servlet>
    <description>
        This is the description of my J2EE component
    </description>
    <display-name>
        This is the display name of my J2EE component
    </display-name>

    <servlet-name>InitServlet</servlet-name>
    <servlet-class>com.dts.core.util.InitServlet</servlet-class>

    <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/config/system.properties</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>InitServlet</servlet-name>
    <url-pattern>/initservlet</url-pattern>
</servlet-mapping>

Now here this InitServlet.java file problem came, from here I am trying to fetch the properties of system.properties which is saved under config folder. Here is the code which is throwing exception from line props.load(fis);, I am posting few lines exception and the structure of folder tree -

package com.dts.core.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;

import com.dts.core.dao.AbstractDataAccessObject;
import com.dts.core.db.DBFactory;

public class InitServlet extends HttpServlet
{
    AbstractDataAccessObject dobject;

    public void init(ServletConfig sc)
    {
        ServletContext ctx = sc.getServletContext();
        InputStream fis = ctx.getResourceAsStream(sc.getInitParameter("config"));
        Properties props = new Properties();

        try
        {
            props.load(fis);
        }
        catch (IOException ioe)
        {
            ioe.printStackTrace();
        }
        dobject = new AbstractDataAccessObject();
        dobject.setProperties(props);

Mar 27, 2016 1:06:30 AM org.apache.catalina.core.StandardContext loadOnStartup SEVERE: Servlet /MobileServices threw load() exception java.lang.NullPointerException at java.util.Properties$LineReader.readLine(Properties.java:434) at java.util.Properties.load0(Properties.java:353) at java.util.Properties.load(Properties.java:341) at com.dts.core.util.InitServlet.init(InitServlet.java:26) at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193) at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088) at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ......

Project directory structure

Also I am wondering why configuration file folder is created in Netbeans projects. Can I use this folder to store my system.properties file, if Yes then from here how I will get the properties. See in image the folder I am talking about - Highlight with Pink Color

I will be very thankful for any suggestion/guidance/advice.

Amit Roy
  • 33
  • 5
  • did you check this out http://stackoverflow.com/questions/9892480/loading-properties-file-in-init-of-servlet-without-using-context-param-tag-in ? – Deendayal Garg Mar 27 '16 at 05:19
  • @DeendayalGarg what special written in the post you mention same solution code. Is there any difference in my code and in accepted solution code. – Amit Roy Mar 27 '16 at 06:42
  • did you try debugging and see whats the value of "sc.getInitParameter("config")" ? – Deendayal Garg Mar 27 '16 at 06:58
  • @DeendayalGarg yes I am getting the values from the config file. Here is code `System.out.println(fileName); props.load(fis); System.out.println(props.getProperty("url"));` and this is the out put >/WEB-INF/config/system.properties >jdbc:mysql://localhost:3306/stud_mng – Amit Roy Mar 27 '16 at 07:11
  • @DeendayalGarg But now after solving this issue I am facing this problem WARNING: java.lang.ClassNotFoundException: "com.mysql.jdbc.Driver" java.lang.ClassNotFoundException: "com.mysql.jdbc.Driver", I have added MySQL Driver in library folder of project still getting this error – Amit Roy Mar 27 '16 at 07:16
  • You're doing this wrong. The resource should be declared as such, the no your context,xml, not in a .properties file. And a .propertties file is not the same as System properties. – user207421 Mar 27 '16 at 09:38

1 Answers1

1

I found the error and corrected it. Its a silly mistake. As you can see in my main Post I gave the code of system.properties like this

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/stud_mng" 
duser="root"
dpass=""
logfile=d:/log/test_log.txt

in above code there are 2 mistakes

1. url=jdbc:mysql://localhost:3306/stud_mng" 

and

2. dpass=""

I replace these 2 lines -

url=jdbc:mysql://localhost:3306/stud_mng" 
dpass=

And project is running perfectly.

May this help anyone.

Amit Roy
  • 33
  • 5