jsp生成html的方法

news.template

<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>

一个生成HTML的类:
import java.io.*;

public class WriteHtml
{

public WriteHtml()
{
}

public static void save(String s, String s1, String s2)
throws WriteFileException
{
try
{
a(s1);
FileOutputStream fileoutputstream = new FileOutputStream(s1 + s2);
byte abyte0[] = s.getBytes();
fileoutputstream.write(abyte0);
fileoutputstream.close();
}
catch(IOException ioexception)
{
throw new WriteFileException();
}
}

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

一个读取摸版的类:
import java.io.*;

public class ReadTemplates
{

private static String _fldif = null;
private static Object a = new Object();

public ReadTemplates()
{
}

public static String getTlpContent(String s)
throws ReadTemplateException
{
if(_fldif == null)
synchronized(a)
{
if(_fldif == null)
try
{
System.out.println("+++++++++++++");
_fldif = a(s);
}
catch(ReadTemplateException readtemplateexception)
{
throw new ReadTemplateException("模板信息读取失败。");
}
}
return _fldif;
}

private static synchronized String a(String s)
throws ReadTemplateException
{
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 ioexception)
{
throw new ReadTemplateException();
}
return s1;
}

}

jsp文件:

<%@ page contentType="text/html; charset=gb2312"%>
<%@ include file="/inc.jsp"%>
<%
String[] flag = {"<$title$>","<$date$>","<$author$>","<$content$>"};
String title=request.getParameter("title");
String content=request.getParameter("content");
String editer="admin";
//Session.getAttribute("s_userName");
int classid=Integer.parseInt(request.getParameter("class"));
String filePath = "";
filePath = application.getRealPath("./adminroot/news.template");
String templateContent;
try{
templateContent = ReadTemplates.getTlpContent(filePath);
}catch(ReadTemplateException 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("操作失败!");
}%>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值