大家好!我最近在搞一个用jsp制作的新闻发布系统,因为新闻的内容发布就基本固定了,我想把jsp文件自动生成html文件,减轻服务器的负担。我想请问大家如何可以自动生成html文件呢?而且我想有以下的效果:1.把数据储存在数据库里,当第一个人访问一则新闻时,可以自动生成html文件,以后的访客就直接访问已经生成的html文件。
2.当有需要修改新闻时,html文件要重新生成。就像上面1一样。
谢谢大家了!!
2.当有需要修改新闻时,html文件要重新生成。就像上面1一样。
谢谢大家了!!
解决方案 »
- 关于MVC模式的实践问题
- 欢迎喜欢j2ee的同仁们加盟
- 问个关于dao层的问题~
- 使用Middlegen-Hibernate-r4,如何能将“视图”也生成PO ,Oracle数据库
- 说说继承action的那几个类有什么不同
- 长字符串换行问题
- 高手指教,使用struts1.0编写程序,调试通过,为什么jsp提交后,文本框、密码框内还存有数据,刷新也消除不了,
- 年初到杭州比较大的一家软件公司实习,请个位前辈指教!!
- mysql 报错1005 请大神们帮忙解决下
- SSH网站统计页面浏览数?
- spring+quartz 配置多个动态定时任务出错,请帮忙看看,谢谢
- 燃煤之际!!急!!!访问ip地址的方法,为啥不能自动完成?
<%@page import="cn.nbaia.newPage.newsPage.*,cn.nbaia.newPage.newsImg.*,java.util.*,cn.nbaia.newPage.columnPage.*,cn.nbaia.company.*,cn.nbaia.link.*" %>
<%@page import="java.io.*" %>
<%//在这里如果写成“WEB-INF/templates/template.htm”程序会报错
String filePath = request.getRealPath("/")+"test/template.htm";
out.print(filePath);
String templateContent="";FileInputStream fileinputstream = new FileInputStream(filePath);//读取模块文件
int lenght = fileinputstream.available();
byte bytes[] = new byte[lenght ];
fileinputstream.read(bytes);
fileinputstream.close();templateContent = new String(bytes);
int newsid=2484;//request.getParameter("id");
newsCtl nCtl=new newsCtl();
news news=new news();
// newsImg newsimg=new newsImg();
//newsImgCtl nImgCtl=new newsImgCtl();
news=nCtl.selectOneById(newsid);
//int rector=news.getRedactor();
//String source=news.getSource();
//String datetime=news.getDateTime();
//int degree=news.getBrowseDegree();
//int companyid=news.getCompanyId();
String title=news.getTitle();
String content=news.getContent();
if(title==null) title="";
//if(source==null) source="";
//if(datetime==null) datetime="";
if(content==null) content="";
templateContent=templateContent.replaceAll("#title#",title);
templateContent=templateContent.replaceAll("#content#",content);
// 根据时间得文件名
Calendar calendar = Calendar.getInstance();
String fileame = String.valueOf(newsid) +".html";
String file = "filename/"+String.valueOf(newsid) +".html";
fileame = request.getRealPath("/")+"/filename/"+fileame;//生成的html文件保存路径
out.print(templateContent);
FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立文件输出流
byte tag_bytes[] = templateContent.getBytes("utf-8");
//String username = "00";
//username = new String(username.getBytes("ISO8859_1"),"GB2312");
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
response.sendRedirect("../"+file);
//out.print("<script>window.location.href='../"+file+"';</script>");
%>
模版页面:template.htm<%@ page contentType="text/html;charset=utf-8"%>
<html>
<head>
<title>jsp</title><meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8"><!--<link rel="stylesheet" type="text/css" href="./styles.css">--></head><body><!--top start--><SPAN id=showTop></SPAN><IE:DOWNLOAD
id=oDownload style="BEHAVIOR: url(#default#download)" />
<SCRIPT>
function topOnDownloadDone(downDate){
showTop.innerHTML=downDate
}
oDownload.startDownload('inc/top.jsp',topOnDownloadDone)
</SCRIPT>
<!--top end-->
<TABLE width="778" border="0" align="center" cellpadding="0" cellspacing="0">
<tr><td valign="top" bgcolor="#FFFFFF"><div align="center" class="d9p">#title#</div></td></tr>
<tr><td valign="top" bgcolor="#FFFFFF"><div align="center" class="d9p">#content#</div></td></tr>
</TABLE>
<!--bottom start--><SPAN id=showBottom></SPAN>
<SCRIPT>
function bottomOnDownloadDone(downDate)
{
showBottom.innerHTML=downDate
}
oDownload.startDownload('inc/bottom.jsp',bottomOnDownloadDone)
</SCRIPT><!--bottom end--></body></html>
不过它只是把jsp网页生成html网页,不能达到更新的功能。
但是我们可以把它修改一下,大概这样步骤:
1.添加新闻时,在保存新闻数据到数据库的代码后面,加上html.jsp这段代码。就相当于在保存新闻后就直接读取新闻数据,接着就生成html文件,html文件名可以用新闻代号(不要使用时间命名,方面后面更新是覆盖html文件)
2.修改新闻是,在保存修改后新闻数据的代码后面,也加上html.jsp这段代码。也就是更新新闻时就会重新读取数据,重新生成html文件,名字就是新闻代码,与之前的相同,所以会自动覆盖,达到更新的效果。
这就是我现在想到的方法。希望大家有更好的方法可以一起分享。谢谢!!
建议你仔细考虑如果几点:
1.别用JSP页面作为控制内核,而改用Servlet,否则很多优化的方案都无法实施;
2.仔细考虑“更新策略”,显然页面维护和展现需要分开实现,甚至在不同的系统中实现,这样就需要采用存储转发的模式(通俗讲就是生产者、消费者模型);
3.在展现的策略上尽量多吸取成功案例的实施方案,同时建议你加入缓存机制,这对系统性能的优化(特别是响应时间这个指标的改善)特别显著。