首先应创建一个模板文件,文件名和文件后缀可以随意,但我一般常用的还是 *.template ,因此,这里就以 template.template 为例( 将模板文件放入 /WEB-INF/templates/ 文件夹下 ):
template.template:
<html>
<head>
<meta http-equiv = "Content-Type" content = "text/html; charset=gb2312" >
<title>#title#</title>
</head>
<body bgcolor = "#CCCCCC" >
<h3 align = "center" color = "#FFFFFF">
<font color = "#FFFFFF" >
#title#
<br><hr><br>
author:
</font>
<font color = "red" >#author#</font>
<br>
<font color = "#FFFFFF" >content:</font>
<font color = "red" >#content#</font>
<br><hr><br>
<font color = "#FFFFFF" >Copy Right 2007 - 2008 © Power by Easyworks.cn</font>
</h3>
</body>
</html>
由于只是介绍一下原理,所以就只用一个 JSP 页面来处理:
Test.jsp:
<%@ page language = "java" %>
<%@ page import = "java.util.* , java.io.*" %>
<%@ page contentType = "text/html; charset=gb2312" pageEncoding = "gb2312" %>
<%
//设置字符编码
request.setCharacterEncoding( "gb2312" );
response.setCharacterEncoding( "gb2312" );
String title = "This is a title 标题";
String author = "Easyoworks.cn 作者";
String content = "This is the Content Area 正文";
String filePath = "";
// 获得模板文件的路径
filePath = request.getRealPath( "/" ) + "WEB-INF/templates/template.template";
/ 打印出路径
out.println( filePath ); // 注释
String templateContent = "";
// 读取模板文件
FileInputStream fis = new FileInputStream( filePath );
int length = fis.available();
byte[] bytes = new byte[ length ];
fis.read( bytes );
fis.close();
templateContent = new String( bytes );
// 打印出模板内容
out.println( "以下是模板内容:<br>" + templateContent + "<br>以下是置换以后的 html 内容<br>" ); // 注释
templateContent = templateContent.replaceAll( "#title#" , title );
templateContent = templateContent.replaceAll( "#author#" , author );
templateContent = templateContent.replaceAll( "#content#" , content );
// 根据时间得出文件名
Calendar cld = Calendar.getInstance();
String fileName = String.valueOf( cld.getTimeInMillis() ) + ".html";
// 生成的 html 文件保存路径
fileName = request.getRealPath( "/" ) + fileName;
out.println( templateContent ); // 注释
// 建立文件输出流
FileOutputStream fos = new FileOutputStream( fileName );
byte[] tag_bytes = templateContent.getBytes();
fos.write( tag_bytes );
fos.close();
%>
template.template:
<html>
<head>
<meta http-equiv = "Content-Type" content = "text/html; charset=gb2312" >
<title>#title#</title>
</head>
<body bgcolor = "#CCCCCC" >
<h3 align = "center" color = "#FFFFFF">
<font color = "#FFFFFF" >
#title#
<br><hr><br>
author:
</font>
<font color = "red" >#author#</font>
<br>
<font color = "#FFFFFF" >content:</font>
<font color = "red" >#content#</font>
<br><hr><br>
<font color = "#FFFFFF" >Copy Right 2007 - 2008 © Power by Easyworks.cn</font>
</h3>
</body>
</html>
由于只是介绍一下原理,所以就只用一个 JSP 页面来处理:
Test.jsp:
<%@ page language = "java" %>
<%@ page import = "java.util.* , java.io.*" %>
<%@ page contentType = "text/html; charset=gb2312" pageEncoding = "gb2312" %>
<%
//设置字符编码
request.setCharacterEncoding( "gb2312" );
response.setCharacterEncoding( "gb2312" );
String title = "This is a title 标题";
String author = "Easyoworks.cn 作者";
String content = "This is the Content Area 正文";
String filePath = "";
// 获得模板文件的路径
filePath = request.getRealPath( "/" ) + "WEB-INF/templates/template.template";
/ 打印出路径
out.println( filePath ); // 注释
String templateContent = "";
// 读取模板文件
FileInputStream fis = new FileInputStream( filePath );
int length = fis.available();
byte[] bytes = new byte[ length ];
fis.read( bytes );
fis.close();
templateContent = new String( bytes );
// 打印出模板内容
out.println( "以下是模板内容:<br>" + templateContent + "<br>以下是置换以后的 html 内容<br>" ); // 注释
templateContent = templateContent.replaceAll( "#title#" , title );
templateContent = templateContent.replaceAll( "#author#" , author );
templateContent = templateContent.replaceAll( "#content#" , content );
// 根据时间得出文件名
Calendar cld = Calendar.getInstance();
String fileName = String.valueOf( cld.getTimeInMillis() ) + ".html";
// 生成的 html 文件保存路径
fileName = request.getRealPath( "/" ) + fileName;
out.println( templateContent ); // 注释
// 建立文件输出流
FileOutputStream fos = new FileOutputStream( fileName );
byte[] tag_bytes = templateContent.getBytes();
fos.write( tag_bytes );
fos.close();
%>