jsp如何自动生成静态页面--急啊,在线等

tender_hui 2007-06-11 10:23:57
有谁知道从数据库调出来数据后,再自动生成静态页面
...全文
186 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
cyhs03 2007-06-11
  • 打赏
  • 举报
回复
上面用Servlet做的一个向数据库添加数据时就动态生成的一个静态页面
cyhs03 2007-06-11
  • 打赏
  • 举报
回复
package servlet;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Ueser.DealString;
import biz.Newbiz;

import dto.Dto;
public class Addnews extends HttpServlet {

String filename="";
String url="";
String author="";

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String title = request.getParameter("title");
title = new String(title.getBytes("iso-8859-1"),"gb2312");
String content = request.getParameter("content");
content = new String(content.getBytes("iso-8859-1"),"gb2312");
Dto dto = new Dto();
dto = (Dto)request.getSession().getAttribute("user");
author =dto.getUsername();
url = generateHtml(request);
String imageurl = request.getScheme() +"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/upload/"+filename;
Newbiz biz;
try {
biz = new Newbiz();
dto.setTitle(title);
dto.setContent(content);
dto.setAuthor(author);
dto.setUrl(url);
dto.setImageurl(imageurl);
int i = biz.insertnew(dto);
if(i==1)
{
response.sendRedirect("/news/Selectnews");
}else
{
response.sendRedirect("/tongxi/error.html");
}
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (SQLException e1) {
e1.printStackTrace();
}
}

public String generateHtml(HttpServletRequest request)
{
String createtime = DealString.getDateTime();
String templateFilePath =
request.getScheme()+
"://"
+ request.getServerName()
+ ":"
+ request.getServerPort()
+ request.getContextPath()
+ "/news.html";
//System.out.println(templateFilePath);
String templateContent = "";
FileInputStream fileinputstream;

try {
fileinputstream = new FileInputStream("F:/work1/news/news.html");
int lenght = fileinputstream.available();
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes);
fileinputstream.close();
templateContent = new String(bytes);
} catch (FileNotFoundException e) {
System.err.println("输入文件找不到");
e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}
String title = request.getParameter("title");
String content = request.getParameter("content");
try {
title = new String(title.getBytes("iso-8859-1"),"gb2312");
content = new String(content.getBytes("iso-8859-1"),"gb2312");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String imageurl = request.getScheme()
+"://"+request.getServerName()
+":"+request.getServerPort()+request.getContextPath()+"/upload/"+filename;

templateContent = templateContent.replaceAll("###title###", title);
templateContent = templateContent.replaceAll("###author###",author);
templateContent = templateContent.replaceAll("###createtime###", createtime);
templateContent = templateContent.replace("###imageurl###", imageurl);
templateContent = templateContent.replaceAll("###content###",content);

Calendar calendar = Calendar.getInstance();
String htmlFileName = String.valueOf(calendar.getTimeInMillis())+ ".html";

String tomcatPath = request.getRealPath("/") + "news\\" + htmlFileName;
String eclipsePath = "F:/work1/news/news/" + htmlFileName;
System.out.println(tomcatPath);
System.out.println(eclipsePath);
String uurl = request.getScheme()
+ "://"
+ request.getServerName()
+ ":"
+ request.getServerPort()
+ request.getContextPath()
+"/news/"
+ htmlFileName;

try
{

FileOutputStream tomcatOutputStream;
tomcatOutputStream = new FileOutputStream(tomcatPath);
FileOutputStream eclipseOutputStream = new FileOutputStream(eclipsePath);
byte tag_bytes[] = templateContent.getBytes();

tomcatOutputStream.write(tag_bytes);
tomcatOutputStream.close();
eclipseOutputStream.write(tag_bytes);
eclipseOutputStream.close();
}
catch (FileNotFoundException e)
{
System.err.println("输出文件找不到");
}
catch (IOException e)
{
e.printStackTrace();
}
return uurl;
}

}
tender_hui 2007-06-11
  • 打赏
  • 举报
回复
能不能具体一点,谢谢
hosenbin 2007-06-11
  • 打赏
  • 举报
回复
那要用IO流才行啊

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧