不难的,用文件操作即可实现。给你一个我以前写的类:CreateHtml.javapackage HtmlUtil;import java.io.*;
import java.util.*;public class CreateHtml
{
private class BlockTag
{
public String Tag = null;
public String replaceString = null;
public BlockTag(String Name,String Replace)
{
Tag = "<!--###"+Name+"###-->";
replaceString = Replace;
}
}//end subclass
private Vector replaceTags = new Vector();
private String templateName = null;
private String outfileName = null;
public CreateHtml(String TemplateName,String FileName)
{
templateName = TemplateName;
outfileName = FileName;
}
protected void finalize()
{
replaceTags.removeAllElements();
}
public void AddTag(String Name,String Replace)
{
BlockTag item = new BlockTag(Name,Replace);
replaceTags.addElement(item);
}
private String isTag(String str)
{
Enumeration items = replaceTags.elements();
str = str.trim();
while(items.hasMoreElements())
{
BlockTag item = (BlockTag) items.nextElement();
if( str.equals(item.Tag) )
return item.replaceString;
else continue;
}
return null;
}
public void Write()
{
try {
BufferedReader in = new BufferedReader(new FileReader(templateName));
PrintWriter out = new PrintWriter(new FileWriter(outfileName),true);
String tempStr = null;
String str = null;
while( (tempStr = in.readLine()) != null )
{
if( (str = isTag(tempStr)) != null )
{
out.println(str);
}
else
{
out.println(tempStr);
}
}
out.close();
}//end try
catch(FileNotFoundException fx)
{
System.err.println("file can found!");
}
catch(IOException ix)
{
System.err.println("IO error");
}
}}//end class
import java.util.*;public class CreateHtml
{
private class BlockTag
{
public String Tag = null;
public String replaceString = null;
public BlockTag(String Name,String Replace)
{
Tag = "<!--###"+Name+"###-->";
replaceString = Replace;
}
}//end subclass
private Vector replaceTags = new Vector();
private String templateName = null;
private String outfileName = null;
public CreateHtml(String TemplateName,String FileName)
{
templateName = TemplateName;
outfileName = FileName;
}
protected void finalize()
{
replaceTags.removeAllElements();
}
public void AddTag(String Name,String Replace)
{
BlockTag item = new BlockTag(Name,Replace);
replaceTags.addElement(item);
}
private String isTag(String str)
{
Enumeration items = replaceTags.elements();
str = str.trim();
while(items.hasMoreElements())
{
BlockTag item = (BlockTag) items.nextElement();
if( str.equals(item.Tag) )
return item.replaceString;
else continue;
}
return null;
}
public void Write()
{
try {
BufferedReader in = new BufferedReader(new FileReader(templateName));
PrintWriter out = new PrintWriter(new FileWriter(outfileName),true);
String tempStr = null;
String str = null;
while( (tempStr = in.readLine()) != null )
{
if( (str = isTag(tempStr)) != null )
{
out.println(str);
}
else
{
out.println(tempStr);
}
}
out.close();
}//end try
catch(FileNotFoundException fx)
{
System.err.println("file can found!");
}
catch(IOException ix)
{
System.err.println("IO error");
}
}}//end class
String urladdress = getServletContext().getRealPath("/");
CreateHtml create = new CreateHtml(urladdress+"/index.template",urladdress+"/index.html");
create.AddTag("new",news);
create.Write();所用的html的模板文件部分如下:
</tr>
<!--###new###-->
<tr> 这样就把这个<!--###new###-->替换成了从数据库中取出的内容。其实你想要的就是生成静态html文件的方法,用jsp或servlet都是一样的,而且不如生成html以后直接显示这个html比显示jsp要好吧?
URL myURL=new URL("http://XXX.test.jsp");
InputStream in = myURL.openStream();
BufferReader bf = new BufferReader(in);
StringBuffer sb = new StringBuffer();
String b;
while((b= bf.readLine())!=null){
sb.append(b);
}
import java.io.*;
import java.net.*;
import javax.servlet.http.*;public class URLToFile
{
java.net.URL url=null;
int fn=0;
String currentFile=""; public URLToFile()
{
}public synchronized String getFile (String fileURL,String dirPath,String extension,HttpServletRequest request)
{
String nm="autogen"+fn+extension;
String filePath="";
fn++;
try{
url=new java.net.URL(fileURL); InputStream in=url.openStream();
String p=dirPath+"/"+nm;
filePath=request.getRealPath(p);
FileOutputStream out=new FileOutputStream(filePath);
int st;
while ((st=in.read())!=-1)
{
out.write(st);
}
currentFile=filePath;
}catch(Exception e){System.out.println("error creating file from url...."+e);}
System.out.println("file created."+nm);
return filePath;
}}