高分求jsp生成html静态文件的方法(100)分

ddrisme 2004-08-28 05:06:46
本人想写一个文章系统。想生成静态html文件,节省空间和cpu利用率。
网上都是asp php的文章。求jsp生成html的方法
谢谢了
...全文
365 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
scorpio2 2004-12-09
  • 打赏
  • 举报
回复
啊,可以研究一下
catblue 2004-12-09
  • 打赏
  • 举报
回复
//建立代表目前目录位置中a.jsp档的File变量, 并由fileName变量变数引用
common com = new common();
String currday = common.getLocalTimeString1();
String wjName = currday + com.random() +".html";
String path = request.getRealPath("/html");
File fileName = new File(path, wjName);
if(fileName.exists())
{
fileName.delete();
//输出目前所在的目录路径
}
else
{
//在目前的目录下建立一个名为wjName的文字档
fileName.createNewFile();
//输出目前所在的目录路径
}
//文件建立成功,开始写文件
FileWriter fw = new FileWriter(path + "\\"+wjName); //建立FileWrite对象,并设定由fw对象变量引用
FileReader fr = new FileReader(path + "\\"+strhtml); //建立FileReader变量,并设定由fr变量变数引用
System.out.println("123--->"+strhtml);
int c = fr.read();
String sHead = "";
while(c != -1) {
sHead += (char)c;
c = fr.read();
}
fr.close();


fw.write(sHead);
fw.write("<p class='tbtitle' align='center'>");
fw.write(BT+"</p><p>");
fw.write(NR);
fw.write("</p>");


fr = new FileReader(path + "\\foot.jsp"); //建立FileReader变量,并设定由fr变量变数引用
c = fr.read();
String sFoot = "";
while(c != -1) {
sFoot += (char)c;
c = fr.read();
}
fr.close();

fw.write(sFoot);

fw.close();

//生成前台静态页面文件
String Link = wjName;






//生成4位随机数的方法
public String random(){

String temp = String.valueOf((int)(java.lang.Math.random()*10000));
System.out.println("temp----->"+temp);
return temp;

}
epson1980 2004-12-09
  • 打赏
  • 举报
回复
学习
benwang6 2004-10-07
  • 打赏
  • 举报
回复
我也想用 pcdll(.net) 的方法,
originalPath 可以用127.0.0.1访问

我看大家好像都用模板里做标记,
然后用内容替换标记的方法,
pcdll(.net)的方法又简单又方便怎么很少见人用呢?
是不是这种方法有没有什么弊病?
请高手指点
mickeylm 2004-08-30
  • 打赏
  • 举报
回复
帮你顶
ddrisme 2004-08-30
  • 打赏
  • 举报
回复
千堆雪: 你好啊。看样子是高手
我们做个朋友吧! 我的qq号20687026
对了你的网站怎么进不去阿??
cceyjames 2004-08-30
  • 打赏
  • 举报
回复
给分给分,快给分,哈哈
cceyjames 2004-08-30
  • 打赏
  • 举报
回复
-_-!偶是菜鸟.....网站服务器这今天不稳定.....
qq:2128402
whirlsun 2004-08-29
  • 打赏
  • 举报
回复
CSDN论坛中,回复时网页中是局部刷新吗?如果是的话,怎么做的?
cceyjames 2004-08-29
  • 打赏
  • 举报
回复
可参考我网站http://www.95ren.com
cceyjames 2004-08-29
  • 打赏
  • 举报
回复
一、模板?
jsp无法做,只能通过java io控制来输出html。在输出之前,你需要读取该文章的所有信息,然后将信息全部读入到io,最后统一存储成为一个html文件。
存储的方式多样,我没用什么模版,自己直接用了9个*.io的文件(全部是html和javascrip代码)来实现自动生成的。

二、局部刷新?
其实在他的html里面包括javascript代码,代码里面嵌入了jsp代码(因为html不能运行jsp代码,所以只能放在javascript里面引用jsp文件),这样就实现了html的动态。
xiangbo520 2004-08-29
  • 打赏
  • 举报
回复
我也正在找这个啊!收藏!!
一点晴 2004-08-28
  • 打赏
  • 举报
回复
完整的方法都在这了,看懂了再修改自己用:

JSP直接生成HTML文件

摸版是这样定义的:
news.template

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<LINK href="../css.css" rel=stylesheet type=text/css>
</head>

<body>
<table width="500" border="0" align="center" cellpadding="0" cellspacing="2">
<tr>
<td align="center"><$title$></td>
</tr>
<tr>
<td align="center">作者:<$author$>  发布时间:<font color=#ff0000><$date$></font></td>
</tr>
<tr>
<td><$content$>
</td>

</tr>

</table>
</body>
</html>

一个生成HTML的类:
import java.io.*;

public class WriteHtml
{

public WriteHtml()
{
}

public static void save(String s, String s1, String s2)
throws WriteFileException
{
try
{
a(s1);
FileOutputStream fileoutputstream = new FileOutputStream(s1 + s2);
byte abyte0[] = s.getBytes();
fileoutputstream.write(abyte0);
fileoutputstream.close();
}
catch(IOException ioexception)
{
throw new WriteFileException();
}
}

private static void a(String s)
{
File file = new File(s);
if(!file.exists())
file.mkdirs();
}
}

一个读取摸版的类:
import java.io.*;



public class ReadTemplates
{

private static String _fldif = null;
private static Object a = new Object();

public ReadTemplates()
{
}

public static String getTlpContent(String s)
throws ReadTemplateException
{
if(_fldif == null)
synchronized(a)
{
if(_fldif == null)
try
{
System.out.println("+++++++++++++");
_fldif = a(s);
}
catch(ReadTemplateException readtemplateexception)
{
throw new ReadTemplateException("模板信息读取失败。");
}
}
return _fldif;
}

private static synchronized String a(String s)
throws ReadTemplateException
{
String s1 = null;
try
{
FileInputStream fileinputstream = new FileInputStream(s);
int i = fileinputstream.available();
byte abyte0[] = new byte[i];
fileinputstream.read(abyte0);
fileinputstream.close();
s1 = new String(abyte0);
}
catch(IOException ioexception)
{
throw new ReadTemplateException();
}
return s1;
}

}



jsp文件:

<%@ page contentType="text/html; charset=gb2312"%>
<%@ include file="/inc.jsp"%>
<%
String[] flag = {"<$title$>","<$date$>","<$author$>","<$content$>"};
String title=request.getParameter("title");
String content=request.getParameter("content");
String editer="admin";
//Session.getAttribute("s_userName");
int classid=Integer.parseInt(request.getParameter("class"));
String filePath = "";
filePath = application.getRealPath("./adminroot/news.template");
String templateContent;
try{
templateContent = ReadTemplates.getTlpContent(filePath);
}catch(ReadTemplateException e){
throw new Exception("模板信息读取失败。请联系系统管理员。");
}
templateContent = ReplaceAll.replace(templateContent,flag[0],title);
templateContent = ReplaceAll.replace(templateContent,flag[1],GetDate.getStringDate());
templateContent = ReplaceAll.replace(templateContent,flag[2],editer);
templateContent = ReplaceAll.replace(templateContent,flag[3],content);

// 根据时间得文件名与路径名
Calendar calendar = Calendar.getInstance();
String fileName = String.valueOf(calendar.getTimeInMillis()) +".shtml";
String pathName = application.getRealPath("./news")+"\\"+ calendar.get(Calendar.YEAR) +
"\\"+ (calendar.get(Calendar.MONTH)+1) +"\\"+ calendar.get(Calendar.DAY_OF_MONTH)+"\\";
try{
WriteHtml.save(templateContent,pathName,fileName);
}catch(WriteFileException we){
throw new Exception("操作失败!");
}%>


这些代码我在JAVA版已经贴了N次了。

pcdll 2004-08-28
  • 打赏
  • 举报
回复
static final String originalPath = 要生成静态页面的原jsp,用http形式;
static final String filePath = 需要放的地方,用操作系统路径;

FileWriter file = null;
java.io.InputStream l_urlStream;
java.net.URL l_url;
java.net.HttpURLConnection l_connection;
java.io.BufferedReader l_reader;

String sCurrentLine;
String sTotalString;
sCurrentLine = "";
sTotalString = "";

l_url = new java.net.URL(originalPath);
l_connection = (java.net.HttpURLConnection) l_url.openConnection();
l_connection.connect();
l_urlStream = l_connection.getInputStream();

l_reader = new java.io.BufferedReader(new java.
io.InputStreamReader(l_urlStream));
while ( (sCurrentLine = l_reader.readLine()) != null)
sTotalString += sCurrentLine + "\n";

file = new FileWriter(filePath);
file.write(sTotalString);
file.close();
Elvewyn 2004-08-28
  • 打赏
  • 举报
回复
就是用StreamWriter把内容写入到文件里吧。不知道是不是还有别的好方法?

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧