jsp生成静态新闻页(二)

59 篇文章 0 订阅

模板页modle.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<table width="200" border="1">
  <tr>
    <td>标题:###title###</td>
  </tr>
  <tr>
    <td>来源:###source###</td>
  </tr>
  <tr>
    <td>发布时间:###addtime###</td>
  </tr>
  <tr>
    <td>文章:###article###</td>
  </tr>

</table>

<body>
</body>
</html>

addNews.jsp (注意这里编码是GB2312)
<%@ page language="java" contentType="text/html;charset=GB2312" errorPage=""%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP &apos;addNews.jsp&apos; starting page</title>
  </head>
  <body>
  <form action="add_do.jsp" method="post">
    <table>
  <tr>
 <td>标题:</td>
 <td><input type="text" name="title"/>  </td>
  </tr>
  <tr>
 <td>来源:</td>
 <td><input type="text" name="source"/> </td>
  </tr>
  <tr>
 <td>内容:</td>
 <td><textarea rows="10" cols="40" name="article"></textarea> </td>
  </tr>
  <tr>
 <td></td>
 <td><input type="submit" value="提交"/> <input type="reset" value="取消"/></td>
  </tr>
  </table>
  </body>
</html>

生成新闻静态页 add_do.jsp
 <%@ page language="java" contentType="text/html;charset=utf-8" errorPage=""%>
<%@ page import="java.util.*,java.io.*,java.text.*"%>

 

<%
 String title=new String(request.getParameter("title").getBytes("iso-8859-1"),"GB2312");
 // System.out.print(title);
 String source =new String(request.getParameter("source").getBytes("iso-8859-1"),"GB2312");
  String article =new String(request.getParameter("article").getBytes("iso-8859-1"),"GB2312");
  SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
  String nowTime=format.format(new Date());
  String fileame="";
  int a = 0;
  try{
    String filePath = "",path="";
    filePath = request.getRealPath("/")+"modle.html";
    filePath = filePath.replaceAll("","/");
    String templateContent="";
 
    //读取hmtl模板文件
    FileInputStream fis = new FileInputStream(filePath);
    StringBuffer content = new StringBuffer();
    DataInputStream in = new DataInputStream(fis);
    BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
    String line = null;
    while ((line = br.readLine()) != null)
    content.append(line + "/n");
    br.close();
    in.close();
    fis.close();
 
    //替换模板里面的相关部分
    templateContent = new String(content);
    templateContent=templateContent.replaceAll("###title###",title);
    templateContent=templateContent.replaceAll("###source###",source);
    templateContent=templateContent.replaceAll("###addtime###",nowTime);
    templateContent=templateContent.replaceAll("###article###",article);//替换掉模块中相应的地方
   System.out.println(templateContent);
    Calendar calendar = Calendar.getInstance();
    fileame = String.valueOf(calendar.getTimeInMillis()) +".html";
 
    //建立新闻页面存放目录
    path= request.getRealPath("/")+nowTime+"/";
    File d=new File(path);//建立代表Sub目录的File对象,并得到它的一个引用
    if(!d.exists()){//检查Sub目录是否存在
       d.mkdir();//建立Sub目录
     }
    File f=new File(path,fileame);
    if(!f.exists()){//检查File.txt是否存在
       f.createNewFile();//在当前目录下建立一个名为File.txt的文件
    }
    fileame = request.getRealPath("/")+nowTime+"/"+fileame;//生成的html文件保存路径
   
    //替换后的内容写入到文件
    FileOutputStream fos = new FileOutputStream(fileame);
    Writer output = new OutputStreamWriter(fos, "UTF-8");
    output.write(templateContent);
    output.close();
    fos.close();
    out.println(" <script>window.alert(&apos;生成静态新闻页成功&apos;);window.location.href=&apos;addNews.jsp&apos;</script>");
 
  }catch(Exception e){
    System.out.print(e.toString());
  }
%>
最后结果 生成文件 2008-09-11/1247651927156.html
2、数据库中同时保存文件标题、文件名、日期等信息,新闻内容等其他的信息就没有必要保留了;
3、前台调用直接从数据库中取XXX.htm文件名、文件标题就行了;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值