ZhangLiHai.Com Blog


也说象新浪、搜狐等大型网站的新闻系统

张利海 于 2003年05月22日 发表

记得一年前当时没有学java时偶尔在我们学校的论坛上看到有人说起过这样的问题,我学过asp 后来就用asp做了一个类似的东西。
现在想起来还是用java写起来简单。
主要思想就是做好n个模板然后在模板中设置标志以后替换这些标志就可以了。因为asp不能直接读取文件流所以把他写在数据库中,然后进行处理。

下面说一下在java中的做法,主题思想一样,如果你用过PHP Templates 就非常明白我在说什么了。不明白可以参考本站的文章。

代码非常简单,不必做任何解释:

表单文件
FileName:Form.html

<html>
<body>
<form method="post" action="http://localhost:9090/testServlet/servlet/CreateHtml">
文章标题:<input name=artTitle /><br/>
文章内容:<textarea name=artBody cols=42 rows=6 ></textArea><br/>
文章作者:<input name=artAuthor /><br/>
文章出处:<input name=artFrom /><br/>
<br/><input type=submit value=" 提 交 " />
</form>
</body>
</html>


模板文件:
FileName:detail.tpl

<html>
<head>
<title>H_artTitle</title>
</head>
<body>
<div align="Left">H_artBody</div>

<hr>
作者:H_artAuthor   时间:H_artDate   出自:H_artFrom
</body>
</html>


Servlet文件:
FileName:CreateHtml.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.text.*;
import TextTools;

/**
从指定模板文件中读取数据然后把把设置的变量替换掉同时生成HTML文件
File:CreateHtml.java
@Author:Alec Cheung
MSN:eboysit@hotmail.com
Date:2003-5-21
Home:http://cheung.cxc.cc
http://www.andyfans.com
http://97741.126.com
Oicq:1695925
*/

public class CreateHtml extends HttpServlet
{
TextTools myText;
Date date = new Date();
SimpleDateFormat sf,sdf;
public void init(ServletConfig conf) throws ServletException{
super.init(conf);
this.myText = new TextTools();
this.sf = new SimpleDateFormat("yyyyMMdd");
this.sdf = new SimpleDateFormat("yyyy-MM-dd");
}

public void doPost(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException{
String fileInputPath = "D:\\resin-2.1.4\\webapps\\testServlet\\templates\\detail.tpl";
String fileOutPath = "D:\\resin-2.1.4\\webapps\\testServlet\\html\\";
String newBody = req.getParameter("artBody"); //"这是内容";
newBody = myText.iso2gb(newBody);
newBody = myText.addHTML(newBody);
String newTitle = req.getParameter("artTitle"); //"这是个标题";
newTitle = myText.iso2gb(newTitle);
String newAuthor = req.getParameter("artAuthor");// "张利海";
newAuthor = myText.iso2gb(newAuthor);
String newFrom = req.getParameter("artFrom"); //"新浪网";
newFrom = myText.iso2gb(newFrom);
String newDate = sdf.format(date);
String artID = "12"; //当前新闻编号一般从数据库中取得当前id
String newFileName= sf.format(date)+"_"+artID+".html"; //"2003535.html";
StringBuffer buf = new StringBuffer();
int b;
try{
BufferedReader br = new BufferedReader(new FileReader(fileInputPath));
while((b=br.read()) != -1){
buf.append((char) b);
}//end while
br.close();
PrintWriter pw = new PrintWriter(new FileOutputStream(fileOutPath+newFileName));
pw.println(myText.temp2Html(buf.toString(),newTitle,newBody,newAuthor,newDate,newFrom));
pw.close();
}//end try
catch(IOException ex){}
}//end doPost()

public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException {
PrintWriter out = res.getWriter();
out.println("This Java Servlet doPost Method!");
}

public void destroy(){

}
}



其中用到的工具文件
FileName:TextTools.java

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

/**
字符串转换工具
File:TextTools.java
@Author:Alec Cheung
MSN:eboysit@hotmail.com
Date:2003-5-21
Home:http://cheung.cxc.cc
http://www.andyfans.com
http://97741.126.com
Oicq:1695925
*/

public class TextTools
{
public TextTools(){

}

public String Replace(String sourceStr,String oldStr,String newStr){
if(sourceStr==null) return null;
int sourceLen = sourceStr.length();
int oldLen = oldStr.length();
int starPost = 0;
int post = 0;
StringBuffer buf = new StringBuffer();
while ((post = sourceStr.indexOf(oldStr,starPost)) >= 0)
{
buf.append(sourceStr.substring(starPost,post));
buf.append(newStr);
starPost = post + oldLen;
}

if (starPost < sourceLen)
{
buf.append(sourceStr.substring(starPost));
}

return buf.toString();
}

public String temp2Html(String sourceStr,String artTitle,String artBody,String artAuthor,String artDate,String artFrom){

sourceStr = Replace(sourceStr,"H_artTitle",artTitle);
sourceStr = Replace(sourceStr,"H_artBody",artBody);
sourceStr = Replace(sourceStr,"H_artAuthor",artAuthor);
sourceStr = Replace(sourceStr,"H_artDate",artDate);
sourceStr = Replace(sourceStr,"H_artFrom",artFrom);

return sourceStr;
}

public String addHTML(String sourceStr){

sourceStr = Replace(sourceStr,">",">");
sourceStr = Replace(sourceStr,"&lt;","<");
sourceStr = Replace(sourceStr,"\r\n","&lt;br&gt;");
sourceStr = Replace(sourceStr,"\"",""");

return sourceStr;
}

public String iso2gb(String str) {
if (str != null) {
byte[] tmpbyte=null;
try {
tmpbyte=str.getBytes("ISO-8859-1");
}
catch (UnsupportedEncodingException e) {
//System.out.println("Error: Method: dbconn.iso2gb :"+e.getMessage());
}
try {
str=new String(tmpbyte,"GBK");
}
catch(UnsupportedEncodingException e) {
//System.out.println("Error: Method: dbconn.gb2iso :"+e.getMessage());
}
}
return str;
}

}



只是个简单的例子如果需要图片还需要扩展方法,并且同时把文章标标题和文件名写到数据库中,以便以后检索用。

新版本Blog中有更多内容
Copyright (C)2002-2007 All Rights Reserved Powered By:ZhangLiHai.Com