如何使用jsp和java动态创建HTML页面? [英] How to create HTML page dynamically using jsp and java?

查看:75
本文介绍了如何使用jsp和java动态创建HTML页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在每次点击按钮时动态创建一个html页面。



我尝试了什么:



公共类CreateHtmlDocument {



public static void main(String [] args)

{



int i = 0;









尝试{

//定义HTML字符串生成器

StringBuilder htmlStringBuilder = new StringBuilder();

//附加html标题和标题

htmlStringBuilder.append(< html>< head>< title> hello< / title>< / head>);

//追加正文

htmlStringBuilder.append(< body>);

//追加表

htmlStringBuilder.append();

//追加行

htmlStringBuilder.append();

//追加行

htmlStringBuilder.append();

//追加行

htmlStringBuilder.append();

//关闭html文件

htmlStringBuilder.append(

< TD>登录
TestId 测试名 的TestResult
001 Passedsssss
002 注销 Passedssss
< / body>< / html>);

//将html字符串内容写入文件

i ++;

字符串a =t.html;



WriteToFile(htmlStringBuilder.toString(),a);

} catch(IOException e){

e.printStackTrace();

}

}

public static void WriteToFile(String fileContent,String fileName)抛出IOException {



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



String tempFile = projectPath + File.separator + fileName;



System.out.println(tempFile);

文件文件=新文件(tempFile);

//如果文件存在,则删除并创建一个新文件

if(file.exists()){

try {

File newFileName = new File(projectPath + File。 separator +backup _+ fileName);

file.renameTo(newFileName);

file.createNewFile();

} catch(IOException e ){

e.printStackTrace();

}

}

//使用OutputStreamWriter写入文件

OutputStream outputStream = new FileOutputStream(file.getAbsoluteFile());

Writer writer = new OutputStreamWriter(outputStream);

writer.write(fileContent);

writer.close();



}

}

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();

}
}

推荐答案

一个php页面,可以是一个HTLM页面,其中.php作为其扩展而不是HTM或HTML,将在服务器端读取并执行php脚本。这些脚本基本上可以完成编程语言可以做的任何事情。



通常,你通过脚本eCHO一些文本到页面,全部或部分一个HTML语句。当这被发送给用户(客户端)时,它将呈现就像从编辑器输入到页面上一样。因此,如果页面具有用户的登录ID,则它可以创建用其信息定制的页面。它带有内置的MySQL方法,Microsoft将提供一个SQL Server库来访问他们的数据库。如果您对此有所了解,您甚至可以使用ODBC并访问各种数据源。



要了解如何执行此操作,请尝试:PHP 5教程 [ ^ ],这是我开始学习语言的地方。它在很多方面类似于javaScript,如果你知道javaScript或任何类似C语言的话,你会很快就能找到它。
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.


这篇关于如何使用jsp和java动态创建HTML页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆