StayClone StayClone - 1 year ago 54
HTML Question

Creating html file based on template in java web application

I am creating a web application and upon user registration I would like to create a html file named by that username (i.e. joebloggs123 is username, therefore the html file is joebloggs123.html). I would like to use a template html file as it will cover basic profile content for the user when created.
So far I have the following:

File htmlTemplateFile = new File("/Convocast-war/web/userTemplate.html");
String htmlString = FileUtils.readFileToString(htmlTemplateFile);
String title = username + "'s Profile";
htmlString = htmlString.replace("$title", title);
File newHtmlFile = new File("/userPages/" + username + ".html");
FileUtils.writeStringToFile(newHtmlFile, htmlString);

However I am getting filenotfoundexception on the htmlTemplateFile path.
Included is my directory structure but no matter the file path I use it will not find the file.
I have tried many other variations but nothing seems to find the file.
File Structure

rD. rD.
Answer Source

You can use ServletContext.getRealPath() to locate that file.

change your first line to:

File f = new File(getServletContext().getRealPath("/userTemplate.html"));