网站提高性能的方案有很多,网站架构方面考虑,最初的性能优化可以考虑提高单台服务器的配置。把数据库
和代码分别部署在两台服务器,页面缓存,数据缓存,静态化,分布式,代码读写分离,负载均衡。这些东西都是大型网站发展
所必须经历的升级过程,前两种方案非常容易实现,页面缓存J2EE中用的不多,数据缓存可以借助hibernate提供的第三方支持,
分布式和读写分离,我的方案是使用EJB3,均衡负载可由Apache+Tomcat提供,本文简单说一下页面静态化技术,
也就是jsp通过html模板生成html页面,比较高级点的处理就是让它自动静态化,其实无论使用什么框架原理都是相通的。我就刨开
框架的束缚,用最基础的servlet和jsp实现,自动静态化技术。
需要一个过滤器(Servlet)实现何时静态化的判断,还需要一个jsp页面实现静态化业务,然后需要静态化页面的模板
(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=gb2312" />
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<center>
<a href="../index.jsp">返回首页</a>
</center>
<div align="center">
<table width="40%" border="1" bordercolor="#000000">
<tr>
<td bordercolor="#FFFFFF">
编号:
</td>
<td bordercolor="#FFFFFF">###i1###</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
标题:
</td>
<td bordercolor="#FFFFFF">###title1###</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
作者:
</td>
<td bordercolor="#FFFFFF">###author1###</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
发布日期:
</td>
<td bordercolor="#FFFFFF">###release_date1###</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
内容:
</td>
<td bordercolor="#FFFFFF"><a href="./showDetail.jsp?id=###id1###">查看详细新闻</a></td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
<a href="./editNews.jsp?id=###id1###">修改</a>
<a href="./deleteNewsHandler.jsp?id=###id1###">删除</a>
</td>
</tr>
</table>
<br>
<table width="40%" border="1" bordercolor="#000000">
<tr>
<td bordercolor="#FFFFFF">
编号:
</td>
<td bordercolor="#FFFFFF">###i2###</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
标题:
</td>
<td bordercolor="#FFFFFF">###title2###</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
作者:
</td>
<td bordercolor="#FFFFFF">###author2###</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
发布日期:
</td>
<td bordercolor="#FFFFFF">###release_date2###</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
内容:
</td>
<td bordercolor="#FFFFFF"><a href="./showDetail.jsp?id=###id2###">查看详细新闻</a></td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
<a href="./editNews.jsp?id=###id2###">修改</a>
<a href="./deleteNewsHandler.jsp?id=###id2###">删除</a>
</td>
</tr>
</table>
<br>
<table width="40%" border="1" bordercolor="#000000">
<tr>
<td bordercolor="#FFFFFF">
编号:
</td>
<td bordercolor="#FFFFFF">###i2###</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
标题:
</td>
<td bordercolor="#FFFFFF">###title2###</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
作者:
</td>
<td bordercolor="#FFFFFF">###author2###</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
发布日期:
</td>
<td bordercolor="#FFFFFF">###release_date2###</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
内容:
</td>
<td bordercolor="#FFFFFF"><a href="./showDetail.jsp?id=###id2###">查看详细新闻</a></td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
<a href="./editNews.jsp?id=###id2###">修改</a>
<a href="./deleteNewsHandler.jsp?id=###id2###">删除</a>
</td>
</tr>
</table>
<br>
<table width="40%" border="1" bordercolor="#000000">
<tr>
<td bordercolor="#FFFFFF">
编号:
</td>
<td bordercolor="#FFFFFF">###i3###</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
标题:
</td>
<td bordercolor="#FFFFFF">###title3###</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
作者:
</td>
<td bordercolor="#FFFFFF">###author3###</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
发布日期:
</td>
<td bordercolor="#FFFFFF">###release_date3###</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
内容:
</td>
<td bordercolor="#FFFFFF"><a href="./showDetail.jsp?id=###id3###">查看详细新闻</a></td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
<a href="./editNews.jsp?id=###id3###">修改</a>
<a href="./deleteNewsHandler.jsp?id=###id3###">删除</a>
</td>
</tr>
</table>
<br>
<table width="40%" border="1" bordercolor="#000000">
<tr>
<td bordercolor="#FFFFFF">
编号:
</td>
<td bordercolor="#FFFFFF">###i4###</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
标题:
</td>
<td bordercolor="#FFFFFF">###title4###</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
作者:
</td>
<td bordercolor="#FFFFFF">###author4###</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
发布日期:
</td>
<td bordercolor="#FFFFFF">###release_date4###</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
内容:
</td>
<td bordercolor="#FFFFFF"><a href="./showDetail.jsp?id=###id4###">查看详细新闻</a></td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
<a href="./editNews.jsp?id=###id4###">修改</a>
<a href="./deleteNewsHandler.jsp?id=###id4###">删除</a>
</td>
</tr>
</table>
<br>
<table width="40%" border="1" bordercolor="#000000">
<tr>
<td bordercolor="#FFFFFF">
编号:
</td>
<td bordercolor="#FFFFFF">###i5###</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
标题:
</td>
<td bordercolor="#FFFFFF">###title5###</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
作者:
</td>
<td bordercolor="#FFFFFF">###author5###</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
发布日期:
</td>
<td bordercolor="#FFFFFF">###release_date5###</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
内容:
</td>
<td bordercolor="#FFFFFF"><a href="./showDetail.jsp?id=###id5###">查看详细新闻</a></td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
<a href="./editNews.jsp?id=###id5###">修改</a>
<a href="./deleteNewsHandler.jsp?id=###id5###">删除</a>
</td>
</tr>
</table>
<br>
</div>
<br>
<br>
</body>
</html>
过滤器的过滤方法:
public
void
doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
HttpSession session = req.getSession();
long timeNum = new Date().getTime();
String strNum = String.valueOf(session.getAttribute( " updateTime " ));
if ( null == strNum && strNum.equals( "" )) {
long Num = timeNum - Long.valueOf(strNum);
if (Num >= 7200000 ) {
session.setAttribute( " updateTime " , String.valueOf(timeNum));
res.sendRedirect(req.getContextPath() + url);
chain.doFilter(request, response);
}
} else {
session.setAttribute( " updateTime " , String.valueOf(timeNum));
res.sendRedirect(req.getContextPath() + url);
chain.doFilter(request, response);
}
chain.doFilter(request, response);
}
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
HttpSession session = req.getSession();
long timeNum = new Date().getTime();
String strNum = String.valueOf(session.getAttribute( " updateTime " ));
if ( null == strNum && strNum.equals( "" )) {
long Num = timeNum - Long.valueOf(strNum);
if (Num >= 7200000 ) {
session.setAttribute( " updateTime " , String.valueOf(timeNum));
res.sendRedirect(req.getContextPath() + url);
chain.doFilter(request, response);
}
} else {
session.setAttribute( " updateTime " , String.valueOf(timeNum));
res.sendRedirect(req.getContextPath() + url);
chain.doFilter(request, response);
}
chain.doFilter(request, response);
}
jsp的处理核心方法:
String Modelpath
=
request.getRealPath(
"
/
"
)
+
"
/news/listNewsMoban.html
"
;
//
模板文件地址
String OutHTMLpath = request.getRealPath( " / " ) + " /news/listNews.html " ; // 静态页文件地址
try {
FileStreamHelp fsh = new FileStreamHelp(); // 实例化文件操作辅助类
String htmlcode = fsh.ReadFile(Modelpath); // 读取模板文件
int i = 0 ;
List < Object > listnews = new ArrayList < Object > ();
BaseDao dao = new NewsDaoImpl();
try {
listnews = dao.listObject();
} catch (Exception e) {
e.printStackTrace();
}
Iterator < Object > it = listnews.iterator();
while (it.hasNext() && i <= 5 ) {
News news = (News) it.next();
i ++ ;
htmlcode = htmlcode.replaceAll( " ###i " + i + " ### " , String.valueOf(i));
htmlcode = htmlcode.replaceAll( " ###title " + i + " ### " , news.getTitle());
htmlcode = htmlcode.replaceAll( " ###author " + i + " ### " , news.getAuthor());
htmlcode = htmlcode.replaceAll( " ###release_date " + i + " ### " , news.getRelease_date());
htmlcode = htmlcode.replaceAll( " ###id " + i + " ### " ,String.valueOf(news.getId()));
}
fsh.WriteFile(htmlcode, OutHTMLpath); // 生成首页文件
} catch (Exception e) {
e.printStackTrace();
}
session.setAttribute( " updateTime " , new Date().getTime());
response.sendRedirect(request.getContextPath() + " /index.jsp " );
String OutHTMLpath = request.getRealPath( " / " ) + " /news/listNews.html " ; // 静态页文件地址
try {
FileStreamHelp fsh = new FileStreamHelp(); // 实例化文件操作辅助类
String htmlcode = fsh.ReadFile(Modelpath); // 读取模板文件
int i = 0 ;
List < Object > listnews = new ArrayList < Object > ();
BaseDao dao = new NewsDaoImpl();
try {
listnews = dao.listObject();
} catch (Exception e) {
e.printStackTrace();
}
Iterator < Object > it = listnews.iterator();
while (it.hasNext() && i <= 5 ) {
News news = (News) it.next();
i ++ ;
htmlcode = htmlcode.replaceAll( " ###i " + i + " ### " , String.valueOf(i));
htmlcode = htmlcode.replaceAll( " ###title " + i + " ### " , news.getTitle());
htmlcode = htmlcode.replaceAll( " ###author " + i + " ### " , news.getAuthor());
htmlcode = htmlcode.replaceAll( " ###release_date " + i + " ### " , news.getRelease_date());
htmlcode = htmlcode.replaceAll( " ###id " + i + " ### " ,String.valueOf(news.getId()));
}
fsh.WriteFile(htmlcode, OutHTMLpath); // 生成首页文件
} catch (Exception e) {
e.printStackTrace();
}
session.setAttribute( " updateTime " , new Date().getTime());
response.sendRedirect(request.getContextPath() + " /index.jsp " );