上司对我说访问量大的页面可以生成静态页面,某网站首页每3分钟生成一次静态页面,这样的话用户访问的首页就是静态页面,减轻数据库的压力。请问在java中如何做?请高手回答

解决方案 »

  1.   

    FreeMarker 可以,具体实现我忘了,你google搜一下.
      

  2.   

    FreeMarker可以的,它能够将页面模板里的内容动态生成,为html文件,适合访问量大的变化内容多的网站首页等设置。
      

  3.   

    可以这样,写一个过滤器,根据访问地址判断需要生成静态HTML的页面,第一次直接把请求foward过去,回来时把out里的流保存一下,然后计时,下次有访问时,如果没到3分钟,直接把流扔出去,否则再foward过去,再保存新的out流,再重新计时。
      

  4.   

    不多说,直接上代码:
    /*
     * Create by: zhouwenfan 
     * Email:[email protected]
     * Time: 2010-09-08
     */package com.lucene.mysql;import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.methods.GetMethod;public class CreateStaticPage {


    public static void main(String[] args) {
        
    try{
        
           HttpClient client = new HttpClient();
          
          
           //client.getHostConfiguration().setHost("http://www.baidu.com");
          
           //读取要生成静态页面的页面
           GetMethod  get = new GetMethod("http://www.baidu.com");
          
           client.executeMethod(get);
          
           String s = get.getResponseBodyAsString();
          
           //生成的静态页面存放的位置及名称
           BufferedWriter bw = new BufferedWriter(new FileWriter(new File("d://baidu.html")));
          
           bw.write(s);
           bw.close();
           get.releaseConnection();
          
         }catch (Exception ex ){
        
     System.out.print(ex);
    }
    } }
    猪楼主好运!
      

  5.   


    这个还蛮简单的,其实lz了解了response的输出流就应该可以做到了比如一般我们用ajax请求的时候,如果不写入任何返回数据的话,将返回整个页面的源码,这个源码其实就是html的了吧。
      

  6.   


    我说的可能有点不正确,应该是生成后在读取整个页面的内容存为html
      

  7.   

     HttpClient client = new HttpClient();
                    
                    
                    //client.getHostConfiguration().setHost("http://www.baidu.com");
                    
                    //读取要生成静态页面的页面
                    GetMethod  get = new GetMethod("http://www.baidu.com");
                    
                    client.executeMethod(get);
                    
                    String s = get.getResponseBodyAsString();
                    
                    //生成的静态页面存放的位置及名称
                    BufferedWriter bw = new BufferedWriter(new FileWriter(new File("d://baidu.html")));
                    
                    bw.write(s);
                    bw.close();
                    get.releaseConnection();
                    
    这种方式编码怎么设置
      

  8.   

    FreeMarker 模板,可以静态生成!
      

  9.   

    OSCache也可以的,不用生成html实现jsp标签级别或者整页面的缓存,并且可以设置缓存时间,网上有很多教程
      

  10.   

    1.起一个定时任务
    2.定时去获取首页的路径(如action等),获取值
    3.放到freeet中
    4.存放到html