0

I am working on a project for my grad school. And I am using Servlets for that. Now, I need to read a HTML file (that is located on the server's webapp directory itself) from my servlet.

For example, the sample code is:

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    PrintWriter pw = response.getWriter();
    response.setContentType("text/html");

    StringBuilder sb = new StringBuilder();

    try{
    FileReader fr = new FileReader(new File("..\\..\\WebContent\\index.html"));

    int ch;

    while((ch = fr.read()) != -1)
    {
        sb.append((char)ch);
    }

    pw.print(new String(sb));
    fr.close();
    }catch(Exception exc){
        exc.printStackTrace();
    }
}

The above code returns an exception: FileNotFoundException even though the index.html file exists. What is the remedy for this? Is there a workaround?

And BTW, I am using the Eclipse JEE Mars IDE along with Tomcat 7 Web Server

RisingHerc
  • 694
  • 1
  • 9
  • 26
  • Try placing index.html in the WEB-INF folder and then access it directly without giving any other path. – Sampada Apr 02 '16 at 05:34
  • Check this for more info: http://stackoverflow.com/questions/19786142/what-is-web-inf-used-for-in-a-java-ee-web-application – Sampada Apr 02 '16 at 05:37

0 Answers0