`

jsp生成html文件

阅读更多

从网上找到了两种方法都是大同小异,经过我整理了一下。
先将两种方法贴出来 尊重原作者,向其致敬
1.作者:仓木泽
为了减轻服务器压力,将原来的文章管理系统由JSP文件的从数据库中取数据显示改为由jsp生成静态html文件后直接访问html文件。下面是一个简单的示例

1.buildhtml.jsp

<%@ page contentType="text/html; charset=gb2312" import="java.util.*,java.io.*"%>
<%
try{
String title="jsp生成静态html文件";
String content="小样,还搞不定你?";
String editer="hpsoft";
String filePath = "";
filePath = request.getRealPath("/")+"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);
out.print(templateContent);
templateContent=templateContent.replaceAll("###title###",title);
templateContent=templateContent.replaceAll("###content###",content);
templateContent=templateContent.replaceAll("###author###",editer);//替换掉模块中相应的地方
out.print(templateContent);
// 根据时间得文件名
Calendar calendar = Calendar.getInstance();
String fileame = String.valueOf(calendar.getTimeInMillis()) +".html";
fileame = request.getRealPath("/")+fileame;//生成的html文件保存路径
FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立文件输出流
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
}
catch(Exception e){
out.print(e.toString());
}

%>

模板文件

2. template.htm

<html>
<head>
<title>###title###</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### </td>
</tr>
<tr>
<td>###content###
</td>

</tr>

</table>
</body>
</html>
2. 作者 :xxx(不好意思给忘了)

<%@ page contentType="text/html; charset=gb2312"%>
<%
String[] flag = {"<$title$>","<$date$>","<$author$>","<$content$>"};
String title=request.getParameter("title");
String content=request.getParameter("content");
String editer="admin";
//Session.getAttribute("s_userName");
String filePath = "";
filePath = application.getRealPath("./adminroot/news.template");
String templateContent;
try{
templateContent = ReadTemplates.getTlpContent(filePath);
}catch(Exception 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("操作失败!");
}%>
import java.io.*;

public class WriteHtml {

public WriteHtml() {
}

public static void save(String s, String s1, String s2)

{
    try {
      a(s1);
      FileOutputStream fileoutputstream = new FileOutputStream(s1 + s2);
      byte abyte0[] = s.getBytes();
      fileoutputstream.write(abyte0);
      fileoutputstream.close();
    }
    catch (IOException e) {
      System.out.println( " write html error" +e.getMessage());
    }
}

private static void a(String s) {
    try {
      File file = new File(s);
      if (!file.exists())
        file.mkdirs();
    }
    catch (Exception e) {
      System.out.println( " mkdirs error!" +e.getMessage());
    }
}
}
import java.io.*;
public class ReplaceAll
{
private static Object a = new Object();
public ReplaceAll()
{
}

public String replace(String content,String flag,String temp)
{
   String str = null;

         try
         {
          //System.out.println(&quot;before-----&quot;+content);
          String s1 = content;
             str = s1.replaceAll(flag,temp);
            // System.out.println(&quot;replace after-----&quot;+str);
         }
         catch(Exception e)
         {
          System.out.println("replace all error:"+e.getMessage());
         }
         return str;

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

import java.io.*;
import java.util.*;

public class ReadTemplates
{

     private String temp = null;
     private Object a = new Object();

     public ReadTemplates()
     {
     }

     public String getTlpContent(String s)

     {
         if(temp == null)
             synchronized(a)
             {
                 if(temp == null)
                     try
                     {
                         System.out.println("----------------------------");
                         temp = a(s);
                         //System.out.println(&quot;test the temp&quot;+temp);
                     }
                     catch(Exception e)
                     {
                        System.out.println("get tlpconente error"+e.getMessage());
                     }
             }
         return temp;
     }

     private synchronized String a(String s)

     {
         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 e)
         {
          System.out.println("ioexception error"+e.getMessage());
         }
         return s1;
     }

}
<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>
下面是我整理的方法:
1.我使用了已有大部分程序,但借用了pathfile字段,将得到的路径分类的保存到系统路径下。
我写了一个bean已实现这个功能:
package com.binghe.publish.database;

import java.io.*;

public class JspToHtml {
private String temp = "";
private Object a = new Object();
public JspToHtml() {
}

public String readTemple(String filepath) {

    if (temp == null) {
      synchronized (a) {
        System.out.println("---------------");
        try {
          FileInputStream fileinputstream = new FileInputStream(filepath);
          int i = fileinputstream.available();
          byte byte0[] = new byte[i];
          fileinputstream.read(byte0);
          fileinputstream.close();
          temp = new String(byte0);
        }
        catch (FileNotFoundException ex) {
          System.out.println("FileNotFoundException error" + ex.getMessage());
        }
        catch (IOException ex1) {
          System.out.println("IOException error" + ex1.getMessage());
        }
      }

    }
    return temp;
}

public String replaceAll(String content, String flag, String s) {
    String str = null;
    String s1 = content;
    try {
      str = s1.replaceAll(flag, s);
    }
    catch (Exception ex2) {
      System.out.println("Exception error" + ex2.getMessage());
    }
    return str;
}

public static void writeHtml(String content, String pathname, String filename) {
    File file = new File(pathname);
    if (!file.exists()) {
      file.mkdir();
    }
    try {
      FileOutputStream fileoutputstream = new FileOutputStream(pathname +
          filename);
      byte byte0[] = content.getBytes();

      fileoutputstream.close();

    }
    catch (FileNotFoundException ex) {
      System.out.println("FileNotFoundException error" + ex.getMessage());
    }
    catch (IOException ex1) {
      System.out.println("IOException error" + ex1.getMessage());
    }

}

}

 

分享到:
评论

相关推荐

    jsp生成静态文件HTML

    把jsp、文件生成Html静态文件 内附有代码 不懂可以留言

    jsp生成html文档

    jsp生成html的源代码文件,放到项目中可以把动态的jsp文件转换成静态的html文档

    java/jsp生成静态页面html/htm

    java/jsp生成静态页面html/htm

    JSP 代码生成工具 JSP Maker 1.1 英文免费版.zip

    JSP Maker 是一款强大易用的代码生成器,可从MySQL和Oracle中快速生成一套完整JSP(JAVA服务器页)代码.使用生成的JSP,用户可以轻松查看,编辑,搜索以及添加删除网页上的数据库记录。生成的代码清洁并易于用户定制.JSP...

    html文件生成pdf文件

    资源提供html、jsp文件导出生成pdf文件,采用客户端js方式生成pdf文件

    让jsp编程html生成静态页面,

    java程序,让程序员编写的动态jsp最终生成静态的html,可适用,方便可以下载看看

    ajax+jsp生成\读取\写入\下载文件

    用JSP生成文件,下载后,AJAX自动更新数据,更新下载次数. 今天完成的

    JSP文件生成静态页面

    用过滤器为JSP文件生成静态页面

    java swing补丁工具,打包class文件,jsp,html,js,css,xml,jar等文件,同时也支持其他格式的文件打包

    jsp,html,js,css,xml,jar等文件花费很长时间整理的朋友,有了我这个工具,似乎这一切都变得简单,你只需要选择路径,然后在输入你的补丁文件名,程序将自动收集补丁文件生成你想要的补丁, 功能: 1.根据项目的包...

    poi创建word、生成html、itext将html转换成pdf。pd4ml将jsp转成pdf

    2.通过读取数据库更改原始的.doc文件、读取数据库生成pdf。 -然后更改webRoot下面的.doc文件。然后调用ReadWriteAndDownloadDocAction生成一 个新的word。 -读取数据库文件生成pdf调用createDBPdf类。 ...

    jsp生成图片验证码

    jsp 简单 生成 图片 验证码 直接把html 的&lt;img&gt; 的src属性指向文件就行了! 生成对应答案放在Session中。

    JSP 实现文件上传功能

    在服务器端,JSP引擎解释JSP标识和小脚本,生成所请求的内容(例如,通过访问JavaBeans组件,使用JDBCTM技术访问数据库,或者包含文件),并且将结果以HTML(或者XML)页面的形式发送回浏览器。这有助于作者保护自己...

    html转jsp工具

    2、此工具会自动在html页面的开始位置加入以下内容,并自动将html或者htm页面转为jsp后缀的文件 *" pageEncoding="utf-8" %&gt; &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt; 3、此工具能够...

    动态JSP页生成静态HTML的方法

    利用Filter的过滤功能把*.jsp过滤出来,判断是否存在对应的.html文件,如果不存在对应的.html文件,则将其内容读出写入指定的.html文件,再跳转到对应的.html即可。如果存在对应的.html文件,则直接跳转到对应的....

    flex jsp 嵌套 swf html

    jsp中嵌套 flex 生成的 swf html 自己开发系统拓扑图的例子

    java结合jsp写的上传文件代码

    // 根据系统时间生成上传后保存的文件名 String prefix = String.valueOf(now); // 保存的最终文件完整路径,保存在web根目录下的ImagesUploaded目录下 String u_name = request.getRealPath("/") + "ImagesUploaded/...

    Jsp文件上传下载(工具类源码)

    // 根据系统时间生成上传后保存的文件名 String prefix = String.valueOf(now); // 保存的最终文件完整路径,保存在web根目录下的ImagesUploaded目录下 String u_name = request.getRealPath("/") + ...

    JSP动态网页制作基础培训教程源代码.rar

    读者应该确保计算机上安装有SQL Server 2000服务器,启动SQL Server服务后,打开查询分析器,然后执行本章目录下的db.sql文件自动生成并初始化数据库。 2.将本章的代码(文件夹news)拷贝到Tomcat服务器安装目录的...

    JSP(全称JavaServer Pages)

    JSP部署于网络服务器上,可以响应客户端发送的请求,并根据请求内容动态地生成HTML、XML或其他格式文档的Web网页,然后返回给请求者。JSP技术以Java语言作为脚本语言,为用户的HTTP请求提供服务,并能与服务器上的...

    JavaWeb开发技术-JSP动作元素.pptx

    在页面被请求的时候引入一个文件 jsp:useBean 寻找或者实例化一个JavaBean jsp:setProperty 设置JavaBean的属性 jsp:getProperty 输出某个JavaBean的属性 jsp:forward 把请求转到一个新的页面 jsp:plugin 根据浏览器...

Global site tag (gtag.js) - Google Analytics