Reading a HTML file from web content folder in Servlet

1.7k Views Asked by At

I am trying to read an .html file from war file. I have used the code below, but it does not work.

Project structure:

Project
|_war
    |_Pages
      |_HtmlFiles
        |_sample.html

My code:

InputStream inputStream = EmailMessages.class.getClassLoader()
        .getResourceAsStream("/sample.html");
2

There are 2 best solutions below

3
On

The root folder is on the classpath, so try this one:

this.getClass().getResourceAsStream("/Pages/HtmlFiles/sample.html");
5
On

Place you files in src\main\resources folder and when you export a war you can find your files in the resources directory under WEB-INF\classes folder. Now read the html file like this,

InputStream in = this.getClass().getResourceAsStream("/Pages/html/index.html");