1

I have a .csv file that needs to be read for my jsp. I have a class that reads the csv and popuates a map inside that class upon construction, and that map is accessed by a jsp that I have. I have the jsp creating the class and accessing the map.

However, it seems that the class cannot seem to find the .csv file in the project.

How make the storage class able to find the csv in my project?

user906153
  • 1,218
  • 8
  • 30
  • 43

1 Answers1

6

That depends on where the CSV file is. If it's in the public web folder, then use

InputStream input = getServletContext().getResourceAsStream("/filename.csv");
// ...

Or if it's in the classpath, then use

InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("filename.csv");
// ...

Unrelated to the concrete problem, you really want to do this in a Servlet class, not in a JSP file.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555