jsp生成静态新闻页(二)

news/2024/7/10 3:29:03 标签: jsp, string, calendar, html, file, path
htmledit_views">

模板页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.html" title=jsp>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.html" title=jsp>jsp&apos; starting page</title>
  </head>
  <body>
  <form action="add_do.html" title=jsp>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.html" title=jsp>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 html" title=calendar>calendar = Calendar.getInstance();
    fileame = String.valueOf(html" title=calendar>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.html" title=jsp>jsp&apos;</script>");
 
  }catch(Exception e){
    System.out.print(e.toString());
  }
%>
最后结果 生成文件 2008-09-11/1247651927156.html
2、数据库中同时保存文件标题、文件名、日期等信息,新闻内容等其他的信息就没有必要保留了;
3、前台调用直接从数据库中取XXX.htm文件名、文件标题就行了;


http://www.niftyadmin.cn/n/1425748.html

相关文章

jsp生成静态新闻页面(三)

1. 修改添加新闻内容 的 用 文件编辑器ewebeditor 2.把文件名,标题存入数据库中,方便前台调用这些生成的静态新闻页面. 1加ewebeditor后 图片显示出错 生成的是<IMG src"/UploadFile/20080716035903984.gif">但是这个静态新闻页是在2008-07-15这个文件夹里面的…

从B站源码里探索推荐算法的奥义

这两天闲来无事&#xff0c;回顾了一下前几年B站沸沸扬扬的代码泄露事件&#xff0c;大致翻阅了一些泄露的代码发现了一些有意思的事情&#xff0c;其中就包括 B站视频推荐 加权部分算法 的相关代码。 不过后期 B站官方辟谣说是老版本代码&#xff0c;并且已经进行相应的防御措…

Python3 字符串转ASCII码、字符串转16进制

字符串与ASCII码转换 #ascii转字符串 a_ascii 97 b chr(a_ascii) print(b) >a#字符串转ascii abc a x ord(abc) print(x) >97对于一个长的字符串使用如下&#xff1a; import numpy as npstr hello worldascii np.fromstring(str, dtypenp.uint8)print(ascii) &g…

【图解RSA加密算法】RSA非对称密码算法的Python实现保姆级教程 | 物联网安全 | 信息安全

系列索引&#xff1a;【图解安全加密算法】加密算法系列索引 Python保姆级实现教程 | 物联网安全 | 信息安全 文章目录一、什么是RSA加密算法二、RSA算法原理三、具体要求四、代码实现五、实验结果与心得体会一、什么是RSA加密算法 RSA加密算法是一种非对称加密算法。在公开密…

*生成水印

生成水印(添加图片,生成文字或图)问题1.只能为jpg格式的加水印2.目标图大小不同写的字怎么控制大小和位置3.想在右下角加字或图,怎么控制 添加水印package test;import java.io.*;import com.sun.image.codec.jpeg.*;import java.awt.*;import java.awt.image.BufferedImage;im…

*hibernate 双向关联和查询技巧

转贴:http://blog.csdn.net/caoyinghui1986/archive/2008/06/15/2549386.aspx 多对一的 双向关联关系使用连接表的双向关联 多对多 例子: 三个表一个关联表PRODUCTS&#xff08;产品表&#xff09; 和 CATEGORIES(类别表)一对多 PRODUCT_SUPPLY 为 SUPPLIERS&#…

【图解SHA1杂凑算法】SHA1杂凑算法的Python实现保姆级教程 | 物联网安全 | 信息安全

系列索引&#xff1a;【图解安全加密算法】加密算法系列索引 Python保姆级实现教程 | 物联网安全 | 信息安全 起初写实验时找到的代码大多基于c/c&#xff0c;python可参考的资料很少&#xff0c;所以借着这次实验的机会把自己走过坑分享一下&#xff0c;希望对大家有所帮助&am…