jsp页面获取数据后怎样生成HTML静态页面

jsp页面获取数据后怎样生成HTML静态页面,第1张

public class HTMLMaker {

final static Object lock = new Object();

public static void makeHtml(String page, String filePath){

makeHtml(page,filePath,"UTF-8");

}

public static void makeHtml(String page, String filePath,String chartset) {

synchronized (lock) {

HttpURLConnection huc = null;

BufferedReader br = null;

BufferedWriter bw = null;

try {

huc = (HttpURLConnection)new URL(page)openConnection();

SystemsetProperty("sunnetclientdefaultConnectTimeout", "30000");

SystemsetProperty("sunnetclientdefaultReadTimeout", "30000");

hucconnect();

InputStream stream = hucgetInputStream();

bw = new BufferedWriter(new OutputStreamWriter (new FileOutputStream(filePath),chartset));

br = new BufferedReader(new InputStreamReader(stream, chartset));

String line;

while((line = brreadLine())!= null){

if(linetrim()length() > 0){

bwwrite(line);

bwnewLine();

}

}

}catch (Exception e) {

eprintStackTrace();

}finally {

try {

brclose();

bwclose();

hucdisconnect();

}catch (Exception e) {

eprintStackTrace();

}

}

}

}

}

参数解释:

page:jsp页面的网络地址,比如http://localhost:8080/xxx/indexjsp

filePath:生成的html文件的保存路径

chartset:编码,不填默认utf-8。

把下面代码复制到文本文档中,然后将文本文档改成"html"形式

这个是提取表格中的数据的方法,看看是你想要的不

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 40 Transitional//EN">

<HTML>

<HEAD>

<TITLE> New Document </TITLE>

<META NAME="Generator" CONTENT="EditPlus">

<META NAME="Author" CONTENT="">

<META NAME="Keywords" CONTENT="">

<META NAME="Description" CONTENT="">

</HEAD>

<SCRIPT LANGUAGE="JavaScript">

<!--

//获得table的tr td等属性

function getTableAtt(){

alert('该table有'+documentalloTablessrowslength+'个tr')

for (i=0; i < documentalloTablessrowslength; i++) {

//循环每个tr里的td

for (j=0; j < documentalloTablessrows(i)cellslength; j++) {

alert('第'+(i+1)+'个tr中的第'+(j+1)+'个td的值为:'+documentalloTablessrows(i)cells(j)innerText)

}

}

}

//-->

</SCRIPT>

<BODY>

<FORM METHOD=POST ACTION="" name="myform">

<hr>

<TABLE ID=oTabless border = "1">

<TR><TD>1</TD><TD>2</TD><TD>3</TD><TD>4</TD></TR>

<TR><TD>5</TD><TD>6</TD><TD>7</TD><TD>8</TD></TR>

</TABLE>

<INPUT TYPE="button" VALUE="Table" onclick="getTableAtt()">

<hr>

</FORM>

</BODY>

</HTML>

大概就是一楼的那个意思 给你个小例子你看下;

先创建一个html模板

<html>

<head>

<title>###title###</title>

<meta http- equiv="Content-Type" content="text/html; charset=gb2312">

<LINK href="/csscss" rel=stylesheet type=text/css>

</head>

<body>

<table width="500" border="0" align="center" cellpadding="0"

cellspacing="2">

<tr>

<td align="center">

###title###

</tr>

<tr>

<td align="center">

作者:###author###

</tr>

<tr>

<td align="center">

###content###

</td>

</tr>

</table>

</body>

</html>

java代码

import javautil;

import javaio;

public class HtmlFile {

public static void main(String[] args) {

try {

String title = "Make Html";

String content = "小样,还搞不定你";

String editer = "秋水";

//模板路径

String filePath = "leonhtml";

Systemoutprint(filePath);

String templateContent = "";

FileInputStream fileinputstream = new FileInputStream(filePath);// 读取模板文件

int lenght = fileinputstreamavailable();

byte bytes[] = new byte[lenght];

fileinputstreamread(bytes);

fileinputstreamclose();

templateContent = new String(bytes);

Systemoutprint(templateContent);

templateContent = templateContentreplaceAll("###title###", title);

templateContent = templateContentreplaceAll("###content###",

content);

templateContent = templateContent

replaceAll("###author###", editer);// 替换掉模板中相应的地方

Systemoutprint(templateContent);

// 根据时间得文件名

Calendar calendar = CalendargetInstance();

String fileame = StringvalueOf(calendargetTimeInMillis())

+ "html";

fileame = "/" + fileame;// 生成的html文件保存路径。

FileOutputStream fileoutputstream = new FileOutputStream(fileame);// 建立文件输出流

Systemoutprint("文件输出路径:");

Systemoutprint(fileame);

byte tag_bytes[] = templateContentgetBytes();

fileoutputstreamwrite(tag_bytes);

fileoutputstreamclose();

} catch (Exception e) {

Systemoutprint(etoString());

}

}

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/12179349.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存