Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create a html page dynamically everytime I click on The button.

What I have tried:

public class CreateHtmlDocument {

public static void main(String[] args)
{

int i=0;




try {
//define a HTML String Builder
StringBuilder htmlStringBuilder=new StringBuilder();
//append html header and title
htmlStringBuilder.append("<html><head><title>hello </title></head>");
//append body
htmlStringBuilder.append("<body>");
//append table
htmlStringBuilder.append("");
//append row
htmlStringBuilder.append("");
//append row
htmlStringBuilder.append("");
//append row
htmlStringBuilder.append("");
//close html file
htmlStringBuilder.append("
TestIdTestNameTestResult
001LoginPassedsssss
002LogoutPassedssss
</body></html>");
//write html string content to a file
i++;
String a="t.html";

WriteToFile(htmlStringBuilder.toString(),a);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void WriteToFile(String fileContent, String fileName) throws IOException {

String projectPath = System.getProperty("user.dir");

String tempFile = projectPath + File.separator+fileName;

System.out.println(tempFile);
File file = new File(tempFile);
// if file does exists, then delete and create a new file
if (file.exists()) {
try {
File newFileName = new File(projectPath + File.separator+ "backup_"+fileName);
file.renameTo(newFileName);
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//write to file with OutputStreamWriter
OutputStream outputStream = new FileOutputStream(file.getAbsoluteFile());
Writer writer=new OutputStreamWriter(outputStream);
writer.write(fileContent);
writer.close();

}
}
Posted
Updated 23-Aug-16 0:47am
Comments
W Balboos, GHB 23-Aug-16 6:27am    
The majority of such pages use PHP to create a page dynamically on the server side. Pages can modify themselves as they render, on the client side, or with the click of a button (see DOM). This custom rendering is all form a single page vs. the possibility of creating a new page on the server and saving it (another rather odd option).
Nitish Govekar 23-Aug-16 6:40am    
Can you please elaborate sir?
W Balboos, GHB 23-Aug-16 6:41am    
See answer coming, below, in the solution section.

1 solution

A php page, which can be an HTLM page with .php as its extension instead of HTM or HTML, will be read on the server side and the php scripts executed. These scripts can do essentially anything a programming language could do.

Commonly, you "ECHO" some text to the page, via the script, that is all or part of an HTML statement. When this is sent to the user (client) it will render just like it had been typed onto the page from an editor. Thus, if the page has the login ID of a user it could create a page customized with their information. It comes with built-in MySQL methods and Microsoft will supply a SQL Server library for access to their database. If you get good at this, you can even use ODBC and access all sorts of data sources.

To learn how to do this, try: PHP 5 Tutorial[^], which is where I started out learning the language. It is similar to javaScript in many way and if you know javaScript or any of the C-like languages you'll catch on rather quickly.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900