1

I have:

a JSP-file called index.jsp which contains the following code:

<%
    JSONVerschillendeTalen jsonVerschillendeTalen = JSONVerschillendeTalen.getInstance();
    JSONObject algemeenJSON = null;
    JSONObject indexJSON = null;
    try {
        indexJSON = jsonVerschillendeTalen.getJSONObject(request, "Index.txt");
        algemeenJSON = jsonVerschillendeTalen.getJSONObject(request, "Algemeen.txt");
    } catch (ParseException e) {
        e.printStackTrace();
    }
%>

And a class called JSONVerschillendeTalen which contains the following code:

public class JSONVerschillendeTalen {

    private static JSONVerschillendeTalen jsonVerschillendeTalen = new JSONVerschillendeTalen();
    private JSONParser jsonParser = new JSONParser();

    public static JSONVerschillendeTalen getInstance() {
        return jsonVerschillendeTalen;
    }

    public JSONObject getJSONObject(HttpServletRequest req, String bestandsnaam) throws IOException, ParseException {
        return (JSONObject) jsonParser.parse(new FileReader(getClass().getClassLoader()
            .getResource("NL/" + bestandsnaam).getPath().replaceAll("%20", " ")));
    }

    private JSONVerschillendeTalen() {

    }

}

Whenever I run the code in the index.jsp file in a normal class (for testing), it works. When I run the same code in the jsp file, it doesn't. Does anybody know why?

Properties directory

Sjoerd
  • 47
  • 1
  • 10

1 Answers1

0

Try to use getClass().getResource() instead of getClass(). getClassLoader().getResource(). The second method search resource relatively to ClassLoader root, which highly likely differs from your sources directory in web container.

Following answer has a very good explanation of difference between both.

Community
  • 1
  • 1
Anton Dovzhenko
  • 2,399
  • 11
  • 16