社区
Java EE
帖子详情
jsp如何自动生成静态页面--急啊,在线等
tender_hui
2007-06-11 10:23:57
有谁知道从数据库调出来数据后,再自动生成静态页面
...全文
186
4
打赏
收藏
jsp如何自动生成静态页面--急啊,在线等
有谁知道从数据库调出来数据后,再自动生成静态页面
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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流才行啊
JSP
极简实操入门
内容包括:
JSP
概述-脚本程序
JSP
概述-中文编码
JSP
概述-
JSP
声明
JSP
概述-
JSP
表达式
JSP
概述-
JSP
注释 异常处理-1 异常处理-2 异常处理-3
JSP
概述-判断语句(If…else块)
JSP
概述-判断语句(switch…case块)
JSP
...
html和
jsp
的区别--
静态
页面
和动态
页面
的区别
客户端使用Web浏览器(IE、FireFox等)经过网络(Network)连接到服务器上,使用HTTP协议发起一个请求(Request),告诉服务器我现在需要得到哪个
页面
,所有的请求交给Web服务器,之后WEB服务器根据用户的需要,从文件...
SpringBoot Thymeleaf模板生成
静态
html
页面
SpringBoot Thymeleaf模板生成
静态
html
页面
1 SpringBoot 整合Thymeleaf 1.1 pom.xml添加 <!-- thymeleaf模板 --> <dependency> <groupId>org.springframework.boot</groupId> <...
jsp
网站的
静态
页面
生成方案
提升网站性能的方式有很多,例如有效的使用缓存,生成
静态
页面
等等。今天要说的就是生成
静态
页面
的方式。什么叫生成
静态
页面
呢?很简单,例如我们如果访问一个链接 http://xyz.com/index.do,那么服务器就会解析这个...
动态
jsp
生成
静态
html网页
和代码分别部署在两台服务器,
页面
缓存,数据缓存,
静态
化,分布式,代码读写分离,负载均衡。这些东西都是大型网站发展 所必须经历的升级过程,前两种方案非常容易实现,
页面
缓存J2EE中用的不多,数据缓存可以...
Java EE
67,512
社区成员
225,880
社区内容
发帖
与我相关
我的任务
Java EE
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
复制链接
扫一扫
分享
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章