When you create a dynamic web app project in Eclipse, the content that will go into the root of the war file gets packaged from the WebContent folder.
It sounds like you want to access a file from the directory WEB-INF/templates at runtime for your web app.
I assume that you are using the absolute path to access a file from there presently. You have already figured out that this probably will not work for your app, once it is deployed.
You will need to access the content of the file using ServletContext.getResourceAsStream(String).
The following snippet finds a file named WEB-INF/templatez/myfile.txt from a servlet that is part if the web app that contains the myfile.txt file. Other web apps and users will will not be able to access the file via http GET requests.
package a;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name="FileFinder", urlPatterns={"/FileFinder"})
public class FileFinder extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
//* TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet FileFinder</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet FileFinder at " + request.getContextPath () + "</h1>");
InputStream is = null;
try {
is = request.getServletContext().getResourceAsStream("/WEB-INF/templatez/myfile.txt");
out.println((null == is ? "did not " : "did ") + "find the file myfile.txt");
} finally {
if (null != is) is.close();
}
out.println("</body>");
out.println("</html>");
//*/
} finally {
out.close();
}
}
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}
}