大家好!我最近在搞一个用jsp制作的新闻发布系统,因为新闻的内容发布就基本固定了,我想把jsp文件自动生成html文件,减轻服务器的负担。我想请问大家如何可以自动生成html文件呢?而且我想有以下的效果:1.把数据储存在数据库里,当第一个人访问一则新闻时,可以自动生成html文件,以后的访客就直接访问已经生成的html文件。
2.当有需要修改新闻时,html文件要重新生成。就像上面1一样。
谢谢大家了!!

解决方案 »

  1.   

    需要一个函数:抓取你的jsp地址,保存为html格式的文件。这个是jsp到html的步骤。用户访问时,如果这个html存在,就展示html,不存在就展示jsp,同时调用上个函数生成html。如果数据库有修改,再次调用上面的函数,重新生成html。
      

  2.   

    (*^__^*)...嘻嘻~~更新的时候! 把以前的html删掉! 然后再调用你生成的html的方法 不就的了! 删了再建 (*^__^*)...嘻嘻! 
      

  3.   

      我在网上见到这样一种方法:jsp自动生成静态文件(html):html.jsp<%@ page contentType="text/html;charset=utf-8"%>
    <%@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文件,名字就是新闻代码,与之前的相同,所以会自动覆盖,达到更新的效果。

    这就是我现在想到的方法。希望大家有更好的方法可以一起分享。谢谢!!
      

  4.   

    做过类似的项目,但比较复杂,所以不太好详说。
    建议你仔细考虑如果几点:
    1.别用JSP页面作为控制内核,而改用Servlet,否则很多优化的方案都无法实施;
    2.仔细考虑“更新策略”,显然页面维护和展现需要分开实现,甚至在不同的系统中实现,这样就需要采用存储转发的模式(通俗讲就是生产者、消费者模型);
    3.在展现的策略上尽量多吸取成功案例的实施方案,同时建议你加入缓存机制,这对系统性能的优化(特别是响应时间这个指标的改善)特别显著。